-------------------------------------------------Vehicle类-------------------------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Qichechuzxt 8 { 9 public abstract class Vehicle 10 { 11 //颜色 12 public string Color { get; set; } 13 14 //日租金 15 public double DailyRent { get; set; } 16 17 //车牌号 18 public string LicenseNO { get; set; } 19 20 //车名称 21 public string Name { get; set; } 22 23 //时间 24 public int RentDate { get; set; } 25 26 //使用人 27 public string RentUser { get; set; } 28 29 //使用年限 30 public int YearsOfService { get; set; } 31 32 //构造函数 33 public Vehicle(string color, double dailyrent, string licenseNO, string name, int rentDate, string rentUser, int yearsOfService) 34 { 35 this.Color = color; 36 this.DailyRent = dailyrent; 37 this.LicenseNO = licenseNO; 38 this.Name = name; 39 this.RentDate = rentDate; 40 this.RentUser = rentUser; 41 this.YearsOfService = yearsOfService; 42 } 43 44 //方法重写 45 public Vehicle() { } 46 47 //执行 48 public abstract double CalcPrice(); 49 } 50 }
-------------------------------------------------Truck类--------------------------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Qichechuzxt 8 { 9 public class Truck : Vehicle 10 { 11 //载重 12 public int Load { get; set; } 13 14 //构造函数 15 public Truck(string color, double dailyrent, string licenseNO, string name, int rentDate, string rentUser, int yearsOfService, int load) 16 : base(color, dailyrent, licenseNO, name, rentDate, rentUser, yearsOfService) 17 { 18 this.Load = load; 19 } 20 21 //计算价格 22 public override double CalcPrice() 23 { 24 double totalPrice = 0; 25 totalPrice = this.DailyRent * this.RentDate; 26 return totalPrice; 27 } 28 } 29 }
-------------------------------------------------Car类----------------------------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Qichechuzxt 8 { 9 public class Car : Vehicle 10 { 11 //构造函数 12 public Car(string color, double dailyrent, string licenseNO, string name, int rentDate, string rentUser, int yearsOfService) 13 : base(color, dailyrent, licenseNO, name, rentDate, rentUser, yearsOfService) 14 { 15 16 } 17 18 //计算价格 19 public override double CalcPrice() 20 { 21 double totalPrice = 0; 22 totalPrice = this.DailyRent * this.RentDate; 23 return totalPrice; 24 } 25 } 26 }
-------------------------------------------------formChuztx主窗体---------------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace Qichechuzxt 12 { 13 public partial class formChuztx : Form 14 { 15 public formChuztx() 16 { 17 InitializeComponent(); 18 } 19 /// <summary> 20 /// 可租车的集合 21 /// </summary> 22 Dictionary<string , Vehicle> dy = new Dictionary<string,Vehicle>(); 23 24 /// <summary> 25 /// 已租车的集合 26 /// </summary> 27 Dictionary<string , Vehicle> doy = new Dictionary<string,Vehicle>(); 28 29 /// <summary> 30 /// 窗口加载 31 /// </summary> 32 /// <param name="sender"></param> 33 /// <param name="e"></param> 34 private void formChuztx_Load(object sender, EventArgs e) 35 { 36 Init(); 37 cmbys.Text = "请选择"; 38 cmbys.Items.Add("白色"); 39 cmbys.Items.Add("黑色"); 40 cmbys.Items.Add("红色"); 41 cmbys.Items.Add("银白色"); 42 } 43 44 /// <summary> 45 /// 初始化租车 46 /// </summary> 47 public void Init() 48 { 49 Car c = new Car("白色", 600, "京A666666", "兰博基尼", 3, "ss", 20); 50 dy.Add(c.LicenseNO, c); 51 52 Car car = new Car("红色", 500, "湘A999999", "法拉利", 3, "ss", 10); 53 dy.Add(car.LicenseNO, car); 54 55 Truck t = new Truck("黑色", 300, "粤A333333", "东风", 3, "ss", 30,2000); 56 doy.Add(t.LicenseNO, t); 57 58 Truck tk = new Truck("蓝色", 300, "粤A888888", "东风", 3, "ss", 10, 1000); 59 doy.Add(tk.LicenseNO, tk); 60 } 61 62 /// <summary> 63 /// listView1绑定数据 64 /// </summary> 65 /// 66 public void show() 67 { 68 listView1.Items.Clear(); 69 foreach (var item in dy) 70 { 71 ListViewItem lvi = new ListViewItem(item.Key); 72 lvi.SubItems.Add(item.Value.Name); 73 lvi.SubItems.Add(item.Value.Color); 74 lvi.SubItems.Add(item.Value.RentDate.ToString()); 75 lvi.SubItems.Add(item.Value.DailyRent.ToString()); 76 77 if (item.Value is Truck) 78 { 79 lvi.SubItems.Add(((item.Value) as Truck).Load.ToString()); 80 } 81 else 82 { 83 lvi.SubItems.Add("无"); 84 } 85 listView1.Items.Add(lvi); 86 } 87 } 88 89 /// <summary> 90 /// listView2绑定数据 91 /// </summary> 92 /// 93 public void show2() 94 { 95 listView2.Items.Clear(); 96 foreach (var item in doy) 97 { 98 ListViewItem lvi2 = new ListViewItem(item.Key); 99 lvi2.SubItems.Add(item.Value.Name); 100 lvi2.SubItems.Add(item.Value.Color); 101 lvi2.SubItems.Add(item.Value.RentDate.ToString()); 102 lvi2.SubItems.Add(item.Value.DailyRent.ToString()); 103 104 if (item.Value is Truck) 105 { 106 lvi2.SubItems.Add(((item.Value) as Truck).Load.ToString()); 107 } 108 else 109 { 110 lvi2.SubItems.Add("无"); 111 } 112 listView2.Items.Add(lvi2); 113 } 114 } 115 116 /// <summary> 117 /// 刷新 118 /// </summary> 119 /// <param name="sender"></param> 120 /// <param name="e"></param> 121 private void button1_Click(object sender, EventArgs e) 122 { 123 show(); 124 } 125 126 /// <summary> 127 /// 退出 128 /// </summary> 129 /// <param name="sender"></param> 130 /// <param name="e"></param> 131 private void button3_Click(object sender, EventArgs e) 132 { 133 this.Close(); 134 } 135 136 /// <summary> 137 /// 租车 138 /// </summary> 139 /// <param name="sender"></param> 140 /// <param name="e"></param> 141 private void button2_Click(object sender, EventArgs e) 142 { 143 if (string.IsNullOrEmpty(txtName.Text.Trim())) 144 { 145 MessageBox.Show("请填写租车姓名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 146 } 147 else 148 { 149 if (listView1.SelectedItems.Count < 0) 150 { 151 MessageBox.Show("请选择租车", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 152 } 153 else 154 { 155 string key = listView1.SelectedItems[0].Text; 156 doy.Add(dy[key].LicenseNO, dy[key]); 157 if (dy.ContainsKey(key)) 158 { 159 dy.Remove(key); 160 show(); 161 } 162 } 163 } 164 } 165 166 /// <summary> 167 /// 刷新 168 /// </summary> 169 /// <param name="sender"></param> 170 /// <param name="e"></param> 171 private void button5_Click(object sender, EventArgs e) 172 { 173 show2(); 174 } 175 176 /// <summary> 177 /// 结算 178 /// </summary> 179 /// <param name="sender"></param> 180 /// <param name="e"></param> 181 private void button4_Click(object sender, EventArgs e) 182 { 183 if (string.IsNullOrEmpty(txttshu.Text.Trim())) 184 { 185 MessageBox.Show("请输入租车天数","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk); 186 } 187 else 188 { 189 if (listView2.SelectedItems.Count < 0) 190 { 191 MessageBox.Show("请选择还车", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 192 } 193 else 194 { 195 string key = listView2.SelectedItems[0].Text; 196 doy[key].RentDate = int.Parse(this.txttshu.Text); 197 double a = doy[key].DailyRent; 198 double totalPrice = doy[key].CalcPrice(); 199 string msg = string.Format("您的总价是:" + totalPrice.ToString()); 200 MessageBox.Show(msg, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); 201 dy.Add(doy[key].LicenseNO, doy[key]); 202 if (doy.ContainsKey(key)) 203 { 204 doy.Remove(key); 205 this.show2(); 206 } 207 } 208 } 209 } 210 211 /// <summary> 212 /// 入库 213 /// </summary> 214 /// <param name="sender"></param> 215 /// <param name="e"></param> 216 private void button6_Click(object sender, EventArgs e) 217 { 218 if (string.IsNullOrEmpty(txtchep.Text.Trim()) || string.IsNullOrEmpty(txtchex.Text.Trim()) 219 || string.IsNullOrEmpty(cmbys.Text.Trim()) || string.IsNullOrEmpty(txtje.Text.Trim()) 220 || string.IsNullOrEmpty(txtsj.Text.Trim()) ) 221 { 222 MessageBox.Show("请完善新车入库信息","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); 223 } 224 else 225 { 226 Vehicle vehicle = null; 227 if (radioButton1.Checked == true) 228 { 229 vehicle = new Car(cmbys.Text, int.Parse(txtje.Text), txtchep.Text, txtchex.Text, 0, "s", int.Parse(txtsj.Text)); 230 } 231 else 232 { 233 vehicle = new Truck(cmbys.Text, int.Parse(txtje.Text), txtchep.Text, txtchex.Text, int.Parse(txtsj.Text), txtsj.Text,2,int.Parse(txtzz.Text)); 234 } 235 try 236 { 237 dy.Add(vehicle.LicenseNO, vehicle); 238 MessageBox.Show("添加成功"); 239 } 240 catch (Exception) 241 { 242 MessageBox.Show("车牌号重复"); 243 } 244 } 245 } 246 247 /// <summary> 248 /// 轿车 249 /// </summary> 250 /// <param name="sender"></param> 251 /// <param name="e"></param> 252 private void radioButton1_CheckedChanged(object sender, EventArgs e) 253 { 254 txtzz.Enabled = false; 255 } 256 257 /// <summary> 258 /// 轿车 259 /// </summary> 260 /// <param name="sender"></param> 261 /// <param name="e"></param> 262 private void radioButton2_CheckedChanged(object sender, EventArgs e) 263 { 264 txtzz.Enabled = true; 265 } 266 } 267 }
-------------------------------------------------运行结果-------------------------------------------------