C#實(shí)現(xiàn)簡(jiǎn)單的汽車(chē)租賃系統(tǒng)
最近學(xué)習(xí)了繼承,多態(tài),集合,設(shè)計(jì)模式,有一個(gè)汽車(chē)租憑系統(tǒng),給大家分享一下:
我們首先來(lái)看看我們這個(gè)系統(tǒng)的效果
1.做一個(gè)項(xiàng)目,我們首先對(duì)項(xiàng)目進(jìn)行分析
根據(jù)我們最近學(xué)的知識(shí),我們可以看出繼承,多態(tài),集合,設(shè)計(jì)模式,我們都能用到
我們把所需要的類(lèi)和簡(jiǎn)單模式中的“簡(jiǎn)單工廠(chǎng)”的工廠(chǎng)準(zhǔn)備好
類(lèi)圖:
01.車(chē)輛類(lèi)(父類(lèi))
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 汽車(chē)租賃系統(tǒng) { // 車(chē)輛類(lèi) 父類(lèi) public abstract class Vehicle { //屬性 //車(chē)牌號(hào) public string LicenseNo { get; set; } //車(chē)名 public string Name { get; set; } //顏色 public string Color { get; set; } //使用時(shí)間 public int RentDate { get; set; } //使用人 public string RentUser { get; set; } //日租金 public double DailyRent { get; set; } //還車(chē)日期 public int ReturnDate { get; set; } public Vehicle() { } //構(gòu)造 public Vehicle(string liceseno,string name,string color,int rentdate,double dailyrent) { this.Color = color; this.DailyRent = dailyrent; this.LicenseNo = liceseno; this.Name = name; this.RentDate = rentdate; } //計(jì)算價(jià)格的虛方法 public abstract double GetNum(); } }
02.子類(lèi)汽車(chē)類(lèi) (繼承 車(chē)輛類(lèi) 父類(lèi))
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 汽車(chē)租憑系統(tǒng) { //汽車(chē)類(lèi) 子類(lèi) public class Car:Vehicle { public Car() { } //構(gòu)造 public Car(string licenseno, string name, string color, int rentdate, double dailyrent) : base(licenseno, name, color, rentdate, dailyrent) { } //重寫(xiě)父類(lèi)計(jì)算價(jià)格的方法 public override double GetNum() { //日租金*租的天數(shù) double result = this.DailyRent * this.ReturnDate; return result; } } }
03.子類(lèi)卡車(chē)類(lèi) 繼承 車(chē)輛類(lèi) 父類(lèi)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 汽車(chē)租憑系統(tǒng) { //子類(lèi) public class Truck:Vehicle { //載重 public int Load { get; set; } public Truck() { } //構(gòu)造 public Truck(string licenseno,string name,string color,int rentdate,double dailyrent,int load ) :base(licenseno,name,color,rentdate,dailyrent ) { this.Load = load; } //重寫(xiě)父類(lèi)計(jì)算價(jià)格的方法 public override double GetNum() { //日租金*租的天數(shù) double result = this.DailyRent * this.ReturnDate; return result; } } }
04.“簡(jiǎn)單工廠(chǎng)”的工廠(chǎng)類(lèi)
說(shuō)這個(gè)工廠(chǎng)類(lèi),就是為了在新車(chē)入庫(kù)的時(shí)候,可以知道它是轎車(chē)還是卡車(chē),實(shí)例化不同的對(duì)象,方便使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 汽車(chē)租賃系統(tǒng) { //工廠(chǎng)類(lèi) public class VehicleFactory { //帶參靜態(tài)方法 public static Vehicle Carteshow(string liceseno, string name, string color, int rentdate, double dailyrent, int Load,string type) { //初始化父類(lèi)對(duì)象 Vehicle vehicle = null; switch (type) { //根據(jù)傳來(lái)的值,實(shí)例化對(duì)應(yīng)的對(duì)象 case"卡車(chē)": vehicle = new Truck(liceseno, name, color, rentdate, dailyrent, Load); break; case"轎車(chē)": vehicle = new Car(liceseno, name, color, rentdate, dailyrent); break; } //返回實(shí)例化對(duì)象 return vehicle; } } }
2. 剩下的就是對(duì)主窗體的功能進(jìn)行實(shí)現(xiàn)
其實(shí)租車(chē)和還車(chē)的核心就是兩個(gè)集合之間的交互
新車(chē)入庫(kù)就是使用“簡(jiǎn)單工廠(chǎng)”的設(shè)計(jì)模式進(jìn)行對(duì)應(yīng)添加
其中有個(gè)我們以前沒(méi)見(jiàn)過(guò)的控件TabControl
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 汽車(chē)租賃系統(tǒng) { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } //保存可租車(chē)輛的集合 Dictionary<string, Vehicle> vehicles=new Dictionary<string,Vehicle>(); //保存租出車(chē)的集合 Dictionary<string, Vehicle> rentvehicles=new Dictionary<string,Vehicle>(); //動(dòng)態(tài)加載listview的方法 public void New(Dictionary<string,Vehicle> list,ListView lvlist) { ListViewItem listview = null; lvlist.Items.Clear(); foreach (Vehicle item in list.Values) { if (item is Car) { 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()); } 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); } } //準(zhǔn)備可租車(chē)輛 public void Intitle() { Truck truck = new Truck("京A111","奧迪","紅色",3,240,10); Car car = new Car("京A222", "寶馬", "黑色", 3, 240); vehicles.Add(truck.LicenseNo,truck); vehicles.Add(car.LicenseNo, car); //加載數(shù)據(jù) New(vehicles,lvlist); } private void FrmMain_Load(object sender, EventArgs e) { Intitle(); } //點(diǎn)擊租車(chē)觸發(fā)的事件 private void btn_zu_Click(object sender, EventArgs e) { if(lvlist.SelectedItems.Count==0) { MessageBox.Show("請(qǐng)選中一行!"); return; } if (txt_name.Text=="") { MessageBox.Show("請(qǐng)輸入姓名!"); return; } //執(zhí)行租車(chē). //獲取車(chē)牌號(hào)的值 string carnum = lvlist.SelectedItems[0].Text; Vehicle ve= vehicles[carnum]; //直接把獲得要租的信息放入rentvehicles集合 rentvehicles.Add(carnum,ve); //刪除原來(lái)的集合 vehicles.Remove(carnum); //重新加載 New(vehicles,lvlist); MessageBox.Show("租車(chē)成功"); } private void button1_Click(object sender, EventArgs e) { //加載已出租車(chē)輛信息 New(rentvehicles,lvlist_huan); } private void btn_ji_Click(object sender, EventArgs e) { if (txt_day.Text=="") { MessageBox.Show("請(qǐng)輸入天數(shù)"); return; } if (lvlist_huan.SelectedItems.Count==0) { MessageBox.Show("請(qǐng)選擇一行"); return; } //獲取車(chē)牌號(hào)的值 string carnum1 = lvlist_huan.SelectedItems[0].Text; Vehicle ve = rentvehicles[carnum1]; //獲取租的天數(shù) int num = Convert.ToInt32(txt_day.Text); ve.ReturnDate = num; double money=ve.GetNum(); DialogResult result= MessageBox.Show("你要支付"+money+"元","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question); if (result==DialogResult.OK) { //直接把獲得要還的信息放入vehicles集合 vehicles.Add(carnum1, ve); //刪除原來(lái)的集合 rentvehicles.Remove(carnum1); //重新加載 New(rentvehicles, lvlist_huan); MessageBox.Show("還車(chē)成功"); } } //刷新按鈕 private void btn_new_Click(object sender, EventArgs e) { //重新加載 New(vehicles, lvlist); } //判斷填寫(xiě)是否正確的方法 public bool Good() { bool flag = true; if (txt_id.Text==""||txt_xing.Text==""||cmb_color.Text==""||txt_time.Text==""||txt_money.Text==""||txt_zhong.Text==""|| rdb_jiao.Text=="") { flag = false; } return flag; } //入庫(kù)按鈕點(diǎn)擊事件 private void btn_ruku_Click(object sender, EventArgs e) { if (Good())//判斷填寫(xiě)是否正確 { foreach (string item in vehicles.Keys) { if (txt_id.Text==item) { MessageBox.Show("此車(chē)牌已經(jīng)有庫(kù)存了,請(qǐng)你確認(rèn)!"); return; } } //使用"簡(jiǎn)單工廠(chǎng)" Vehicle ve = null; if (rdb_jiao.Checked == true) { ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text,Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_jiao.Text); } else { ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text, Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_ka.Text); } //添加集合 vehicles.Add(txt_id.Text, ve); MessageBox.Show("入庫(kù)成功"); //清空所要填的值選項(xiàng) txt_id.Text=""; txt_xing.Text=""; cmb_color.Text=""; txt_time.Text=""; txt_money.Text= ""; txt_zhong.Text = ""; } else { MessageBox.Show("請(qǐng)完善信息的填寫(xiě)!"); } } //選擇不同的按鈕 private void rdb_jiao_CheckedChanged(object sender, EventArgs e) { if (rdb_jiao.Checked==true) { lab_zhong.ForeColor = Color.Red; txt_zhong.Enabled = false; } else { lab_zhong.ForeColor = Color.Black; txt_zhong.Enabled = true; } } } }
我們來(lái)分類(lèi)看看其中的魅力代碼:
1.租車(chē)的界面功能
01.租車(chē)按鈕
//點(diǎn)擊租車(chē)觸發(fā)的事件 private void btn_zu_Click(object sender, EventArgs e) { //確保選中一行 if(lvlist.SelectedItems.Count==0) { MessageBox.Show("請(qǐng)選中一行!"); return; } //確保有人租車(chē) if (txt_name.Text=="") { MessageBox.Show("請(qǐng)輸入姓名!"); return; } //執(zhí)行租車(chē). //獲取車(chē)牌號(hào)的值 string carnum = lvlist.SelectedItems[0].Text; //根據(jù)車(chē)牌號(hào)獲得此車(chē)對(duì)象 Vehicle ve= vehicles[carnum]; //直接把獲得要租的信息放入rentvehicles集合 rentvehicles.Add(carnum,ve); //刪除原來(lái)的集合 vehicles.Remove(carnum); //重新加載 New(vehicles,lvlist); MessageBox.Show("租車(chē)成功");
02.刷新按鈕
//刷新按鈕 private void btn_new_Click(object sender, EventArgs e) { //重新加載 New(vehicles, lvlist); }
這里的刷新定義了一個(gè)方法,也就是動(dòng)態(tài)加載ListView的方法(Nuw方法)
這個(gè)方法有兩個(gè)參數(shù),第一個(gè)參數(shù)傳入車(chē)輛類(lèi)型集合對(duì)象,第二個(gè)傳入Listview的名字
這樣的作用就是在租車(chē)和還車(chē)時(shí)都能使用此方法進(jìn)行刷新,豈不妙哉!
//動(dòng)態(tài)加載listview的方法 public void New(Dictionary<string,Vehicle> list,ListView lvlist) { //初始化LIstviewItem對(duì)象 ListViewItem listview = null; //清除Listview,以免有沖突的值 lvlist.Items.Clear(); foreach (Vehicle item in list.Values) { //判斷賦值 if (item is Car) { 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()); } 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()); } //關(guān)聯(lián) lvlist.Items.Add(listview); } }
2.還車(chē)的界面功能
01.選擇結(jié)算按鈕
private void btn_ji_Click(object sender, EventArgs e) { //確保 是否輸入天數(shù) if (txt_day.Text=="") { MessageBox.Show("請(qǐng)輸入天數(shù)"); return; } //確保是否選中一行 if (lvlist_huan.SelectedItems.Count==0) { MessageBox.Show("請(qǐng)選擇一行"); return; } //獲取車(chē)牌號(hào)的值 string carnum1 = lvlist_huan.SelectedItems[0].Text; //根據(jù)車(chē)牌號(hào)獲得對(duì)應(yīng)的車(chē)輛對(duì)象 Vehicle ve = rentvehicles[carnum1]; //獲取租的天數(shù) int num = Convert.ToInt32(txt_day.Text); //給屬性使用天數(shù)賦值 ve.ReturnDate = num; //調(diào)用計(jì)算方法(多態(tài)的應(yīng)用) double money=ve.GetNum(); DialogResult result= MessageBox.Show("你要支付"+money+"元","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question); if (result==DialogResult.OK) { //直接把獲得要還的信息放入vehicles集合 vehicles.Add(carnum1, ve); //刪除原來(lái)的集合 rentvehicles.Remove(carnum1); //重新加載 New(rentvehicles, lvlist_huan); MessageBox.Show("還車(chē)成功"); } }
02.刷新按鈕(調(diào)用租車(chē)時(shí)寫(xiě)的方法)
private void button1_Click(object sender, EventArgs e) { //加載已出租車(chē)輛信息 New(rentvehicles,lvlist_huan); }
3.新車(chē)入庫(kù)界面功能
01.入庫(kù)按鈕
//入庫(kù)按鈕點(diǎn)擊事件 private void btn_ruku_Click(object sender, EventArgs e) { if (Good())//判斷填寫(xiě)是否正確 { //車(chē)牌號(hào)是唯一的,不能重復(fù)添加已有的車(chē)牌號(hào) foreach (string item in vehicles.Keys) { if (txt_id.Text==item) { MessageBox.Show("此車(chē)牌已經(jīng)有庫(kù)存了,請(qǐng)你確認(rèn)!"); return; } } //使用"簡(jiǎn)單工廠(chǎng)",進(jìn)行對(duì)應(yīng)添加 Vehicle ve = null; if (rdb_jiao.Checked == true) { ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text,Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_jiao.Text); } else { ve = VehicleFactory.Carteshow(txt_id.Text, txt_xing.Text, cmb_color.Text, Convert.ToInt32(txt_time.Text), Convert.ToDouble(txt_money.Text), Convert.ToInt32(txt_zhong.Text), rdb_ka.Text); } //添加集合 vehicles.Add(txt_id.Text, ve); MessageBox.Show("入庫(kù)成功"); //清空所要填的值選項(xiàng) txt_id.Text=""; txt_xing.Text=""; cmb_color.Text=""; txt_time.Text=""; txt_money.Text= ""; txt_zhong.Text = ""; } else { MessageBox.Show("請(qǐng)完善信息的填寫(xiě)!"); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
上一篇:C# webclient中文亂碼問(wèn)題解決方法
欄 目:C#教程
下一篇:C#調(diào)用Python腳本的簡(jiǎn)單示例
本文標(biāo)題:C#實(shí)現(xiàn)簡(jiǎn)單的汽車(chē)租賃系統(tǒng)
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6525.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開(kāi)的方法
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10C#停止線(xiàn)程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線(xiàn)程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子