你的位置:首页 > ASP.net教程

[ASP.net教程]汽车租赁系统 封装。继承和多态


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

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

 

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 //从可租车辆集合中移除车辆A63 //将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);//重新绑定 ListView70 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       else10       {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     }