My code works just fine, but how do I display to the summary information using a
ID: 3656529 • Letter: M
Question
My code works just fine, but how do I display to the summary information using a toString() method? Here is what I have: package userentalcar; import java.util.Scanner; import java.text.DecimalFormat; import java.text.NumberFormat; public class UseRentalCar { public static void main(String[] args){ System.out.println("Enter car type of car 1: " + "(1) for Compact " + "(2) for Mid-sized " + "(3) for Full size "); Scanner my_input = new Scanner(System.in); int cartype = my_input.nextInt(); System.out.println("How many days do you need this rental?"); int numDays = my_input.nextInt(); RentalCar car1 = new RentalCar(cartype,numDays); System.out.println("Enter car type of car 2: " + "(1) for Compact " + "(2) for Mid-sized " + "(3) for Full size "); cartype = my_input.nextInt(); System.out.println("How many days do you need this rental?"); numDays = my_input.nextInt(); RentalCar car2 = new RentalCar(cartype,numDays); System.out.println("Enter car type of car 3: " + "(1) for Compact " + "(2) for Mid-sized " + "(3) for Full size "); cartype = my_input.nextInt(); System.out.println("How many days do you need this rental?"); numDays = my_input.nextInt(); RentalCar car3 = new RentalCar(cartype,numDays); System.out.println("Enter car type of car 4: " + "(1) for Compact " + "(2) for Mid-sized " + "(3) for Full size "); cartype = my_input.nextInt(); System.out.println("How many days do you need this rental?"); numDays = my_input.nextInt(); Luxury car4 = new Luxury(cartype,numDays); NumberFormat formatter = new DecimalFormat("#0.00"); System.out.println("********** SUMMARY **********"); System.out.println("Note: Car Types: 1 = Compact, 2 = Mid-sized, 3 = Full size "); System.out.println(" Rental 1: Car Type: "+car1.getCarType()+" Days: " +car1.getNumDays()+" Price: $"+formatter.format(car1.getRentalRate())+" "); System.out.println(" Rental 2: Car Type: "+car2.getCarType()+" Days: " +car2.getNumDays()+" Price: $"+formatter.format(car2.getRentalRate())+" "); System.out.println(" Rental 3: Car Type: "+car3.getCarType()+" Days: " +car3.getNumDays()+" Price: $"+formatter.format(car3.getRentalRate())+" "); System.out.println(" Rental 4: Car Type: "+car4.getCarType()+" Days: " +car4.getNumDays()+" Price: $"+formatter.format(car4.getRentalRate())+" Surcharge: $" +formatter.format(car4.getSurcharge())+" "); double rentalTotal; rentalTotal = car1.getRentalRate()+car2.getRentalRate()+car3.getRentalRate() +car4.getRentalRate()+car4.getSurcharge(); System.out.println("-------- Your total is: $"+formatter.format(rentalTotal)); } } ________ package userentalcar; public class RentalCar{ int carType; double rentalRate; int numDays; int getNumDays(){ return numDays; } int getCarType(){ return carType; } double getRentalRate(){ return rentalRate; } public RentalCar(int carType,int numDays){ this.numDays = numDays; this.carType = carType; if(carType == 1){ this.rentalRate = 69.95 * numDays; } else if(carType == 2){ this.rentalRate = 89.95 * numDays; } else{ this.rentalRate = 99.99 * numDays; } } } class Luxury extends RentalCar{ double Surcharge; double getSurcharge(){ return this.Surcharge; } public Luxury(int carType,int numDays){ super(carType,numDays); this.Surcharge = 40.00; } }Explanation / Answer
please rate - thanks
import java.util.Scanner; import java.text.DecimalFormat; import java.text.NumberFormat;
public class UseRentalCar
{ public static void main(String[] args)
{ System.out.println("Enter car type of car 1: " + "(1) for Compact " + "(2) for Mid-sized " + "(3) for Full size ");
Scanner my_input = new Scanner(System.in); int cartype = my_input.nextInt();
System.out.println("How many days do you need this rental?"); int numDays = my_input.nextInt();
RentalCar car1 = new RentalCar(cartype,numDays);
System.out.println("Enter car type of car 2: " + "(1) for Compact " + "(2) for Mid-sized " + "(3) for Full size ");
cartype = my_input.nextInt(); System.out.println("How many days do you need this rental?");
numDays = my_input.nextInt(); RentalCar car2 = new RentalCar(cartype,numDays);
System.out.println("Enter car type of car 3: " + "(1) for Compact " + "(2) for Mid-sized " + "(3) for Full size ");
cartype = my_input.nextInt(); System.out.println("How many days do you need this rental?");
numDays = my_input.nextInt(); RentalCar car3 = new RentalCar(cartype,numDays);
System.out.println("Enter car type of car 4: " + "(1) for Compact " + "(2) for Mid-sized " + "(3) for Full size ");
cartype = my_input.nextInt(); System.out.println("How many days do you need this rental?");
numDays = my_input.nextInt(); Luxury car4 = new Luxury(cartype,numDays);
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println("********** SUMMARY **********");
System.out.println("Note: Car Types: 1 = Compact, 2 = Mid-sized, 3 = Full size ");
System.out.println(" Rental 1:"+car1.toString());
System.out.println(" Rental 2:"+car2.toString());
System.out.println(" Rental 3:"+car3.toString());
System.out.println(" Rental 4:"+car4.toString());
double rentalTotal;
rentalTotal = car1.getRentalRate()+car2.getRentalRate()+car3.getRentalRate() +car4.getRentalRate()+car4.getSurcharge();
System.out.println("-------- Your total is: $"+formatter.format(rentalTotal)); } }
----------------------------------------
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class RentalCar{ int carType; double rentalRate; int numDays;
int getNumDays(){ return numDays; }
int getCarType(){ return carType; }
double getRentalRate(){ return rentalRate; }
public String toString()
{NumberFormat formatter = new DecimalFormat("#0.00");
return " Car Type: "+carType+" Days: " +numDays+" Price: $"+formatter.format(rentalRate)+" ";
}
public RentalCar(int carType,int numDays)
{ this.numDays = numDays; this.carType = carType;
if(carType == 1){ this.rentalRate = 69.95 * numDays; }
else if(carType == 2){ this.rentalRate = 89.95 * numDays; }
else{ this.rentalRate = 99.99 * numDays; } } }
class Luxury extends RentalCar{ double Surcharge; double getSurcharge()
{ return this.Surcharge; }
public Luxury(int carType,int numDays)
{ super(carType,numDays); this.Surcharge = 40.00; }
public String toString()
{NumberFormat formatter = new DecimalFormat("#0.00");
return super.toString()+ "Surcharge: $"+formatter.format(Surcharge)+" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.