Go deep into the small car rental system framework of. NET and C,
Before writing this small system, we should guess what knowledge points might be used.
For this small system, we mainly use the following knowledge:
Encapsulation, set (ArrayList and HashTable), generic and non-generic set (generic: List <> and non-generic: Dictioanry <> ),
Constructor, method overload, polymorphism (inheritance, Abstraction: abstract class and abstract method)
:
Step 1.
Create several classes
01: tool class (vehicle) (parent class, super class, base class), this class is an abstract class
Method overload and constructor
This class is mainly responsible for providing vehicle attributes
Attribute:
public string Color { get; set; } public double DailyRent { get; set; } public string LicenseNO { get; set; } public string Name { get; set; } public int RentDate { get; set; } public string RentUser { get; set; } public int YearsOfService { get; set; }
02: Create a Car (subclass)
Constructor, method overload.
03: Create a Truck class (Truck) (subclass)
Constructor, method overload
Attribute:
Public int Load
{
Get {return load ;}
Set {load = value ;}
}
04: create a tool (factory) Class (VehicleUtil)
Step 2.
Code Implementation ideas:
01: tool, parent class (vehicle ):
The Code is as follows:
public Vehicle() { } public Vehicle(string color, double dailyrent, string licenseno, string name, int yearsofservice) { Color = color; DailyRent = dailyrent; LicenseNO = licenseno; Name = name; YearsOfService = yearsofservice; } public abstract double CalcPrice();
02: Car)
The Code is as follows:
public Car() { } public Car(string licenseno, string name, string color, int yearsofservice, double dailyrent) { Color = color; DailyRent = dailyrent; LicenseNO = licenseno; Name = name; YearsOfService = yearsofservice; } public override double CalcPrice() { double totalPrice = 0; totalPrice = RentDate * DailyRent; return totalPrice; }
03: trucks (Truck)
The Code is as follows:
public Truck(string licenseno, string name, string color, int yearsofservice, double dailyrent, int load) { Color = color; DailyRent = dailyrent; LicenseNO = licenseno; Name = name; YearsOfService = yearsofservice; Load = load; } public override double CalcPrice() { double totalPrice = 0; totalPrice = RentDate * DailyRent; return totalPrice;}
04: tool (factory) Class (VehicleUtil
The Code is as follows:
public static Vehicle CreateVehicle(string licenseno, string name, string color, int yearsofservice, double dailyrent, int load, string type) { Vehicle vehicle = null; switch (type) { case "Car": vehicle = new Car(licenseno, name, color, yearsofservice, dailyrent); break; case "Truck": vehicle = new Truck(licenseno, name, color, yearsofservice, dailyrent, load); break; default: break; } return vehicle; }
Step 3.
Rental event: (btn1__click (object sender, EventArgs e ))
The key code is as follows:
If (lvlist. selectedItems. count = 0) {MessageBox. show ("select a row"); return;} if (txtUser. text = "") {MessageBox. show ("Enter the customer name! "); Return;} string key = lvlist. selectedItems [0]. text; Vehicle vehicle = vehicles [key]; vehicles. remove (key); incluvehicles. add (key, vehicle); PrintVehicles (vehicles, lvlist); MessageBox. show ("rental successful !! "); // PrintVehicles bind data to the public void PrintVehicles (Dictionary <string, Vehicle> listprirnt, listview lvinfo) {ListViewItem lv = null; lvinfo. items. clear (); foreach (Vehicle item in listprirnt. values) {if (item is Car) {lv = new ListViewItem (item. licenseNO); lv. subItems. add (item. name); lv. subItems. add (item. color. toString (); lv. subItems. add (item. yearsOfService. toString (); lv. subItems. add (item. dailyRent. toString (); lvinfo. items. add (lv);} else if (item is Truck) {lv = new ListViewItem (item. licenseNO); lv. subItems. add (item. name); lv. subItems. add (item. color. toString (); lv. subItems. add (item. yearsOfService. toString (); lv. subItems. add (item. dailyRent. toString (); lv. subItems. add (Truck) item ). load. toString (); lvinfo. items. add (lv );}
Step 4
Return the vehicle. In the btnGiveMoney_Click event, write
If (txtDayNum. Text = string. Empty) {MessageBox. Show ("Enter the number of days! "); Return;} string addNum = lvlistGive. selectedItems [0]. text; Vehicle vechile = incluvehicles [addNum]; incluvehicles. remove (addNum); vechile. required date = Convert. toInt32 (txtDayNum. text); double money = 0; // call the calculation price method money = vechile. calcPrice (); MessageBox. show ("You want to benefit le" + money); vehicles. add (addNum, vechile); // refresh PrintVehicles (incluvehicles, lvlistGive); MessageBox. show ("Successful !! ");
Step 5.
Add a vehicle in btnIncAR_Click event to write
If (txtWeight. text = "" | txtUseTime. text = "" | txtType. text = "" | txtID. text = "" | txtday1_money. text = "" | cboColor. text = "") {MessageBox. show ("Please complete it! ");} Else {string licon = txtID. text; string color = cboColor. text; double mon = Convert. toDouble (txtday1_money. text); int tine = Convert. toInt32 (txtUseTime. text); string ty = txtType. text; if (radJiaoCar. checked) {Car ca = new Car (licon, ty, color, tine, mon); vehicles. add (licon, ca);} if (radKaCar. checked) {int load = Convert. toInt32 (txtWeight. text); Truck tr = new Truck (licon, ty, color, Tine, mon, load); vehicles. Add (licon, tr);} MessageBox. Show ("added successfully! ");}
Note: This article does not contain code comments because you want to view the code under any circumstances,
You can also understand the meaning of the Code.