一、系統窗體 1)vehic類 //父類 汽車類 public abstract class Vehicle { //汽車牌照 public string CarID { get; set; } //汽車名 public string Name { get; set; } //顏色 public s
一、系統窗體
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 = ""; } }
這種方法相對簡單理解