First, the System form
Second, the Thinking Analysis:
We see that there are three classes that are: Vehicle Vehicle class The parent car and truck are Vehicle, and its subclasses need to be written in the knowledge points of inheritance and polymorphism, simple factory
1) Vehic class
Public abstract class Vehicle { //no parameter Public Vehicle () { } //Parameter public Vehicle ( String Name, String Licenseno, string Color, int yearsofservice, double dailyrent) {this . name = name; This. Licenseno = Licenseno; This. color = color; This. Yearsofservice = Yearsofservice; This. Dailyrent = dailyrent; } Color public string color {get; set;} Daily rent public double dailyrent {get; set;} Vehicle Grade public string Licenseno {get; set;} Car name public string name {get; set;} Lease date Public int Rentdate {get; set;} Hirer public string rentuser {get; set;} Used time public int Yearsofservice {get; set;} Method of calculating price public abstract Double CalcPrice (); }
2) Car Car category
Car public class car:vehicle {public car () {} Public car (string Name, String Licenseno, String Color, int yearsofservice, double dailyrent) : Base (Name, Licenseno, Color, Yearsofservice, dailyrent) { }< c8/>//public override Double CalcPrice () { double totalprice = 0 for calculated price; Double Baseicprice = this. Rentdate*this. dailyrent; if (this. Rentdate <=) { totalprice = Baseicprice; } else { Totalprice = Baseicprice + (this. RENTDATE-30) * this. Dailyrent * 0.1; } return totalprice; } }
3) Truck Truck class
Big Truck public class truck:vehicle { //Load Weight public Truck () {} public Truck (string Name, String Licenseno, string Color, int yearsofservice, double dailyrent,int Load) : Base (name,licenseno,color,yearsofservice , dailyrent) {this . Load = load; } public int Load {get; set;} Public override Double CalcPrice () to calculate price () { double totalprice = 0; Double Basicprice = rentdate * dailyrent; if (rentdate <=) { totalprice = Basicprice; } else { Totalprice = Basicprice + (RentDate-30) * (Dailyrent * 0.1) * Load; } return totalprice; } }
4) We do not know what kind of user will choose, so we have a factory class to help us do this thing. Vehiclefactory
Public class Vehiclefactory {public static Vehicle Readmagenner (String Name, String Licenseno, String Color, int yearsofservice, double dailyrent, string Type, int Load) { Vehicle Vehicle = null; Switch (Type) {case "Car": vehicle = new Car (name,licenseno,color,yearsofservice,dailyrent); break; Case "Truck": vehicle = new Truck (Name, Licenseno, Color, Yearsofservice, Dailyrent, Load); break; } return vehicle; } }
5) We are going to frmmain the main form
Public partial class Frmmain:form {public Frmmain () {InitializeComponent (); }//Save a collection of rental cars (vehicle name, vehicle object) dictionary<string, vehicle> notrent = new dictionary<string, vehicle> () ; Save a collection of rented vehicles. dictionary<string, vehicle> alreadyrent = new dictionary<string, vehicle> (); Constructed two cars, trucks in the Lind event call public void LoadData () {Car car = new Car ("Audi A8", "Jing R00544", "Black", 3, 2 40); Truck Truck = new Truck ("Dongfeng", "Jing A9988770", "Blue", 3, 300, 240); Notrent.add (truck. Licenseno, truck); Notrent.add (Car.licenseno, Car); Rental cars Car Rentcar = new Car ("Austrian A001", "Dongfeng", "Red", 3,200); Rentcar.rentuser = Tbpeople.text; Truck Renttruck = new Truck ("Jing B111", "BMW", "Red", 3,200,300); Alreadyrent.add (Rentcar.licenseno,rentcar); Alreadyrent.add (Renttruck.licenseno, Renttruck); } privatevoid Frmmain_load (object sender, EventArgs e) {//loads data into the collection LoadData (); Default My big truck load cannot use tbbigcar.enabled = false; } private void Btnew_click (object sender, EventArgs e) {//Refresh car Rental control's name Myrefre SH (notrent, listcar); }//Refresh the car rental button by calling the method public void Myrefresh (dictionary<string, vehicle> rnotrent, ListView lvshow) {//list (CAR) is emptied ListCar.Items.Clear (); foreach (Vehicle item in Rnotrent. Values) {//listview add value listviewitem lvitem = new ListViewItem (item. Licenseno); If (item is Car) {Lvitem. SubItems.Add (item. Name); Lvitem. SubItems.Add (item. Color); Lvitem. SubItems.Add (item. Yearsofservice.tostring ()); Lvitem. SubItems.Add (item. Dailyrent.tostring ()); } if(Item is Truck) {Lvitem. SubItems.Add (item. Name); Lvitem. SubItems.Add (item. Color); Lvitem. SubItems.Add (item. Yearsofservice.tostring ()); Lvitem. SubItems.Add (item. Dailyrent.tostring ()); Lvitem. SubItems.Add ((Truck) item). Load.tostring ()); } lvshow. Items.Add (Lvitem); }} private void Btcar_click (object sender, EventArgs e) {//Determine if the name of the lessor is written if (TB people.text== "") {MessageBox.Show ("Please enter the name of the lessor"); Return }//Remove vehicle A from the collection of rental vehicles A//add a to the rented vehicle collection if (listcar.selecteditems.count>0) { //?? Look at string number = Listcar.selecteditems[0]. Subitems[0]. Text; Vehicle Ve=notrent[number]; Notrent.remove (number); Myrefresh (Notrent,listcar); AlReadyrent.add (Number,ve); MessageBox.Show ("Car rental Success"); }} private void Btreturncar_click (object sender, EventArgs e) {//used to save the rented vehicle control name Myrefresh (Alreadyrent, Listreturn); private void Btselect_click (object sender, EventArgs e) {//To determine if (tbday.text== "") {MessageBox.Show ("Please enter your rental time"); Return }//Remove car A from a set of collections//add car A to the rental vehicle in string number = Listreturn.selecteditems[0]. Subitems[0]. Text; Vehicle Ve=alreadyrent[number]; Alreadyrent.remove (number); Myrefresh (Alreadyrent,listreturn); Notrent.add (Number,ve); Ve. Rentdate = Convert.ToInt32 (Tbday.text); Double money = 0; Money = ve. CalcPrice (); MessageBox.Show ("You need to pay" + Money + "Yuan"); private void Btput_click (object sender, EventArgs e) {string lincesno = Tbcarnum.text; String name = Tbtype.text; string color = Cbclor.text; int time = Convert.ToInt32 (Tbtime.text); Double dailyrent = Convert.ToInt32 (Tbdaymoney.text); if (rbsmallcar.checked) {Car car = new Car (lincesno, name, color, time, dailyrent); Notrent.add (Lincesno,car); } if (rbbigcar.checked) {int load = Convert.ToInt32 (Tbbigcar.text); Truck Truck = new Truck (lincesno, name, color, time, dailyrent, load); Notrent.add (Lincesno, truck); } MessageBox.Show ("Add success ... "); } private void Rbsmallcar_checkedchanged (object sender, EventArgs e) {tbbigcar.enabled = false; private void Rbbigcar_checkedchanged (object sender, EventArgs e) {tbbigcar.enabled = tr Ue } private void BtexiT_click (object sender, EventArgs e) {this. Close (); } }
This program is over, there may be flaws, I hope I can continue to progress.
Car rental System