Create a class named CarRental that contains fields that hol
Create a class named CarRental that contains fields that hold a renter\'s name, zip code, size of the car rented, daily rental rate, length of rental (in days), and total cost. The class contains a constructor that requires all the rental data user information (name, zip, size, length). The daily rental rate depends on the size of the car:
economy: $29.99 per day
midsize: $38.99 per day
fullsize: $43.50 per day
Calculate the total cost for the rental car and include a display( ) method to display all the rental information.
Create a subclass named LuxuryCarRental. This class sets the daily rental rate to $79.99 per day and prompts the user to respond to the option of including a chauffeur at and additional $200 per day. Override the parent class display( ) method to include the chauffeur fee information.
Write an application named UseCarRental that prompts the user for the data needed for a rental and creates an object of the correct type. Display the rental information with the total cost for the car.
Solution
public class CarRental {
protected String name, zip, size;
protected double rate, total;
protected int days;
public CarRental(String name, String zip, String size, int length) {
super();
this.name = name;
this.zip = zip;
this.size = size;
this.days = length;
setRate();
}
protected void setRate(){
if(size.equals(\"economy\"))
rate = 29.99;
if(size.equals(\"midsize\"))
rate = 38.99;
if(size.equals(\"fullsize\"))
rate = 43.50;
}
public double getTotal(){
return rate*days;
}
public void display(){
System.out.println(\"Total rent = $\"+getTotal());
}
}
//------------------------------------------------------------------------------
import java.util.Scanner;
public class LuxuryCarRental extends CarRental{
double cf=0;
public LuxuryCarRental(String name, String zip, String size, int length) {
super(name, zip, size, length);
rate = 79.99;
//getChauffer();
// TODO Auto-generated constructor stub
}
public void getChauffer(){
System.out.println(\"Do you want a chauffer ? (Y /N)\");
Scanner sc=new Scanner(System.in);
String ch=\"N\";
ch=sc.next();
if(ch.equals(\"Y\")){
cf = 200.00;
}
sc.close();
}
public void display(){
System.out.println(\"Chauffer fee = $\"+days*cf);
super.display();
}
}
//--------------------------------------------------------------------------------------------
import java.util.Scanner;
public class UseCarRental {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
CarRental c;
String name, zip, size;
int days=0;
System.out.println(\"Enter your name:\");
name = sc.nextLine();
System.out.println(\"Enter your zip:\");
zip = sc.nextLine();
System.out.println(\"Enter the car size(economy,\"
+ \"midsize,fullsize or luxury): \");
size=sc.nextLine();
System.out.println(\"Enter the number of days:\");
days=sc.nextInt();
if(size.equals(\"luxury\")){
c = new LuxuryCarRental(name, zip, size, days);
((LuxuryCarRental) c).getChauffer();
}
else
c = new CarRental(name, zip, size, days);
c.display();
}
}
//---------------------------------------------------------------------------
Output:
Enter your name:
ABC
Enter your zip:
700032
Enter the car size(economy,midsize,fullsize or luxury):
economy
Enter the number of days:
12
Total rent = $359.88
Enter your name:
ABC
Enter your zip:
713212
Enter the car size(economy,midsize,fullsize or luxury):
luxury
Enter the number of days:
2
Do you want a chauffer ? (Y /N)
Y
Chauffer fee = $400.0
Total rent = $159.98


