C#总结项目《汽车租聘系统》项目代码实例【全注释版】
生活随笔
收集整理的這篇文章主要介紹了
C#总结项目《汽车租聘系统》项目代码实例【全注释版】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
所有代碼均為自己編寫,未盡允許,請勿轉載
因為所有代碼都有比較完整的注釋,就不再多寫文字了
汽車租聘系統(tǒng)頁面樣子,程序未加載的截圖
關系圖
?
1.Vehicle車輛信息類(車輛信息)
//車輛類public abstract class Vehicle{//車的顏色private string carColor;//車一天的租金private double carMoney;//車牌號private string carNum;//車的名字private string carName;//使用時間private int carTime;//使用人private string carUserName;//使用天數(shù)private int carDay;//構造函數(shù)public Vehicle() { } //構造函數(shù)重載public Vehicle(string carcolor,double carmoney,string carnum,string carname,int cartime,string caruesrname,int carday){this.CarColor = carcolor;this.CarMoney = carmoney;this.CarNum = carnum;this.CarName = carname;this.CarTime = cartime;this.CarUserName = caruesrname;this.CarDay = carday;}//抽象方法-計算租車的價格public abstract double Jisuan(int days);public string CarColor{get{return carColor;}set{carColor = value;}}public double CarMoney{get{return carMoney;}set{carMoney = value;}}public string CarNum{get{return carNum;}set{carNum = value;}}public string CarName{get{return carName;}set{carName = value;}}public int CarTime{get{return carTime;}set{carTime = value;}}public string CarUserName{get{return carUserName;}set{carUserName = value;}}public int CarDay{get{return carDay;}set{carDay = value;}}}2.Truck卡車類(存一個卡車多出的載重信息)
//卡車類public class Truck : Vehicle{//卡車載重private int load;//構造函數(shù)public Truck(string carcolor, double carmoney, string carnum, string carname, int cartime, string caruesrname, int carday,int load):base(carcolor, carmoney, carnum, carname, cartime, caruesrname, carday){ this.Load = load; }//重寫父類的抽象方法public override double Jisuan(int days){double jieguo = this.CarMoney * days;return jieguo;}public int Load{get{return load;}set{load = value;}}}3.Car轎車類
//汽車類public class Car : Vehicle{//構造函數(shù)public Car(string carcolor, double carmoney, string carnum, string carname, int cartime, string caruesrname, int carday): base(carcolor, carmoney, carnum, carname, cartime, caruesrname, carday){ }//重寫父類的抽象方法public override double Jisuan(int days){double jieguo = this.CarMoney * days;return jieguo;}}實現(xiàn)思路:
1.初始化車輛的數(shù)據(jù)
//存儲租車信息Dictionary<string, Vehicle> diczy = new Dictionary<string, Vehicle>();//存儲租出車的信息Dictionary<string, Vehicle> diczc = new Dictionary<string, Vehicle>();//初始化租車信息public void Chushizc(){//未租。。。//卡車diczy.Add("京A125648", new Truck("紅色", 300, "京A125648", "一汽大眾A8", 3, "", 0, 32));//汽車diczy.Add("豫A451356", new Car("白色", 120, "豫A451356", "寶馬3系T3運動款", 3, "", 0));UpdataListzy();//租出。。。//汽車diczc.Add("貴G451323", new Car("藍色", 120, "貴G451323", "奔馳A級", 3, "張三", 5));UpdataListzc();}2.聲明兩個方法來刷新待出租和已租出的車輛信息
//刷新-租車//刷新ListView列表操作public void UpdataListzy() {//清空操作this.listView1.Items.Clear();foreach(Vehicle ves in diczy.Values){ListViewItem lis = new ListViewItem(ves.CarNum);//存儲車輛載重string Load = "";//判斷是轎車還是卡車if(ves is Car){Load = "無";}else if(ves is Truck){Truck tk = ves as Truck;Load = Convert.ToString(tk.Load);}//向lis對象中添加中l(wèi)is.SubItems.AddRange(new string[] { ves.CarName, ves.CarColor, Convert.ToString(ves.CarTime), Convert.ToString(ves.CarMoney), Load });//向ListView控件中添加存儲信息的lis對象this.listView1.Items.Add(lis);}}//刷新-還車//刷新ListView列表操作public void UpdataListzc(){//清空操作this.listView2.Items.Clear();foreach (Vehicle ves in diczc.Values){ListViewItem lis = new ListViewItem(ves.CarNum);//存儲車輛載重string Load = "";//判斷是轎車還是卡車if (ves is Car){Load = "無";}else if (ves is Truck){Truck tk = ves as Truck;Load = Convert.ToString(tk.Load);}//向lis對象中添加中l(wèi)is.SubItems.AddRange(new string[] { ves.CarName, ves.CarColor, Convert.ToString(ves.CarTime), Convert.ToString(ves.CarMoney), Load });//向ListView控件中添加存儲信息的lis對象this.listView2.Items.Add(lis);}}3.設置窗體加載時發(fā)生的事情
private void Form1_Load(object sender, EventArgs e){//初始化租聘車的信息并刷新顯示Chushizc();this.comboBox1.SelectedIndex = 0;}4.點擊租車發(fā)生的事情
//點擊租車按鈕的操作private void button2_Click(object sender, EventArgs e){//獲取租用者string UserName = this.textBox1.Text.Trim();if (!UserName.Equals(string.Empty)){if (this.listView1.SelectedItems.Count == 1){//記錄查找車輛信息bool pd = false;//獲取選中的車牌號string carNum = this.listView1.SelectedItems[0].SubItems[0].Text;//遍歷循環(huán)查詢以下要租憑的車輛是否還在被租聘的列表中//因為可能用戶沒有刷新列表導致信息傳輸不及時foreach (string nums in diczy.Keys){if (nums.Equals(carNum)){pd = true;}}//判斷是否匹配到車輛信息if (pd == true){//判斷數(shù)據(jù)類型-添加相應的個數(shù)據(jù)if(diczy[carNum] is Car){diczc.Add(carNum, new Car(diczy[carNum].CarColor, diczy[carNum].CarMoney, diczy[carNum].CarNum, diczy[carNum].CarName, diczy[carNum].CarTime, UserName, 0));}else if(diczy[carNum] is Truck){diczc.Add(carNum, new Truck(diczy[carNum].CarColor, diczy[carNum].CarMoney, diczy[carNum].CarNum, diczy[carNum].CarName, diczy[carNum].CarTime, UserName, 0, Convert.ToInt32(this.listView1.SelectedItems[0].SubItems[5].Text)));}//刪除租聘信息中的數(shù)據(jù)diczy.Remove(carNum);//清除輸入的姓名this.textBox1.Text = null;//刷新車輛信息UpdataListzy();UpdataListzc();MessageBox.Show("租車成功!", ts);}else{MessageBox.Show("該車輛已經(jīng)出租,請點擊刷新刷新租車信息數(shù)據(jù)!", ts);}}else{MessageBox.Show("請選擇一輛租車信息后再進行操作!");}}else{MessageBox.Show("租用者姓名不能為空!", ts);}}5.點擊換車的操作
//換車租用天數(shù)private void button4_Click(object sender, EventArgs e){//獲取選中的鍵值string key = this.listView2.SelectedItems[0].SubItems[0].Text;//獲取用戶輸入天數(shù)string days = this.textBox2.Text.Trim();//驗證輸入天數(shù)是否為空if (days.Equals(string.Empty)){MessageBox.Show("租用天數(shù)不能為空!", ts);return;}//驗證用戶是否選中東西if (this.listView2.SelectedItems.Count != 1){MessageBox.Show("請選擇一條車輛出租信息進行操作!", ts);return;}//記錄車輛狀態(tài)bool pd = false;//驗證已出租的車輛中是否存在當前選中的車輛信息foreach(string strs in diczc.Keys){if (strs.Equals(key)){pd = true;}}//判斷車輛狀態(tài)if (pd == true){//開始結算操作try{double jieguo = diczc[key].Jisuan(Convert.ToInt32(days));MessageBox.Show("您的總價是:" + jieguo + "¥");//把信息添加到可租聘車量的信息中//判斷添加的是什么類型的車if (diczc[key] is Car){diczy.Add(diczc[key].CarNum, new Car(diczc[key].CarColor, diczc[key].CarMoney, diczc[key].CarNum, diczc[key].CarName, diczc[key].CarTime, "", 0));}else if(diczc[key] is Truck){diczy.Add(diczc[key].CarNum, new Truck(diczc[key].CarColor, diczc[key].CarMoney, diczc[key].CarNum, diczc[key].CarName, diczc[key].CarTime, "", 0, Convert.ToInt32(this.listView2.SelectedItems[0].SubItems[5].Text)));}//刪除在已出租車輛中的信息diczc.Remove(key);//清空輸入天數(shù)this.textBox2.Text = null;//刷新車輛信息UpdataListzc();UpdataListzy();}catch(Exception ex){MessageBox.Show(ex.Message);}}else{MessageBox.Show("該車輛暫無租出,請點擊刷新刷新租車信息數(shù)據(jù)!", ts);}}6.點擊入庫
private void button6_Click(object sender, EventArgs e){try{//車牌號string carnum = this.textBox7.Text.Trim();//車型號string carname = this.textBox6.Text.Trim();//車輛的顏色string carcolor = this.comboBox1.Text;//使用時間string cartime =this.textBox5.Text.Trim();//每日租金string carmoney = this.textBox4.Text.Trim();//車輛載重string load = this.textBox3.Text.Trim();//判斷if (carnum.Equals(string.Empty)){MessageBox.Show("車牌號不能為空", ts);return;}//車牌號不為空,foreach遍歷循環(huán)查詢是否存在相同的車牌號else{//待出租車輛判斷foreach(string name1 in diczy.Keys){if (name1.Equals(carnum)){MessageBox.Show("已存在相同車牌號的車輛,請檢查是否錄入信息有誤!", ts);return;}}//已出租車輛判斷foreach(string name2 in diczc.Keys){if (name2.Equals(carnum)){MessageBox.Show("已存在相同車牌號的車輛,請檢查是否錄入信息有誤!", ts);return;}}}if (carname.Equals(string.Empty)){MessageBox.Show("車型號不能為空", ts);return;}if (carcolor.Equals(string.Empty)){MessageBox.Show("車顏色不能為空!", ts);}if (cartime.Equals(string.Empty)){MessageBox.Show("使用時間不能為空", ts);return;}if (carmoney.Equals(string.Empty)){MessageBox.Show("每日租金不能為空", ts);return;}if (this.radioButton1.Checked == true){if (load.Equals(string.Empty)){//開始執(zhí)行保存操作diczy.Add(carnum, new Car(carcolor, Convert.ToDouble(carmoney), carnum, carname, Convert.ToInt32(cartime), "", 0));MessageBox.Show("保存信息成功!");UpdataListzy();Huanyuan();return;}else{MessageBox.Show("轎車不需要填寫載重!", ts);}}else if (this.radioButton2.Checked == true){if (load.Equals(string.Empty)){MessageBox.Show("卡車需要填寫載重!", ts);return;}else{//開始執(zhí)行保存操作diczy.Add(carnum, new Truck(carcolor, Convert.ToDouble(carmoney), carnum, carname, Convert.ToInt32(cartime), "", 0, Convert.ToInt32(load)));MessageBox.Show("保存信息成功!");UpdataListzy();return;}}else{MessageBox.Show("請選擇車輛類型!", ts);}}catch(Exception ex){MessageBox.Show(ex.Message);}}//還原新車入庫的空白填寫框public void Huanyuan(){//車牌號this.textBox7.Text = null;//車型號this.textBox6.Text = null;//車輛的顏色this.comboBox1.SelectedIndex = 0;//使用時間this.textBox5.Text = null;//每日租金this.textBox4.Text = null;//車輛載重this.textBox3.Text = null;//車輛類型this.radioButton1.Checked = false;this.radioButton2.Checked = false;}7.租車功能中的刷新按鈕
//租車-刷新private void button1_Click(object sender, EventArgs e){UpdataListzy();}8.還車中的刷新按鈕
//還車-刷新private void button5_Click(object sender, EventArgs e){UpdataListzc();}9.點擊退出按鈕后
private void button3_Click(object sender, EventArgs e){Application.Exit();}這個項目的具體實現(xiàn)大致就是這樣,如果有細節(jié)處遺漏,請私信說一下我補全
項目要求來自北大青鳥ACCP8.0S2深入.Net平臺和C#變成第10章的項目作業(yè)
總結
以上是生活随笔為你收集整理的C#总结项目《汽车租聘系统》项目代码实例【全注释版】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奶香大麻花怎么做?
- 下一篇: 山西的刀削面做法堪称一绝,品尝也是一门学