Preface: Before writing this small system, we should guess what knowledge points we may need to use.
So for this small system: the main use of the following knowledge:
Encapsulation, Collections (ArrayList and Hashtable) and generics and non-generic collections (generics:list<> and non-generic:dictioanry<>),
Constructors, method overloads, polymorphism (inheritance, Abstraction: Abstract classes and abstract methods)
:
The first step.
Create several classes
Vehicle: Tool class ( parent class, superclass, base class ), this class is abstract class
There are method overloads, constructors
This class is primarily responsible for providing the properties of the car class
Property:
Public stringColor {Get;Set; } Public Doubledailyrent {Get;Set; } Public stringLicenseno {Get;Set; } Public stringName {Get;Set; } Public intrentdate {Get;Set; } Public stringRentuser {Get;Set; } Public intYearsofservice {Get;Set; }
02: Create a Car Class (car) (subclass)
Constructor, method overload.
03: Create a Truck Class (Truck) (subclass)
constructors, method overloads
Property:
public int Load
{
get {return load;}
set {load = value;}
}
04: Create a tool (Factory) class (Vehicleutil)
The second step.
Code implementation Ideas:
Vehicle: Tool, Parent class (*):
The code is as follows:
PublicVehicle () {} PublicVehicle (stringColorDoubleDailyrent,stringLicenseno,stringNameintYearsofservice) {Color=color; Dailyrent=dailyrent; Licenseno=Licenseno; Name=name; Yearsofservice=Yearsofservice; } Public Abstract DoubleCalcPrice ();
02: Automobile (CAR)
The code is as follows:
PublicCar () {} PublicCar (stringLicenseno,stringNamestringColorintYearsofservice,Doubledailyrent) {Color=color; Dailyrent=dailyrent; Licenseno=Licenseno; Name=name; Yearsofservice=Yearsofservice; } Public Override DoubleCalcPrice () {DoubleTotalprice =0; Totalprice= Rentdate *dailyrent; returnTotalprice; }
03: Truck Class (Truck)
The code is as follows:
PublicTruck (stringLicenseno,stringNamestringColorintYearsofservice,DoubleDailyrent,intload) {Color=color; Dailyrent=dailyrent; Licenseno=Licenseno; Name=name; Yearsofservice=Yearsofservice; Load=load; } Public Override DoubleCalcPrice () {DoubleTotalprice =0; Totalprice= Rentdate *dailyrent; returnTotalprice;}
04: Tool (Factory) class (Vehicleutil
The code is as follows:
Public StaticVehicle Createvehicle (stringLicenseno,stringNamestringColorintYearsofservice,DoubleDailyrent,intLoadstringtype) {Vehicle Vehicle=NULL; Switch(type) { Case "Car": Vehicle=NewCar (Licenseno, name, color, Yearsofservice, dailyrent); Break; Case "Truck": Vehicle=NewTruck (Licenseno, name, color, Yearsofservice, dailyrent, load); Break; default: Break; } returnvehicle; }
The third step.
Rental Event: (Btnrent_click (object sender, EventArgs e))
The key code is as follows:
if(Lvlist. Selecteditems.count = =0) {MessageBox.Show ("Please selectOne line"); return; } if(Txtuser.text = ="") {MessageBox.Show ("Please enter the customer name! "); return; } stringKey = Lvlist. selecteditems[0]. Text; Vehicle Vehicle=Vehicles[key]; Vehicles. Remove (key); Rentvehicles. ADD (key, vehicle); Printvehicles (vehicles, lvlist); MessageBox.Show ("Rental Success!! ");//printvehicles bind data to the ListView Public voidPrintvehicles (dictionary<string, vehicle>listprirnt, ListView lvinfo) {ListViewItem LV=NULL; Lvinfo. Items.clear (); foreach(Vehicle Iteminchlistprirnt. Values) {if(item isCar) {LV=NewListViewItem (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 isTruck) {LV=NewListViewItem (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); }
Fourth Step
and the car. Written in the Btngivemoney_click event
if(Txtdaynum.text = =string. Empty) {MessageBox.Show ("Please enter the number of days! "); return; } stringAddnum = lvlistgive.selecteditems[0]. Text; Vehicle vechile=Rentvehicles[addnum]; Rentvehicles. Remove (Addnum); Vechile. Rentdate=Convert.ToInt32 (Txtdaynum.text); DoubleMoney =0; //call the Calculate price method Money=vechile. CalcPrice (); MessageBox.Show ("you're going to be blessed ."+Money ); Vehicles. ADD (Addnum, vechile); //Refreshprintvehicles (Rentvehicles, lvlistgive); MessageBox.Show ("Success!! ");
Fifth step.
Add a vehicle to write in the Btnincar_click event
if(Txtweight.text = =""|| Txtusetime.text = =""|| Txttype.text = =""|| Txtid.text = =""|| Txtdayrentmoney.text = =""|| Cbocolor.text = ="") {MessageBox.Show ("please fill in the complete! "); } Else { stringLicon =Txtid.text; stringcolor =Cbocolor.text; DoubleMon =convert.todouble (Txtdayrentmoney.text); intTine =Convert.ToInt32 (Txtusetime.text); stringTy =Txttype.text; if(radjiaocar.checked) {Car CA=NewCar (licon, Ty, color, tine, mon); Vehicles. ADD (Licon, CA); } if(radkacar.checked) {intLoad =Convert.ToInt32 (Txtweight.text); Truck TR=NewTruck (licon, Ty, color, tine, Mon, load); Vehicles. ADD (Licon, TR); } MessageBox.Show ("Add success! "); }
Note: This article has no code comment, because you want to look at the code under any circumstances,
can also read the meaning of the code.
Depth. NET and C # small car rental system framework