Create a class named CarRental that contains fields that hold a renter’s name, z
ID: 3762697 • Letter: C
Question
Create a class named CarRental that contains fields that hold a renter’s name,
zip code, size of the car rented, daily rental fee, length of rental in days, and
total rental fee. The class contains a constructor that requires all the rental data
except the daily rate and total fee, which are calculated based on the size of the car:
economy at $29.99 per day, midsize at $38.99 per day, or full size at $43.50 per day.
The class also includes a display() method that displays all the rental data.
Create a subclass named LuxuryCarRental. This class sets the rental fee at $79.99
per day and prompts the user to respond to the option of including a chauffeur
at $200 more per day. Override the parent class display() method to include
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 total rental fee. Save the files as CarRental.java,
LuxuryCarRental.java, and UseCarRental.java.
Explanation / Answer
public class CarRental
{
private String name;
private CarSize size;
private int zip,rentalDays;
private double dailyFee,totalFee;
public CarRental(String name,int zip,CarSize size,int rentalDays)
{
this.name = name;
this.zip = zip;
this.size = size;
this.rentalDays = rentalDays;
setDailyFee(size);
}
public void display()
{
System.out.println("Customer name: "+name+" Zip: "+zip+" Carsize: "+size+" Rental Days: "+rentalDays+" Daily Fee: "+dailyFee+" Total Fee: "+totalFee);
}
public void setDailyFee(CarSize size)
{
switch(size)
{
case ECONOMY:
this.setDailyFee(29.99);
break;
case MIDSIZE:
this.setDailyFee(38.99);
break;
case FULLSIZE:
this.setDailyFee(43.50);
break;
case LUXURY:
this.setDailyFee(79.99);
break;
}
}
public String getName() {
return name;
}
public CarSize getSize() {
return size;
}
public int getZip() {
return zip;
}
public int getRentalDays() {
return rentalDays;
}
public double getDailyFee() {
return dailyFee;
}
public double getTotalFee() {
return totalFee;
}
public void setName(String name) {
this.name = name;
}
public void setSize(CarSize size) {
this.size = size;
}
public void setZip(int zip) {
this.zip = zip;
}
public void setRentalDays(int rentalDays) {
this.rentalDays = rentalDays;
}
public void setDailyFee(double dailyFee) {
this.dailyFee = dailyFee;
this.setTotalFee(this.getRentalDays()*this.getDailyFee());
}
public void setTotalFee(double totalFee) {
this.totalFee = totalFee;
}
}
enum CarSize
{
ECONOMY,MIDSIZE,FULLSIZE,LUXURY
}
//######################################################################################
public class LuxuryCarRental extends CarRental
{
boolean hasChauffer;
public LuxuryCarRental(String name, int zip, CarSize size, int rentalDays,boolean hasChauffer) {
super(name, zip, size, rentalDays);
this.hasChauffer = hasChauffer;
setDailyFee(CarSize.LUXURY,hasChauffer);
}
public void setDailyFee(CarSize size,boolean hasChauffer)
{
if(hasChauffer)
setDailyFee(2799.99);
else
setDailyFee(79.99);
}
public boolean getHasChauffer() {
return hasChauffer;
}
public void display()
{
super.display();
if(hasChauffer)
System.out.println("Chauffer fee: 200");
}
}
//###########################################################################
import java.util.*;
public class UserCarRental
{
public static void main(String[] args)
{
int option = -1;
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to CarRentals");
while(option < 1 || option > 4)
{
System.out.println("Select your car:");
System.out.println("1. Economy");
System.out.println("2. Mid Size");
System.out.println("3. Full Size");
System.out.println("4. Luxury");
System.out.print("Enter your option: ");
option = Integer.parseInt(sc.nextLine());
}
System.out.println("Enter your name: ");
String name = sc.nextLine();
System.out.println("Zip Code: ");
String zip = sc.nextLine();
System.out.println("No.of days: ");
String days = sc.nextLine();
CarRental rental = null;
switch(option)
{
case 1:
rental = new CarRental(name, Integer.parseInt(zip), CarSize.ECONOMY, Integer.parseInt(days));
break;
case 2:
rental = new CarRental(name, Integer.parseInt(zip), CarSize.MIDSIZE, Integer.parseInt(days));
break;
case 3:
rental = new CarRental(name, Integer.parseInt(zip), CarSize.FULLSIZE, Integer.parseInt(days));
break;
case 4:
System.out.print("You want a Chauffer(y/n): ");
String chauffer = sc.nextLine();
if(chauffer.charAt(0) == 'y' || chauffer.charAt(0) == 'Y')
rental = new LuxuryCarRental(name, Integer.parseInt(zip), CarSize.LUXURY, Integer.parseInt(days),true);
else
rental = new LuxuryCarRental(name, Integer.parseInt(zip), CarSize.LUXURY, Integer.parseInt(days),false);
break;
}
rental.display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.