首页 > 代码库 > (深入.Net平台和C#编程)第十章.课程总复习.20170413

(深入.Net平台和C#编程)第十章.课程总复习.20170413

==============================================实体==============================================

---------------------------------------车辆类(父类)---------------------------------------

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Car.entity
 8 {
 9     /// <summary>
10     /// 车辆类
11     /// </summary>
12      public abstract class Vehicle
13     {
14         /// <summary>
15         /// 颜色
16         /// </summary>
17         public string Color { get; set; }
18         /// <summary>
19         /// 日租金
20         /// </summary>
21         public double DailyRent { get; set; }
22         /// <summary>
23         /// 车牌号
24         /// </summary>
25         public string LicenseNo { get; set; }
26         /// <summary>
27         /// 车名
28         /// </summary>
29         public string Name { get; set; }
30         /// <summary>
31         /// 租用天数
32         /// </summary>
33         public int RentDate { get; set; }
34         /// <summary>
35         /// 租用人
36         /// </summary>
37         public string RentUser { get; set; }
38         /// <summary>
39         /// 使用时间
40         /// </summary>
41         public int YearsOfService { get; set; }
42 
43          /// <summary>
44          /// 计算总价抽象方法
45          /// </summary>
46          /// <returns></returns>
47         public abstract string Money();
48 
49     }
50 }
Vehicle

---------------------------------------卡车类(子类)---------------------------------------

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Car.entity
 8 {
 9     /// <summary>
10     /// 卡车
11     /// </summary>
12     public class Truck:Vehicle
13     {
14         /// <summary>
15         /// 载重
16         /// </summary>
17         public int Load { get; set; }
18 
19         /// <summary>
20         /// 计算租车总价 (租车时间*该车日租金)
21         /// </summary>
22         /// <returns></returns>
23         public override string Money()
24         {
25            string money = (base.RentDate * base.DailyRent).ToString();
26            return money;
27         }
28     }
29 }
Truck

---------------------------------------轿车类(子类)---------------------------------------

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Car.entity
 8 {
 9     /// <summary>
10     /// 轿车
11     /// </summary>
12     public class JCar:Vehicle
13     {
14 
15         /// <summary>
16         /// 计算租车总价 (租车时间*该车日租金)
17         /// </summary>
18         /// <returns></returns>
19         public override string Money()
20         {
21             string money = (base.RentDate * base.DailyRent).ToString();
22             return money;
23         }
24     }
25 }
JCar

 

==============================================窗体==============================================

技术分享技术分享技术分享

------------------------------------------------------------------------------

技术分享
  1 using Car.entity;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.ComponentModel;
  5 using System.Data;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
 11 
 12 namespace Car
 13 {
 14     public partial class frmZuCheSystem : Form
 15     {
 16         public frmZuCheSystem()
 17         {
 18             InitializeComponent();
 19         }
 20 
 21         /// <summary>
 22         /// 可租车集合
 23         /// </summary>
 24         Dictionary<string, Vehicle> KeZuChe = new Dictionary<string,Vehicle>();
 25 
 26         /// <summary>
 27         /// 已经租了的车集合
 28         /// </summary>
 29         Dictionary<string, Vehicle> YiZuChe = new Dictionary<string, Vehicle>();
 30 
 31 
 32         /// <summary>
 33         /// 加载事件
 34         /// </summary>
 35         /// <param name="sender"></param>
 36         /// <param name="e"></param>
 37         private void ZuCheSystem_Load(object sender, EventArgs e)
 38         {
 39             Init();
 40             BangKeZuChe();
 41         }
 42 
 43         /// <summary>
 44         /// 初始化可租车列表
 45         /// </summary>
 46         public void Init()
 47         {
 48             Vehicle vehicle = new JCar();
 49             vehicle.Color = "黑色";
 50             vehicle.DailyRent = 200;
 51             vehicle.LicenseNo = "粤A12345";
 52             vehicle.Name = "奥迪";
 53             vehicle.YearsOfService = 2;
 54 
 55             Vehicle vehicle1 = new JCar();
 56             vehicle1.Color = "银色";
 57             vehicle1.DailyRent = 200;
 58             vehicle1.LicenseNo = "粤B22222";
 59             vehicle1.Name = "宝马";
 60             vehicle1.YearsOfService = 2;
 61 
 62             Vehicle vehicle2 = new Truck();
 63             vehicle2.Color = "蓝色";
 64             vehicle2.DailyRent = 200;
 65             vehicle2.LicenseNo = "粤C33333";
 66             vehicle2.Name = "东风";
 67             ((Truck)vehicle2).Load = 20;
 68             vehicle2.YearsOfService = 2;
 69 
 70             KeZuChe.Add(vehicle.LicenseNo, vehicle);
 71             KeZuChe.Add(vehicle1.LicenseNo, vehicle1);
 72             KeZuChe.Add(vehicle2.LicenseNo, vehicle2);
 73         }
 74 
 75         /// <summary>
 76         /// 绑定可租车ListView数据
 77         /// </summary>
 78         public void BangKeZuChe()
 79         {
 80             //清空ListView
 81             lvKeZu.Items.Clear();
 82             //遍历可租车集合 添加到 ListView
 83             foreach (var item in KeZuChe)
 84             {
 85                 ListViewItem lvi = new ListViewItem(item.Key);
 86                 lvi.SubItems.Add(item.Value.Name);
 87                 lvi.SubItems.Add(item.Value.Color);
 88                 lvi.SubItems.Add(item.Value.YearsOfService.ToString());
 89                 lvi.SubItems.Add(item.Value.DailyRent.ToString());
 90                 if (item.Value is Truck)
 91                 {
 92                     lvi.SubItems.Add((item.Value as Truck).Load.ToString());
 93                 }
 94                 else
 95                 {
 96                     lvi.SubItems.Add("");
 97                 }
 98                 lvKeZu.Items.Add(lvi);
 99             }
100 
101         }
102 
103         /// <summary>
104         /// 单击租车
105         /// </summary>
106         /// <param name="sender"></param>
107         /// <param name="e"></param>
108         private void btnZu_Click(object sender, EventArgs e)
109         {
110             //判断
111             if (lvKeZu.SelectedItems.Count > 0)
112             {
113                 if (!string.IsNullOrEmpty(txtName.Text.Trim()))
114                 {
115                     //获取选中行的车牌号
116                     string chePaiHao = lvKeZu.SelectedItems[0].SubItems[0].Text.ToString();
117                     Vehicle vehicle = null;
118                     //判断选中的是哪种车
119                     if (KeZuChe[chePaiHao] is JCar)
120                     {
121                         vehicle = new JCar();
122                         vehicle = KeZuChe[chePaiHao];
123                     }
124                     if (KeZuChe[chePaiHao] is Truck)
125                     {
126                         vehicle = new Truck();
127                         vehicle = KeZuChe[chePaiHao];
128                         //如果选的是卡车就加一个重量属性
129                         ((Truck)vehicle).Load = ((Truck)KeZuChe[chePaiHao]).Load;
130                     }
131 
132                     vehicle.RentUser = txtName.Text;
133                     //添加指定车辆 到已租车集合
134                     YiZuChe.Add(vehicle.LicenseNo, vehicle);
135                     //删除指定车辆 的可租车集合
136                     KeZuChe.Remove(chePaiHao);
137                     MessageBox.Show("客户  " + vehicle.RentUser + "  租车成功!");
138                 }
139                 else
140                 {
141                     MessageBox.Show("请填写租用者", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
142                 }
143             }
144             else
145             {
146                 MessageBox.Show("请选择车辆","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);
147             }
148         }
149 
150         /// <summary>
151         /// 单击刷新重新绑定  可租车列表
152         /// </summary>
153         /// <param name="sender"></param>
154         /// <param name="e"></param>
155         private void btnShuaXinKeZu_Click(object sender, EventArgs e)
156         {
157             BangKeZuChe();
158         }
159 
160         /// <summary>
161         /// 单击退出
162         /// </summary>
163         /// <param name="sender"></param>
164         /// <param name="e"></param>
165         private void btnExit_Click(object sender, EventArgs e)
166         {
167             Application.Exit();
168         }
169 
170         /// <summary>
171         /// 刷新已租车列表 (结算界面)
172         /// </summary>
173         /// <param name="sender"></param>
174         /// <param name="e"></param>
175         private void btnShuaXin_Click(object sender, EventArgs e)
176         {
177             BangYiZuCheList();
178         }
179 
180         /// <summary>
181         /// 绑定已租车列表 lvJieSuan
182         /// </summary>
183         public void BangYiZuCheList()
184         {
185             //清空ListView
186             lvJieSuan.Items.Clear();
187             //遍历已租车集合 添加到 ListView
188             foreach (var item in YiZuChe)
189             {
190                 ListViewItem lvi = new ListViewItem(item.Key);
191                 lvi.SubItems.Add(item.Value.Name);
192                 lvi.SubItems.Add(item.Value.Color);
193                 lvi.SubItems.Add(item.Value.YearsOfService.ToString());
194                 lvi.SubItems.Add(item.Value.DailyRent.ToString());
195                 if (item.Value is Truck)
196                 {
197                     lvi.SubItems.Add((item.Value as Truck).Load.ToString());
198                 }
199                 lvJieSuan.Items.Add(lvi);
200             }
201         }
202 
203         /// <summary>
204         /// 更改tabControl1选项时
205         /// </summary>
206         /// <param name="sender"></param>
207         /// <param name="e"></param>
208         private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
209         {
210             //当选中还车时绑定已租车列表 lvJieSuan
211             if (tabControl1.SelectedIndex == 1)
212             {
213                 BangYiZuCheList();
214             }
215         }
216 
217         /// <summary>
218         /// 当选中发生改变
219         /// </summary>
220         /// <param name="sender"></param>
221         /// <param name="e"></param>
222         private void rbtnKa_CheckedChanged(object sender, EventArgs e)
223         {
224             txtLoad.Enabled = true;
225         }
226 
227         /// <summary>
228         /// 当选中发生改变
229         /// </summary>
230         /// <param name="sender"></param>
231         /// <param name="e"></param>
232         private void rbtnJiao_CheckedChanged(object sender, EventArgs e)
233         {
234             txtLoad.Enabled = false;
235         }
236 
237         /// <summary>
238         /// 单击入库
239         /// </summary>
240         /// <param name="sender"></param>
241         /// <param name="e"></param>
242         private void btnRuKu_Click(object sender, EventArgs e)
243         {
244             // 判断输入信息是否为空
245             if (FeiKong())
246             {
247                 Vehicle vehicle = null;
248                 //判断添加车型是哪种
249                 if (rbtnJiao.Checked == true)
250                 {
251                     vehicle = new JCar();
252                 }
253                 else
254                 {
255                     vehicle = new Truck();
256                     (vehicle as Truck).Load = int.Parse(txtLoad.Text);
257                 }
258                 vehicle.Color = cmbColor.Text;
259                 vehicle.DailyRent = double.Parse(txtDailyRent.Text);
260                 vehicle.LicenseNo = txtLicenseNo.Text;
261                 vehicle.Name = txtVehicleName.Text;
262                 vehicle.YearsOfService = int.Parse(txtYearsOfService.Text);
263 
264                 try
265                 {
266                     //当vehicle.LicenseNo(Keys)车牌号重复时,报出异常
267                     KeZuChe.Add(vehicle.LicenseNo, vehicle);
268                     MessageBox.Show("添加成功");
269                 }
270                 catch (Exception)
271                 {
272                     MessageBox.Show("已有该车牌号");
273                 }
274             }
275             else
276             {
277                 MessageBox.Show("信息不能为空!");
278             }
279         }
280 
281         /// <summary>
282         /// 判断是否为空
283         /// </summary>
284         /// <returns></returns>
285         public bool FeiKong()
286         {
287             bool isK = false;
288             if (string.IsNullOrEmpty(txtLicenseNo.Text.Trim()))
289             {
290             }
291             else if (string.IsNullOrEmpty(txtVehicleName.Text.Trim()))
292             {
293             }
294             else if (string.IsNullOrEmpty(cmbColor.Text.Trim()))
295             {
296             }
297             else if (string.IsNullOrEmpty(txtYearsOfService.Text.Trim()))
298             {
299             }
300             else if (string.IsNullOrEmpty(txtDailyRent.Text.Trim()))
301             {
302             }
303             else
304             {
305                isK = true;
306             }
307             return isK;
308         }
309 
310 
311         /// <summary>
312         /// 单击结算
313         /// </summary>
314         /// <param name="sender"></param>
315         /// <param name="e"></param>
316         private void btnJie_Click(object sender, EventArgs e)
317         {
318             //判断
319             if (lvJieSuan.SelectedItems.Count > 0)
320             {
321                 if (!string.IsNullOrEmpty(txtDay.Text.Trim()))
322                 {
323                     //找到选中还车是哪一辆
324                     string key = lvJieSuan.SelectedItems[0].Text;
325                     //按照输入的租车天数给该车属性赋值
326                     YiZuChe[key].RentDate = int.Parse(txtDay.Text);
327                     //调用该车的计算总价方法 并输出总价
328                     MessageBox.Show("还车成功,总价为:" + YiZuChe[key].Money());
329                     //还车成功后, 在可租车集合中添加该车
330                     KeZuChe.Add(YiZuChe[key].LicenseNo,YiZuChe[key]);
331                     //还车成功后, 在已租车集合中删除该车
332                     YiZuChe.Remove(key);
333                 }
334                 else
335                 {
336                     MessageBox.Show("请输入租车天数!");
337                 }
338             }
339             else
340             {
341                 MessageBox.Show("请选中一辆车!");
342             }
343         }
344     }
345 }
frmZuCheSystem

 

(深入.Net平台和C#编程)第十章.课程总复习.20170413