類圖:
父類(車類,抽象類)
/// <summary> /// 車輛基本信息類。搞一個抽象類玩玩 /// </summary> public abstract class Vehicle { public string Color { get; set; }//顏色 public double DailyRent { get; set; }//日租金 public string LicenseNo { get; set; }//車牌號 public string Name { get; set; }//車名 public int RentDate { get; set; }//使用時間 public string RentUser { get; set; }//租用者 public int YearsOfService { get; set; }//租用天數 public Vehicle() { } //構造 public Vehicle(string color, double dailyRent, string licenseNo,string name,int rentDate) { this.Color = color; this.Name = name; this.RentDate = rentDate; this.LicenseNo = licenseNo; this.DailyRent = dailyRent; } //結算租金抽象方法 public abstract double GetJieSuang(); }
汽車類:
/// <summary> /// 汽車類 /// </summary> public class Car:Vehicle { public Car() { } //構造 public Car(string licenseNo, string name, string color, int rentDate, double dailyRent) :base(color, dailyRent,licenseNo,name, rentDate) { } //重寫父類計算價格的方法 public override double GetJieSuang() { //日租金*租的天數 double result = this.DailyRent * this.YearsOfService; return result; } }
卡車類:
/// <summary> /// 卡車類 /// </summary> public class Truck:Vehicle { public int Load { get; set; }//卡車載重 //無參構造 public Truck() { } //構造 public Truck(string licenseNo, string name, string color, int rentDate, double dailyRent, int load) : base(color, dailyRent, licenseNo, name, rentDate) { this.Load = load; } //重寫父類計算租金的方法 public override double GetJieSuang() { //日租金*租的天數 double result = this.DailyRent * this.YearsOfService; return result; } }
具體實現:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CarRentalSystem { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.cbColor.SelectedIndex = 0;//顯示顏色 this.txtTruckLoad.Visible = false; } //保存可租用車的集合 Dictionary<string, Vehicle> vehicle = new Dictionary<string,Vehicle>(); //保存已租出的車的集合 Dictionary<string, Vehicle> rentvehcile = new Dictionary<string,Vehicle>(); //動態加載listview的方法 public void carZaiZhong() { if (rbcar.Checked == true) { this.txtTruckLoad.Visible = false; } else if(rbTruck.Checked==true) { this.txtTruckLoad.Visible = true; } } /// <summary> /// 刷新ListView的帶參方法,傳兩個不同集合 和刷新對應的ListView /// </summary> /// <param name="list"></param> /// <param name="lvlist"></param> public void New(Dictionary<string, Vehicle> list, ListView lvlist) { ListViewItem listview = null;//局部變量要賦初始值 lvlist.Items.Clear();//清除項 foreach (Vehicle item in list.Values) { //如果是Car,對應的ListView添加數據 if (item is Car) { listview = new ListViewItem();//new出ListViewItem視圖 listview.Text = item.LicenseNo;//listView第一列 listview.SubItems.Add(item.Name);//添加子項車名 listview.SubItems.Add(item.Color);//顏色 listview.SubItems.Add(item.RentDate.ToString());//使用時間 listview.SubItems.Add(item.DailyRent.ToString());//日租金
listview.SubItems.Add("無");
} else if (item is Truck) { listview = new ListViewItem(); listview.Text = item.LicenseNo; listview.SubItems.Add(item.Name); listview.SubItems.Add(item.Color); listview.SubItems.Add(item.RentDate.ToString()); listview.SubItems.Add(item.DailyRent.ToString()); listview.SubItems.Add(((Truck)item).Load.ToString()); } lvlist.Items.Add(listview);//添加到對應集合的ListView中 } } /// <summary> /// 初始化可租用車集合信息 /// </summary> public void initial() { Truck truck = new Truck("京A88888", "大卡", "紅色", 3, 900, 20);//卡車 Car car = new Car("京A99999", "奔馳", "黑色", 3, 300);//汽車信息 vehicle.Add(truck.LicenseNo, truck);//添加到可租車集合 vehicle.Add(car.LicenseNo, car); //租車數據 New(vehicle, lvCarkezu); } /// <summary> /// 初始化已租出車的信息 /// </summary> public void initialHc() { //對象初始化信息 Truck truck1 = new Truck("京B99999", "小卡", "紅色", 3, 280, 10);//卡車 Car car1 = new Car("京B99999", "紅旗", "黑色", 3, 280);//汽車信息 rentvehcile.Add(truck1.LicenseNo, truck1);//添加到已租車集合 rentvehcile.Add(car1.LicenseNo, car1); //還車數據 New(rentvehcile, lvHuanche); } //選擇結算,還車 private void button5_Click(object sender, EventArgs e) { //租用天數非空驗證 if (this.txtRendDay.Text == "") { MessageBox.Show("請輸入天數!"); return; } //確定是否選中一行 if (this.lvHuanche.SelectedItems.Count == 0) { MessageBox.Show("請選擇一行!"); return; } //獲取車牌號的值 string carnum1 = this.lvHuanche.SelectedItems[0].Text; //根據車牌號獲得對應的車輛對象 Vehicle ve = rentvehcile[carnum1]; //獲取租用天數 int num = Convert.ToInt32(this.txtRendDay.Text); //給屬性使用天數賦值 ve.YearsOfService = num; //結算租金 double money = ve.GetJieSuang(); DialogResult result = MessageBox.Show("你要支付" + money + "元", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (result == DialogResult.OK) { //直接把獲得要還的信息放入vehicles集合 vehicle.Add(carnum1, ve); //刪除原來的集合 rentvehcile.Remove(carnum1); //重新加載 New(rentvehcile, lvHuanche); MessageBox.Show("還車成功"); } } private void textBox5_TextChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { initial();//初始化信息 initialHc(); } private void button1_Click(object sender, EventArgs e) { //退出 DialogResult choice = MessageBox.Show("確定要退出嗎?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if(choice==DialogResult.OK) { Application.Exit(); } } //新車入庫車型驗證,非空驗證 public bool emptyNo() { //如果選擇了汽車 if (this.rbcar.Checked == true) { if (this.txtLicenseNo.Text != "" && this. txtDailyRent.Text != "" && this.txtName.Text != "" && this.txtRentDate.Text != "") { try { //文本框數字驗證 int rntDate = int.Parse(this.txtRentDate.Text); int dailyRent = int.Parse(this.txtDailyRent.Text); //double truckLoad = double.Parse(this.txtTruckLoad.Text); return true; } catch (Exception) { MessageBox.Show("使用時間,日租金應輸入數字!"); return false; } } else { MessageBox.Show("請完善新入庫汽車信息!"); return false; } } else if (this.rbTruck.Checked == true) { if (this.txtLicenseNo.Text != "" && this.txtDailyRent.Text != "" && this.txtName.Text != "" && this.txtRentDate.Text != ""&&this.txtTruckLoad.Text!="") { try { //文本框數字驗證 int rntDate = int.Parse(this.txtRentDate.Text); int dailyRent = int.Parse(this.txtDailyRent.Text); double truckLoad = double.Parse(this.txtTruckLoad.Text); return true; } catch (Exception) { MessageBox.Show("使用時間,日租金或卡車載重應輸入數字!"); return false; } } else { MessageBox.Show("請完善新入庫卡車信息!"); return false; } } return true;//防止報錯 } //新車入庫 private void btnRuku_Click(object sender, EventArgs e) { //文本框非空驗證 if (emptyNo()) { //對車載重的值做一個判斷,然后存入不同的集合 if(this.txtTruckLoad.Visible==false) { //車牌號是唯一的 string licenseNo=this.txtLicenseNo.Text;//車牌號 foreach (string ve in vehicle.Keys)//循環查看是否有相同的車牌號,有給出提示 { if (ve == licenseNo) { MessageBox.Show("已經有相同的汽車車牌號,請您重新確認車牌號!"); return; } } string name =this.txtName.Text;//車名 string cbColor = this.cbColor.SelectedItem.ToString();//顏色 int rentDate=Convert.ToInt32(this.txtRentDate.Text);//使用時間 double dailyRent = Convert.ToDouble(this.txtDailyRent.Text);//每日租金 //雙列集合一般用對象初始化器,單列集合用集合初始化器,在添加數據的時候比較清晰,方便但是我這里沒用。。。 Car car = new Car(licenseNo, name, cbColor, rentDate, dailyRent); //添加到可租車集合 vehicle.Add(car.LicenseNo,car); MessageBox.Show("汽車入庫成功!"); } else if (this.txtTruckLoad.Visible == true) { //車牌號是唯一的 string licenseNo = this.txtLicenseNo.Text;//車牌號 foreach (string rve in vehicle.Keys)//循環查看是否有相同的車牌號,有給出提示 { if (rve== licenseNo) { MessageBox.Show("已經有相同的卡車車牌號,請您重新確認車牌號!"); return; } } string name = this.txtName.Text;//車名 string cbColor = this.cbColor.SelectedItem.ToString();//顏色 int rentDate = Convert.ToInt32(this.txtRentDate.Text);//使用時間 double dailyRent = Convert.ToDouble(this.txtDailyRent.Text);//每日租金 int load = Convert.ToInt32(this.txtTruckLoad.Text); //初始化信息 Truck tk = new Truck(licenseNo, name, cbColor, rentDate, dailyRent,load); //添加到可租車集合 vehicle.Add(tk.LicenseNo, tk); MessageBox.Show("卡車入庫成功!"); } } } private void btnBreak_Click(object sender, EventArgs e) { //刷新 New(vehicle, lvCarkezu); } private void btnRent_Click(object sender, EventArgs e) { //租車 if (this.lvCarkezu.SelectedItems.Count == 0) { MessageBox.Show("請選中一行!"); return; } if (txtRentUser.Text == "")//非空判斷 { MessageBox.Show("請輸入租用者!"); return; } //執行租車. //獲取車牌號的值 string carnum = this.lvCarkezu.SelectedItems[0].Text; Vehicle ve = vehicle[carnum]; //直接把獲得要租的信息放入rentvehicles集合 rentvehcile.Add(carnum, ve); //刪除原來的集合 vehicle.Remove(carnum); //重新加載 New(vehicle, lvCarkezu); MessageBox.Show("租車成功"); } private void btnBreakor_Click(object sender, EventArgs e) { //刷新租出車的信息 New(rentvehcile, lvHuanche); } private void rbcar_CheckedChanged(object sender, EventArgs e) { //選中則顯示文本框 if (this.rbcar.Checked == true) { this.txtTruckLoad.Visible = false; } } private void rbTruck_CheckedChanged(object sender, EventArgs e) { //判斷是否選中 //carZaiZhong(); if (this.rbTruck.Checked == true) { this.txtTruckLoad.Visible = true; } } private void tabPage3_Click(object sender, EventArgs e) { } private void lvCarkezu_SelectedIndexChanged(object sender, EventArgs e) { } } }