一、系统窗体
1)vehic类
//父类 汽车类 public abstract class Vehicle { //汽车牌照 public string CarID { get; set; } //汽车名 public string Name { get; set; } //颜色 public string Color { get; set; } //使用 时间 public int RentDate { get; set; } //日租金 public int Price { get; set; } //计算租金的抽象方法 public abstract double GetPrice(int day); }
2)Car小轿车类
//小车类 public class Car:Vehicle { //计算汽车租金 public override double GetPrice(int day) { double Price = this.Price * day; return Price; } }
3)Truck卡车类
public class Truck:Vehicle { //卡车载重 public int Load { get; set; } //计算卡车的租金 public override double GetPrice( int day) { double Price = this.Price * day; return Price; } }
4)FrmMain窗体的代码
public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } //保存可租车辆的信息 (Key : 车牌号 , Value: 车辆对象) public Dictionary<string, Vehicle> RentalCar = new Dictionary<string, Vehicle>(); //保存租车 车辆信息 (Key : 车牌号 , Value 车辆对象) public Dictionary<string, Vehicle> EndCar = new Dictionary<string, Vehicle>(); //初始化租车信息 public void Rental() { Car car1 = new Car(); car1.CarID = "京Ro7845"; car1.Color = "红色"; car1.Name = "奔驰"; car1.Price = 100; car1.RentDate = 30; Car car2 = new Car(); car2.CarID = "湘Rok545"; car2.Color = "红色"; car2.Name = "大众"; car2.Price = 500; car2.RentDate = 30; Truck truck1 = new Truck(); truck1.CarID = "粤Jo7845"; truck1.Color = "黑色"; truck1.Name = "路虎"; truck1.Price = 60; truck1.RentDate = 30; truck1.Load = 130; Truck truck2 = new Truck(); truck2.CarID = "赣Kjg845"; truck2.Color = "白色"; truck2.Name = "三菱"; truck2.Price = 120; truck2.RentDate = 30; truck2.Load = 60; RentalCar.Add(car1.CarID, car1); RentalCar.Add(truck1.CarID, truck1); RentalCar.Add(car2.CarID, car2); RentalCar.Add(truck2.CarID, truck2); } private void FrmMain_Load(object sender, EventArgs e) { //初始化租车信息 Rental(); //绑定数据到租车信息的listview上 GetDataToList(); } //给租车Listview上绑定数据 private void GetDataToList() { //清空Iistview集合的数据 重新绑定 lvlist.Items.Clear(); foreach (KeyValuePair<string,Vehicle> item in RentalCar) { ListViewItem list = new ListViewItem(item.Value.CarID); list.SubItems.Add(item.Value.Name); list.SubItems.Add(item.Value.Color); list.SubItems.Add(item.Value.RentDate.ToString()); list.SubItems.Add(item.Value.Price.ToString()); if (item.Value is Truck) { Truck truck = (Truck)item.Value; list.SubItems.Add(truck.Load.ToString()); } else { list.SubItems.Add("无"); } lvlist.Items.Add(list); } } //给还车ListView绑定数据 public void GetDataToListEnd() { //清空数据 lvlistEnd.Items.Clear(); foreach (KeyValuePair<string, Vehicle> item in EndCar) { ListViewItem list = new ListViewItem(item.Value.CarID); list.SubItems.Add(item.Value.Name); list.SubItems.Add(item.Value.Color); list.SubItems.Add(item.Value.RentDate.ToString()); list.SubItems.Add(item.Value.Price.ToString()); if (item.Value is Truck) { Truck truck = (Truck)item.Value; list.SubItems.Add(truck.Load.ToString()); } else { list.SubItems.Add("无"); } lvlistEnd.Items.Add(list); } } //租车刷新 private void button2_Click(object sender, EventArgs e) { GetDataToList(); } //租车 private void button1_Click(object sender, EventArgs e) { //判断租车人信息不为空 if (txtName.Text != "") { //判断是否选中一项 if (lvlist.SelectedItems.Count > 0) { //获取选中车辆的车牌号 string CarID = lvlist.SelectedItems[0].SubItems[0].Text; //在可租集合里面找到车牌号为CarID的车辆,并将其车对象赋值到已租车辆的集合中 EndCar.Add(CarID, RentalCar[CarID]); //移除可租车辆里该车辆的信息 RentalCar.Remove(CarID); MessageBox.Show("租车成功!!"); } else { MessageBox.Show("请选择一项!!"); } } else { MessageBox.Show("请填写租车人信息!!"); } } //还车刷新 private void button3_Click(object sender, EventArgs e) { //刷新还车list GetDataToListEnd(); } //选择选项卡时发生 private void tabControl1_Selected(object sender, TabControlEventArgs e) { if (e.TabPage == tabPage1) //租车的选项卡 { //刷新租车List GetDataToList(); } else if (e.TabPage == tabPage2) //还车选项卡 { //刷新还车list GetDataToListEnd(); } else { rdoCar.Checked = true; } } //还车结算 private void button4_Click(object sender, EventArgs e) { //判断租车时间不为空 if (txtDay.Text != "") { //获得租车的天数 int Day = Convert.ToInt32(txtDay.Text); //判断是否选中一项 if (lvlist.SelectedItems.Count > 0) { //获取选中车辆的车牌号 string CarID = lvlistEnd.SelectedItems[0].SubItems[0].Text; //调用计算价格的方法 double Price = EndCar[CarID].GetPrice(Day); MessageBox.Show("您的总价格是"+Price.ToString()); //将已借车辆从已借集合中复制到可借车辆集合中 RentalCar.Add(CarID,EndCar[CarID]); //移除已借车辆集合中的该车对象 EndCar.Remove(CarID); } else { MessageBox.Show("请选择一项!!"); } } else { MessageBox.Show("请填写租车使用时间"); } } //新车入库 private void button5_Click(object sender, EventArgs e) { //获得车牌号 string CarId = txtCarId.Text; //获得车名 string CarName = txtName.Text; //获得车颜色 string CarColor = rboColor.Text; //获得车使用时间 int Day = Convert.ToInt32(txtuseTime.Text); //获得日租金 int Price = Convert.ToInt32(txtPrice.Text); //判断是选中的卡车还是汽车 if (rdoTruck.Checked == true) { //获得载重量 int Height = Height = Convert.ToInt32(txtHeight.Text); Truck truck = new Truck(); truck.CarID = CarId; truck.Name = CarName; truck.Price = Price; truck.RentDate = Day; truck.Color = CarColor; //将新增车加入到可借集合中 RentalCar.Add(CarId,truck); } else { Car car = new Car(); car.CarID = CarId; car.Name = CarName; car.Price = Price; car.RentDate = Day; car.Color = CarColor; //将新增车加入到可借集合中 RentalCar.Add(CarId, car); } } //单选按钮 private void rdoCar_CheckedChanged(object sender, EventArgs e) { //判断是选中的卡车还是汽车 if (rdoTruck.Checked == true) { Clear(); //启用载重控件 txtHeight.Enabled = true; } else { Clear(); //禁用载重控件 txtHeight.Enabled = false; } } //清空txt文本的值 public void Clear() { txtCarId.Text = ""; txtName.Text = ""; rboColor.Text = ""; txtuseTime.Text= ""; txtPrice.Text= ""; txtCarName.Text = ""; } }
这种方法相对简单理解