Inheritance and polymorphism of car rental system

Source: Internet
Author: User
Tags car license plate number

1. Car Rental

Display all available cars in the system, select the car you want to rent, enter the rental person to take a taxi,

2. Also car

Select the car information in the list of cars, enter the rental days, calculate the rent,

3. New Car Storage

Need to enter the car license plate number, model, color, use time, and the daily rent, if the truck also to enter the truck load,

Specific implementation process:

1. Build the System

Create a class from class diagram, realize the relationship between Vehicle,trech and car three classes

Car class:

    

Namespace _09 Car Rental system
{
public class Car:vehicle
{
Public Car (String Licenseno, string name, string color, int yearsofservice, double dailyrent)
: Base (Licenseno, name, color, Yearsofservice, dailyrent)
{
;
}
public override double CalcPrice ()
{
Double totalprice = 0;
Double Basicprice = this. Rentdate * this. Dailyrent;
if (this. Rentdate <= 30)
{
Totalprice = Basicprice;
}
Else
{
Totalprice = Basicprice + (this. RENTDATE-30) * this. Dailyrent * 0.1;
}
return totalprice;
}
}
}

Vehicle class

    

Namespace _09 Car Rental system
{
Public abstract class Vehicle
{
Public Vehicle () {}
Public Vehicle (String licenseno,string name,string color,int yearofservice,double dailyrent)
{
This.licenseno = Licenseno;
THIS.name = name;
This.color = color;
This.yearofservice = Yearofservice;
This.dailyrent = dailyrent;
}
Rental Date
private int rentdate;

public int Rentdate
{
get {return rentdate;}
set {rentdate = value;}
}
Rental person
private string Rentuser;

public string Rentuser
{
get {return rentuser;}
set {Rentuser = value;}
}
Daily rent
Private double dailyrent;

Public double Dailyrent
{
get {return dailyrent;}
set {dailyrent = value;}
}
Use time
private int yearofservice;

public int Yearofservice
{
get {return yearofservice;}
set {Yearofservice = value;}
}
Color
private string color;

public string Color
{
get {return color;}
set {color = value;}
}
Car name
private string name;

public string Name
{
get {return name;}
set {name = value;}
}
Car Grade
private string Licenseno;

public string Licenseno
{
get {return Licenseno;}
set {Licenseno = value;}
}
How to calculate the price
public abstract double CalcPrice ();
}
}

Trech class:

    

Namespace _09 Car Rental system
{
public class Truck:vehicle
{
Public Truck () {}
Public Truck (String Licenseno, string name, string color, int yearsofservice, double dailyrent, int load)
: Base (Licenseno, name, color, Yearsofservice, dailyrent)
{
This. Load = load;
}
Load weight
private int load;

public int Load
{
get {return load;}
set {load = value;}
}
Calculation method of truck cost
30 days or less (inclusive of 30) based on daily rent
More than 30 days of excess: daily, per ton (load weight) Increase day rent 10%
public override double CalcPrice ()
{
Double totalprice = 0;
Double Basicprice = rentdate * dailyrent;
if (rentdate<=30)
{
Totalprice = Basicprice;
}
Else
{
Totalprice = Basicprice + (RentDate-30) * (Dailyrent * 0.1) *load;
}
return totalprice;
}
}
}

Save a collection of rented cars (vehicle name, vehicle object)
Dictionary<string, vehicle> notrent;
Save a collection of rented vehicles.
Dictionary<string, vehicle> alreadyrent;

2. Realize Car Rental

  

if (txtrenter.text== "")
{
MessageBox.Show ("Please enter the name of the lessor");
Return
}
Removal of vehicle A from a collection of rental vehicles
Add a to the rented vehicle collection
if (lvrent.selecteditems.count>0)
{
String number = Lvrent.selecteditems[0]. Text;
Vehicle ve = Notrent[number];
Notrent.remove (number);
Myrefresh (notrent,lvrent);
Alreadyrent.add (number, ve);
MessageBox.Show ("Car Rental success! ");

3. Realize the car

  

if (txtrentdate.text== "")

{
MessageBox.Show ("Please enter your rental time");
Return
}
01. Remove the//02 from the rented collection and add the car A to the rental vehicle
String Number=lvreturn.selecteditems[0]. Text;
Vehicle ve = Alreadyrent[number];
Alreadyrent.remove (number);
Myrefresh (Alreadyrent, Lvreturn);
Notrent.add (number, ve);
Ve. Rentdate = Convert.ToInt32 (Txtrentdate.text);
Double money=0;
Money = ve. CalcPrice ();
MessageBox.Show ("You need to pay" +money+ ");

4. Realization of new car storage

  

  

String Lincesn0=txtautonum.text;
String Name=txtname.text;
String Color=cobcolor.text;
int Time=convert.toint32 (txtyears.text);
Double Dailyrent=convert.toint32 (Txtletting.text);

if (rdocar.checked)
{

Car car = new car (lincesN0, name, color, time, dailyrent);
Notrent.add (lincesN0, car);
}
if (rdotruck.checked)
{
int load = Convert.ToInt32 (Txtload.text);
Truck Truck = new Truck (lincesN0, name, color, time, dailyrent, load);
Notrent.add (lincesN0, truck);
}
MessageBox.Show ("added successfully! ");

Inheritance and polymorphism of car rental system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.