-------------------------------------------------Vehicle类-------------------------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.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 Vehicle10 {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)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.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 : Vehicle10 {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)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.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 : Vehicle10 {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)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.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 ///20 /// 可租车的集合 21 /// 22 Dictionarydy = new Dictionary (); 23 24 /// 25 /// 已租车的集合 26 /// 27 Dictionarydoy = new Dictionary (); 28 29 /// 30 /// 窗口加载 31 /// 32 /// 33 /// 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 ///45 /// 初始化租车 46 /// 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 ///63 /// listView1绑定数据 64 /// 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 ///90 /// listView2绑定数据 91 /// 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 else109 {110 lvi2.SubItems.Add("无");111 }112 listView2.Items.Add(lvi2);113 }114 }115 116 ///117 /// 刷新118 /// 119 /// 120 /// 121 private void button1_Click(object sender, EventArgs e)122 {123 show();124 }125 126 ///127 /// 退出128 /// 129 /// 130 /// 131 private void button3_Click(object sender, EventArgs e)132 {133 this.Close();134 }135 136 ///137 /// 租车138 /// 139 /// 140 /// 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 else154 {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 ///167 /// 刷新168 /// 169 /// 170 /// 171 private void button5_Click(object sender, EventArgs e)172 {173 show2();174 }175 176 ///177 /// 结算178 /// 179 /// 180 /// 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 else188 {189 if (listView2.SelectedItems.Count < 0)190 {191 MessageBox.Show("请选择还车", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);192 }193 else194 {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 ///212 /// 入库213 /// 214 /// 215 /// 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 else225 {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 else232 {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 try236 {237 dy.Add(vehicle.LicenseNO, vehicle);238 MessageBox.Show("添加成功");239 }240 catch (Exception)241 {242 MessageBox.Show("车牌号重复");243 }244 }245 }246 247 ///248 /// 轿车249 /// 250 /// 251 /// 252 private void radioButton1_CheckedChanged(object sender, EventArgs e)253 {254 txtzz.Enabled = false;255 }256 257 ///258 /// 轿车259 /// 260 /// 261 /// 262 private void radioButton2_CheckedChanged(object sender, EventArgs e)263 {264 txtzz.Enabled = true;265 }266 }267 }
-------------------------------------------------运行结果-------------------------------------------------