From Previous Chapter my Rental and Rental Demo class public class Rental { publ
ID: 3713852 • Letter: F
Question
From Previous Chapter my Rental and Rental Demo class
public class Rental
{
public static final int MINUTES_PER_HOUR = 60;
public static final double HOURLY_RATE = 40;
private String contractNumber;
private String contactPhoneNumber;
private int hours;
private int minsOver;
private double cost;
public Rental(String num, int minutes)
{
setContractNumber(num);
setHoursAndMinutes(minutes);
}
public Rental()
{
this("A000", 0);
}
public void setContractNumber(String num)
{
if(num.matches("[a-zA-Z][0-9]{3}"))
{
num=num.substring(0,1).toUpperCase()+num.substring(1);
contractNumber = num;
}
else
{
setContractNumber("A000");
}
}
public void setHoursAndMinutes(int minutes)
{
hours = minutes / MINUTES_PER_HOUR;
minsOver = minutes % MINUTES_PER_HOUR;
if(minsOver <= HOURLY_RATE)
cost = hours * HOURLY_RATE + minsOver;
else
cost = hours * HOURLY_RATE + HOURLY_RATE;
}
public String getContractNumber()
{
return contractNumber;
}
public int getHours()
{
return hours;
}
public int getMinsOver()
{
return minsOver;
}
public double getCost()
{
return cost;
}
public String getContactPhoneNumber()
{
if(contactPhoneNumber !=null && !contactPhoneNumber.isEmpty())
return " (" + contactPhoneNumber.substring(0, 3) + ") " + contactPhoneNumber.substring(3, 6) + " - " + contactPhoneNumber.substring(6);
return "";
}
public void setContactPhoneNumber(String contactPhoneNumber)
{
contactPhoneNumber= contactPhoneNumber.replaceAll("[^a-zA-Z0-9]", "");
if(contactPhoneNumber.length()<10)
{
setContactPhoneNumber("0000000000");
}
else
{
this.contactPhoneNumber = contactPhoneNumber;
}
}
}
*****************************************************************************************
import java.util.Scanner;
public class RentalDemo
{
public static void main(String[] args)
{
String contractNum;
String contactPhoneNumber;
int minutes;
contractNum = getContractNumber();
contactPhoneNumber = getContactPhoneNumber();
minutes = getMinutes();
Rental r1 = new Rental(contractNum, minutes);
contractNum = getContractNumber();
minutes = getMinutes();
r1.setContactPhoneNumber(contactPhoneNumber);
Rental r2 = new Rental(contractNum, minutes);
contractNum = getContractNumber();
minutes = getMinutes();
r2.setContactPhoneNumber(contactPhoneNumber);
Rental r3 = new Rental(contractNum, minutes);
contractNum = getContractNumber();
minutes = getMinutes();
r3.setContactPhoneNumber(contactPhoneNumber);
displayDetails(r1);
displayDetails(r2);
displayDetails(r3);
System.out.println("");
System.out.println("For Contract #" + r1.getContractNumber() + " with a time of " + r1.getHours() + " hours and " + r1.getMinsOver() + " minutes, and Contract #" +
r2.getContractNumber() + " with a time of " + r2.getHours() + " hours and " + r2.getMinsOver() + " minutes, the one with the longer time is Contract #" + getLongerRental(r1, r2).getContractNumber());
System.out.println("");
System.out.println("For Contract #" + r1.getContractNumber() + " with a time of " + r1.getHours() + " hours and " + r1.getMinsOver() + " minutes, and Contract #" + r3.getContractNumber() + " with a time of " + r3.getHours() + " hours and " + r3.getMinsOver() + " minutes, the one with the longer time is Contract #" + getLongerRental(r1, r3).getContractNumber());
System.out.println("");
System.out.println("For Contract #" + r2.getContractNumber() + " with a time of " + r2.getHours() + " hours and " + r2.getMinsOver() + " minutes, and Contract #" + r3.getContractNumber() + " with a time of " + r3.getHours() + " hours and " + r3.getMinsOver() + " minutes, the one with the longer time is Contract #" + getLongerRental(r2, r3).getContractNumber());
}
public static String getContractNumber()
{
String num;
Scanner input = new Scanner(System.in);
System.out.print("Enter contract number: ");
num = input.nextLine();
return num;
}
public static String getContactPhoneNumber()
{
String num;
Scanner input = new Scanner(System.in);
System.out.print("Enter contact Phone number: ");
num = input.nextLine();
return num;
}
public static int getMinutes()
{
int minutes;
final int LOW_MIN = 60;
final int HIGH_MIN = 7200;
Scanner input = new Scanner(System.in);
System.out.print("Enter minutes: ");
minutes = input.nextInt();
while(minutes < LOW_MIN || minutes > HIGH_MIN)
{
System.out.println("Time must be between " + LOW_MIN + " minutes and " + HIGH_MIN + " minutes");
System.out.print("Please reenter minutes: ");
minutes = input.nextInt();
}
return minutes;
}
public static void displayDetails(Rental r)
{
System.out.println(" Contract #" + r.getContractNumber());
System.out.println(" Contact Phone Number: "+r.getContactPhoneNumber());
System.out.println("For a rental of " + r.getHours() + " hours and " + r.getMinsOver() + " minutes, at a rate of $"+ r.HOURLY_RATE + " the cost is $" + r.getCost());
}
public static Rental getLongerRental(Rental r1, Rental r2)
{
Rental longer = new Rental();
int minutes1;
int minutes2;
final double COUPON=0.1;
minutes1 = r1.getHours() * Rental.MINUTES_PER_HOUR + r1.getMinsOver();
minutes2 = r2.getHours() * Rental.MINUTES_PER_HOUR + r2.getMinsOver();
if(minutes1 >= minutes2)
{
longer = r1;
}
else
{
longer = r2;
return longer;
}
if (minutes1 >= minutes2)
{
System.out.println("Coupon good for 10 percent off next rental");
}
else
{
System.out.println("Coupon good for 10 percent off next rental");
}
return longer;
}
}
Explanation / Answer
All parts of the question have been programmed, follow the code sequentially I have added comments where there is addition or deletion of code. If there is any confusion please comment below. If you find this answer helpful give a thumbs up. Thanks.
public class Rental
{
public static final int MINUTES_PER_HOUR = 60;
public static final double HOURLY_RATE = 40;
private String contractNumber;
private String contactPhoneNumber;
private int hours;
private int minsOver;
private double cost;
private int type; // declared type as integer
// declared equipments final String array which has list of the all rental equipments available
public static final String[] equipments = {"Jet Ski", "Pontoon Boat", "Rowboat", "Canoe", "Kayak", "Beach Chair", "Umbrella", "Other"};
public Rental(String num, int minutes)
{
setContractNumber(num);
setHoursAndMinutes(minutes);
}
public Rental()
{
this("A000", 0, 8);
}
public void setContractNumber(String num)
{
if(num.matches("[a-zA-Z][0-9]{3}"))
{
num=num.substring(0,1).toUpperCase()+num.substring(1);
contractNumber = num;
}
else
{
setContractNumber("A000");
}
}
public void setHoursAndMinutes(int minutes)
{
hours = minutes / MINUTES_PER_HOUR;
minsOver = minutes % MINUTES_PER_HOUR;
if(minsOver <= HOURLY_RATE)
cost = hours * HOURLY_RATE + minsOver;
else
cost = hours * HOURLY_RATE + HOURLY_RATE;
}
public String getContractNumber()
{
return contractNumber;
}
public int getHours()
{
return hours;
}
public int getMinsOver()
{
return minsOver;
}
public double getCost()
{
return cost;
}
// Added this function getEquipmentType() to get the type of equipment number from the object
public int getEquipmentType()
{
return type;
}
// Added this function getEquipmentName(int) access the equipment name based on the number supplied
public String getEquipmentName(int n) {
if(n < 8) // checks if the entered number is with the range of number of equipments available else returns "Other"
{
return equipments[n];
} else
{
return equipments[8];
}
}
public String getContactPhoneNumber()
{
if(contactPhoneNumber !=null && !contactPhoneNumber.isEmpty())
return " (" + contactPhoneNumber.substring(0, 3) + ") " + contactPhoneNumber.substring(3, 6) + " - " + contactPhoneNumber.substring(6);
return "";
}
public void setContactPhoneNumber(String contactPhoneNumber)
{
contactPhoneNumber= contactPhoneNumber.replaceAll("[^a-zA-Z0-9]", "");
if(contactPhoneNumber.length()<10)
{
setContactPhoneNumber("0000000000");
}
else
{
this.contactPhoneNumber = contactPhoneNumber;
}
}
// Added this function setEquipmentType(int) to set the type of equipment number of the object
public void setEquipmentType(int n)
{
if(n < 8)
{
type = n;
} else
{
type = 8
}
}
}
import java.util.Scanner;
public class RentalDemo
{
public static void main(String[] args)
{
String contractNum;
String contactPhoneNumber;
int minutes;
int type;
Rental [] r = new Rental[3]; // This creates an array of 3 null objects
for (int i = 0; i < 3; i++)
{
contractNum = getContractNumber(); // to get the contract number
contactPhoneNumber = getContactPhoneNumber(); // to get the contact phone number
minutes = getMinutes(); // to get the number of minutes to rent
type = getEquipmentType(); // to get the type of equipment rented
Rental r[i] = new Rental(contractNum, minutes); // this call will populate the objects with our values supplied in the constructor
r[i].setContactPhoneNumber(contactPhoneNumber); // sets the contact phone number
r[i].setEquipmentType(type)); // sets the equipment type
displayDetails(r[i]);
}
// Removed all the statements that print the coupons on the basis of rental timings
}
public static String getContractNumber()
{
String num;
Scanner input = new Scanner(System.in);
System.out.print("Enter contract number: ");
num = input.nextLine();
return num;
}
public static String getContactPhoneNumber()
{
String num;
Scanner input = new Scanner(System.in);
System.out.print("Enter contact Phone number: ");
num = input.nextLine();
return num;
}
// Added Function getEquipmentType() for allowing user to enter the equipment type number
public static int getEquipmentType() {
int t;
Scanner input = new Scanner(System.in);
System.out.print("Enter equipment number: ");
t = input.nextInt();
return t;
}
public static int getMinutes()
{
int minutes;
final int LOW_MIN = 60;
final int HIGH_MIN = 7200;
Scanner input = new Scanner(System.in);
System.out.print("Enter minutes: ");
minutes = input.nextInt();
while(minutes < LOW_MIN || minutes > HIGH_MIN)
{
System.out.println("Time must be between " + LOW_MIN + " minutes and " + HIGH_MIN + " minutes");
System.out.print("Please reenter minutes: ");
minutes = input.nextInt();
}
return minutes;
}
public static void displayDetails(Rental r)
{
System.out.println(" Contract #" + r.getContractNumber());
System.out.println(" Contact Phone Number: "+r.getContactPhoneNumber());
System.out.println(" The Equipment used is: "+r.getEquimpentName(r.getEquipmentType())); // this line prints the equipment to be rented
System.out.println(" For a rental of " + r.getHours() + " hours and " + r.getMinsOver() + " minutes, at a rate of $"+ r.HOURLY_RATE + " the cost is $" + r.getCost());
}
// Removed the function getLongerRental(Rental, Rental) because it contained all the unnecessary statements for coupons calculation
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.