小總結:在汽車租賃系統中用到了類的方法,構造函數,值類型,和引用類型
使用集合儲存數據,並能使用封裝,繼承,多態創建和操作類
關系圖
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 }