码迷,mamicode.com
首页 > 其他好文 > 详细

汽车租赁系统 封装。继承和多态

时间:2015-07-12 14:04:54      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

小总结:在汽车租赁系统中用到了类的方法,构造函数,值类型,和引用类型

使用集合储存数据,并能使用封装,继承,多态创建和操作类

 

1.租车。。显示系统中所有可出租的汽车,选中要出租的汽车,输入租用人 以出租汽车            

 

技术分享                                           

代码如下:

 1  public Dictionary<string, Vehicle> vehicles;//可租的车的集合保存
 2         public Dictionary<string, Vehicle> renttVehicles;//租出的车的集合保存
 3         private void Form1_Load(object sender, EventArgs e)
 4         {
 5             //将数据加载到集合中
 6             vehiclee();
 7             //默认载重量文本框不可用
 8             this.txtzai.ReadOnly = true;
 9         }
10         //构造出两辆小汽车,两辆卡车
11         public void vehiclee()
12         {
13             //实例化未出租字典类型的集合
14             vehicles = new Dictionary<string, Vehicle>();
15             Car dd = new Car("粉色", 99999, "京P1111", "捷豹TK07", 1);
16             Truck ds = new Truck("蓝色", 150, "豫C6644", "长江卡货", 2, 40);
17             vehicles.Add(dd.LicenseNo, dd);
18             vehicles.Add(ds.LicenseNo, ds);
19             //实例化已出租字典类型的集合
20             renttVehicles = new Dictionary<string, Vehicle>();
21             //出租出去的车
22             Car  car= new Car("白色", 240, "京PC9K11", "奥迪A8", 3);
23             Truck  truck= new Truck("黑色", 582, "京AP6666", "擎天柱", 5, 30);
24             renttVehicles.Add(car.LicenseNo, car);
25             renttVehicles.Add(truck.LicenseNo, truck);
26             car.RentUser = this.txtname.Text;
27 
28         }
29    public void PrintVehicles(Dictionary<string, Vehicle> rnotrent, ListView lvshow)
30         {
31             lvshow.Items.Clear();
32             foreach (Vehicle item in rnotrent.Values)
33             {
34                 ListViewItem lvitem = new ListViewItem(item.LicenseNo);
35                 if (item is Car)
36                 {
37                     lvitem.SubItems.Add(item.Name);
38                     lvitem.SubItems.Add(item.Color.ToString());
39                     lvitem.SubItems.Add(item.YearsOfService.ToString());
40                     lvitem.SubItems.Add(item.DailyRent.ToString());
41 
42                 }
43                 if (item is Truck)
44                 {
45                     lvitem.SubItems.Add(item.Name);
46                     lvitem.SubItems.Add(item.Color.ToString());
47                     lvitem.SubItems.Add(item.YearsOfService.ToString());
48                     lvitem.SubItems.Add(item.DailyRent.ToString());
49                     lvitem.SubItems.Add(((Truck)item).Load.ToString());
50                 }
51                 lvshow.Items.Add(lvitem);
52             }
53         }                                                                                                                                
技术分享
54 //租车 55 private void button2_Click(object sender, EventArgs e) 56 { 57 if (txtname.Text=="") 58 { 59 MessageBox.Show("请您输入租用者的姓名再来租用!哦"); 60 return; 61 } 62 //从可租车辆集合中移除车辆A 63 //将A添加到已租车辆集合中 64 if (listView1.SelectedItems.Count>0) 65 { 66 string number = listView1.SelectedItems[0].Text; 67 Vehicle ve = vehicles[number]; 68 vehicles.Remove(number); 69 PrintVehicles(vehicles, listView1);//重新绑定 ListView 70 renttVehicles.Add(number, ve); 71 MessageBox.Show("已出租。", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); 72 } 73 } 74 //刷新 75 private void button1_Click(object sender, EventArgs e) 76 { 77 PrintVehicles(vehicles, listView1); 78 }
1         //关闭窗体
2         private void button6_Click(object sender, EventArgs e)
3         {
4             this.Close();
5         }

2.还车。在还车列表中选择汽车信息,输入出租天数,计算租金                                    

技术分享                                                          

技术分享                                                    

    //结算
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox2.Text==string.Empty)
            {
                MessageBox.Show("请输入租出时间!");
                return;
            }
            //01.将车A从已租集合中移除   //02,将车A加入到可租车辆中
            string number = listView2.SelectedItems[0].Text;
            Vehicle ve = renttVehicles[number];
            renttVehicles.Remove(number);
            PrintVehicles(renttVehicles, listView2);//重新绑定 ListView
            vehicles.Add(number, ve);
            ve.RentDate = Convert.ToInt32(textBox2.Text);
            double money = 0;
            money = ve.CalcPrice();
            MessageBox.Show("您需要支付"+money+"元");
        }
        //刷新
        private void button4_Click(object sender, EventArgs e)
        {
            PrintVehicles(renttVehicles, listView2);
        }

3.新手入库。需要录入汽车的车牌号,车型,颜色使用时间和每日租金。如果是卡车还要录入卡车的载重,

技术分享                         

 

 1       //入库
 2         private void button5_Click(object sender, EventArgs e)
 3         {
 4             if (txthao.Text == string.Empty || txtxing.Text == string.Empty || cmdse.Text == string.Empty || txtshijan.Text == string.Empty 
 5                 || txtmei.Text == string.Empty)
 6             {
 7                 MessageBox.Show("请您都填写完整在入库!");
 8             }
 9             else
10             {
11                 string linno = txthao.Text;
12                 string xing = txtxing.Text;
13                 string color = cmdse.Text;
14                 int time = Convert.ToInt32(txtshijan.Text);
15                 double zu = Convert.ToDouble(txtmei.Text);
16                 if (radioButton1.Checked)
17                 {
18                     Car dd = new Car(color, zu, linno, xing, time);
19                     vehicles.Add(linno, dd);
20                 }
21                 if (radioButton2.Checked)
22                 {
23                     
24                     int load = Convert.ToInt32(txtzai.Text);
25                     Truck ds = new Truck(color, zu, linno, xing, time, load);
26                     vehicles.Add(linno, ds);
27                 }
28                 MessageBox.Show("添加成功@!");
29             }
30         }
31         //切换到汽车载重量变成不可选用
32         private void radioButton1_CheckedChanged(object sender, EventArgs e)
33         {
34             this.txtzai.ReadOnly = true;
35         }
36         //切换到卡车添加清空一下所有的文本框
37         private void radioButton2_CheckedChanged(object sender, EventArgs e)
38         {
39             this.txthao.Text = string.Empty;
40             this.txtmei.Text = string.Empty;
41             this.txtname.Text = string.Empty;
42             this.txtshijan.Text = string.Empty;
43             this.txtxing.Text = string.Empty;
44             this.txtzai.Text = string.Empty;
45             this.txtzai.ReadOnly = false;
46         }

技术分享                             

 

汽车租赁系统 封装。继承和多态

标签:

原文地址:http://www.cnblogs.com/xiaotangtang/p/4640873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!