汽車租賃系統
主界面如下:
關鍵點一:怎樣理清各個類之間的關系?
一共需要4個類:Car 類,Truck類,Vehicle類,VehicleUtil類
Car類:小汽車類 主要包括小汽車價格的計算方法
Truck類:貨車類 主要包括貨車費用的計算方法
Vehicle類:車輛類 描述車輛的一些基本信息
VehicleUtil類:工具類 創建汽車對象
下面附上一張類圖:
關鍵點二:租車事件
首先要有兩道驗證:即 “輸入出租人姓名驗證” 和 “選擇車輛驗證”,代碼如下:
1 if (String.IsNullOrEmpty(this.txtRenter.Text))2 {3 MessageBox.Show("請輸入租車人姓名","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);4 return;5 }6 if (this.lvRent.SelectedItems.Count == 0)7 {8 MessageBox.Show("請選擇車輛", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);9 }
租車完整代碼如下:
1 PRivate void btnRent_Click(object sender, EventArgs e) 2 { 3 string key = null; 4 if (String.IsNullOrEmpty(this.txtRenter.Text)) 5 { 6 MessageBox.Show("請輸入租車人姓名","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information); 7 return; 8 } 9 if (this.lvRent.SelectedItems.Count == 0)10 {11 MessageBox.Show("請選擇車輛", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);12 }13 else14 {15 key = lvRent.SelectedItems[0].Text;16 vehicles[key].RentUser = this.txtRenter.Text;17 rentVehicles.Add(vehicles[key].LicenseNO, vehicles[key]);18 if(vehicles.ContainsKey(key))19 {20 vehicles.Remove(key);21 }22 PrintVehicles(vehicles, lvRent);23 MessageBox.Show("已出租。", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);24 }25 }
關鍵點三:還車事件
開始依然是兩道驗證,模式如上。
驗證“選擇車輛” 和 “輸入租車天數” 關鍵代碼:
1 if (String.IsNullOrEmpty(this.txtRentDate.Text))2 {3 MessageBox.Show("請輸入租車天數", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);4 return;5 }6 if (this.lvReturn.SelectedItems.Count == 0)7 {8 MessageBox.Show("請選擇車輛", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);9 }
完整還車結算代碼如下:
1 private void btnCompute_Click(object sender, EventArgs e) 2 { 3 double totalPrice = 0; 4 string key = null; 5 if (String.IsNullOrEmpty(this.txtRentDate.Text)) 6 { 7 MessageBox.Show("請輸入租車天數", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); 8 return; 9 }10 if (this.lvReturn.SelectedItems.Count == 0)11 {12 MessageBox.Show("請選擇車輛", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);13 }14 else15 {16 key = lvReturn.SelectedItems[0].Text;17 rentVehicles[key].RentDate = int.Parse(this.txtRentDate.Text);18 //調用抽象方法19 totalPrice = rentVehicles[key].CalcPrice();20 string msg = string.Format("您的總價是{0}。", totalPrice.ToString());21 MessageBox.Show(msg, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);22 23 vehicles.Add(rentVehicles[key].LicenseNO, rentVehicles[key]);24 25 if (rentVehicles.ContainsKey(key))26 {27 rentVehicles.Remove(key);28 }29 this.PrintVehicles(rentVehicles, lvReturn);30 }31 }
關鍵點四:新車入庫
這里要注意的是:選擇轎車單選鈕的時候,卡車載重文本框是不可用狀態。選擇卡車單選鈕則是可用狀態
關鍵代碼如下:
1 if (rdoCar.Checked)2 {3 type = "car";4 }5 if (rdoTruck.Checked)6 {7 type = "truck";8 load = int.Parse(this.txtLoad.Text);9 }
新車入庫完整代碼如下:
1 private void btnAdd_Click(object sender, EventArgs e) 2 { 3 try 4 { 5 string LicenseNO = this.txtAutoNum.Text; 6 string name = this.txtName.Text; 7 string color = this.cobColor.Text; 8 int years = int.Parse(this.txtYears.Text); 9 double DailyRent = double.Parse(this.txtLetting.Text);10 int load = 0;11 string type = null;12 if (rdoCar.Checked)13 {14 type = "car";15 }16 if (rdoTruck.Checked)17 {18 type = "truck";19 load = int.Parse(this.txtLoad.Text);20 }21 Vehicle auto = VehicleUtil.CreateVehicle(LicenseNO, name, color, years, DailyRent, load, type);22 vehicles.Add(auto.LicenseNO, auto);23 MessageBox.Show("添加成功。","提示!",MessageBoxButtons.OK,MessageBoxIcon.Information);24 }25 catch (Exception ex)26 {27 MessageBox.Show("入庫數據不正確!","錯誤!",MessageBoxButtons.OK,MessageBoxIcon.Error);28 }29 finally30 {31 this.txtAutoNum.Text = "";32 this.txtLetting.Text = "";33 this.txtLoad.Text = "";34 this.txtName.Text = "";35 this.txtRentDate.Text = "";36 this.txtRenter.Text = "";37 this.txtYears.Text = "";38 }39 }
關鍵點五:刷新
首先要把listView的項清一下:
1 listView.Items.Clear();
刷新關鍵代碼如下:
1 private void PrintVehicles(Dictionary<string, Vehicle> autos, ListView listView) 2 { 3 listView.Items.Clear(); 4 if (autos.Count == 0) 5 { 6 MessageBox.Show("沒有數據", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); 7 } 8 else 9 {10 foreach (Vehicle auto in autos.Values)11 {12 ListViewItem item = new ListViewItem(auto.LicenseNO);13 if (auto is Car)14 {15 item.SubItems.AddRange(new string[]{auto.Name,auto.Color,auto.YearsOfService.ToString(),16 auto.DailyRent.ToString(),"無"});17 }18 if (auto is Truck)19 {20 item.SubItems.AddRange(new string[]{auto.Name,auto.Color,auto.YearsOfService.ToString(),21 auto.DailyRent.ToString(),((Truck)auto).Load.ToString()});22 }23 listView.Items.Add(item);24 }25 }26 }
再在按鈕中調用方法:
1 private void btnQueryRent_Click(object sender, EventArgs e)2 {3 this.PrintVehicles(vehicles, lvRent);4 }
知識回顧:窗體傳值
方法一、利用屬性傳值 BackGround:①點擊 Button按鈕,將主窗體Form1中textBox1 中的值傳到 Form2中的textBox2中。② 點擊Form2中的按鈕,將Form2中textBox的值傳給主窗體的文本框。1、 在Form2中定義一個字段,封裝成屬性:private string flag; /// <summary> /// 接收傳過來的值 /// </summary> public string Flag { get { return flag; } set { flag = value; } }2、 在Form1 Button按鈕事件中,實例化一個Form2 窗體對象,并將textBox1中的值賦給 Form2中的Flag,這樣在窗體Form2的登錄事件中就可以獲取到窗體Form1傳過來的值。窗體:Form1中的代碼:private void button1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.Flag = textBox1.Text;//關鍵地方 ↓ if (f2.ShowDialog() == DialogResult.OK) { textBox1.Text = f2.Flag; } } 窗體:Form2的Load()事件private void Form2_Load(object sender, EventArgs e) { textBox1.Text = this.flag; }3、 子窗體傳值給父窗體(回傳) 點擊Form2中的button按鈕將Form2中textBox的值傳給父窗體Form1.窗體:Form2中的代碼private void button1_Click(object sender, EventArgs e) { flag = this.textBox1.Text;//關鍵地方 ↓ this.DialogResult = DialogResult.OK; }方法二、利用子窗體中的構造函數 (實現了父窗體給子窗體的傳值,但是子窗體的值回傳暫未實現)1、 重載窗體Form2中的 構造函數string str = String.Empty;//接收傳過來的值 public Form2(string textValue) { InitializeComponent(); this.str = textValue; }2、 主窗體調用子窗體時候傳參數:主窗體Form1的Button事件Form2 f2 = new Form2(textBox1.Text); f2.ShowDialog();
新聞熱點
疑難解答