In the last chapter, you modified the RentalDemo program for Sammy’s Seashore Su
ID: 3739389 • Letter: I
Question
In the last chapter, you modified the RentalDemo program for Sammy’s Seashore Supplies to accept and display data for an array of three Rental objects. Now, modify the program to use an array of eight Rental objects. Prompt the user to choose an option to sort Rentals in ascending order by contract number, price, or equipment type. Display the sorted list, and continue to prompt the user for sorting options until the user enters a sentinel value. Save the file as RentalDemo.
java. import java.util.Scanner; class Rental { public static final int MINUTES_IN_HOUR = 60; public static final double HOUR_RATE = 40.00; private String contractNumber; private int hours; private int extraMinutes; private double price; private String contactPhoneNumber; private int equipment; private static final String[] equipments = {"jet ski", "pontoon boat", "rowboat", "canoe", "kayak", "beach chair", "umbrella", "other"}; public Rental(String num, int minutes, String phone, int equipment) { setContractNumber(num); setHoursAndMinutes(minutes); setContactPhoneNumber(phone); setEquipment(equipment); } public Rental() { this("A000", 0, "0000000000", 7); } public void setContractNumber(String num) { if(num.length()!=4 || !num.substring(1).matches("[0-9]+") || !num.substring(0,1).matches("[a-zA-Z]+")) num = "A000"; if(!num.substring(0,1).matches("[A-Z]+")) num = num.toUpperCase(); contractNumber = num; } public void setContactPhoneNumber(String number){ number = number.replaceAll("[^0-9]", ""); contactPhoneNumber = number; if(number.length()!=10) contactPhoneNumber = "0000000000"; } public void setHoursAndMinutes(int minutes) { hours = minutes / MINUTES_IN_HOUR; extraMinutes = minutes % MINUTES_IN_HOUR; if(extraMinutes <= HOUR_RATE) price = hours * HOUR_RATE + extraMinutes; else price = hours * HOUR_RATE + HOUR_RATE; } public String getContractNumber() { return contractNumber; } public int getHours() { return hours; } public int getExtraMinutes() { return extraMinutes; } public String getContactPhoneNumber() { StringBuilder sb = new StringBuilder(contactPhoneNumber); sb.insert(0,'('); sb.insert(4,')'); sb.insert(8,'-'); return sb.toString(); } public double getPrice() { return price; } public String getEquipment() { return equipments[equipment]; } public void setEquipment(int equipment) { if(equipment > 7) this.equipment = 7; else this.equipment = equipment; } }
import java.util.Scanner; public class RentalDemo { public static void main(String[] args) { Rental rentals[] = new Rental[3]; for(int i=0; i<3; i++) { System.out.println(" Enter details of Rental " + (i+1)); System.out.println("--------------------------------------------------- "); rentals[i] = new Rental(getContractNumber(), getMinutes(), getContactPhoneNumber(), getEquipment()); } for(int i=0; i<3; i++) { System.out.println(" Details of Rental " + (i+1)); System.out.println("--------------------------------------------------- "); displayDetails(rentals[i]); } } 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 phoneNumber; Scanner input = new Scanner(System.in); System.out.print("Enter contact phone number >> "); phoneNumber = input.nextLine(); return phoneNumber; } public static int getMinutes() { int minutes; Scanner input = new Scanner(System.in); System.out.print("Enter minutes >> "); minutes = input.nextInt(); return minutes; } public static int getEquipment() { int equipment; Scanner input = new Scanner(System.in); System.out.print("Enter equipment >>"); equipment = input.nextInt(); return equipment; } public static void displayDetails(Rental r) { System.out.println(" Contract #" + r.getContractNumber()); System.out.print("For a rental of " + r.getHours() + " hours and " + r.getExtraMinutes()); System.out.printf(" minutes, at a rate of $%.2f",r.HOUR_RATE); System.out.printf(" the price is $%.2f ",r.getPrice()); System.out.println("Contact phone Number is :"+r.getContactPhoneNumber()); System.out.println("The equipment you rented is :"+r.getEquipment()); } public static Rental getLongerRental(Rental r1, Rental r2) { Rental longer = new Rental(); int minutes1; int minutes2; minutes1 = r1.getHours() * Rental.MINUTES_IN_HOUR + r1.getExtraMinutes(); minutes2 = r2.getHours() * Rental.MINUTES_IN_HOUR + r2.getExtraMinutes(); if(minutes1 >= minutes2) longer = r1; else longer = r2; return longer; } public static void displayCoupon(Rental r1) { int loopcount = r1.getHours(); for (int i = 0; i < loopcount; i++){ System.out.println("Coupon good for 10 percent off next rental with your rental of contract number " + r1.getContractNumber() + "."); } } }
Explanation / Answer
Below is your code.
I have changed only RentalDemo class as per your question. I have given output as well at the end for your reference.
RentalDemo.java
public class RentalDemo {
public static void main(String[] args) {
//Rental rentals[] = new Rental[3];
//Array of 8 rental objects
Rental rentals[] = new Rental[8];
//getting details of 8 rentals
for (int i = 0; i < 8; i++) {
System.out.println(" Enter details of Rental " + (i + 1));
System.out.println("--------------------------------------------------- ");
rentals[i] = new Rental(getContractNumber(), getMinutes(), getContactPhoneNumber(), getEquipment());
}
/*
for (int i = 0; i < 3; i++) {
System.out.println(" Details of Rental " + (i + 1));
System.out.println("--------------------------------------------------- ");
displayDetails(rentals[i]);
}*/
int choice = 0;
Scanner scan = new Scanner(System.in);
while(choice != 4) {
showMenu();
choice = Integer.parseInt(scan.next());
Rental temp = null;
switch(choice) {
case 1:
for (int i = 0; i < 8; i++)
{
for (int j = i + 1; j < 8; j++)
{
if (rentals[i].getContractNumber().compareTo(rentals[j].getContractNumber()) > 0)
{
temp = rentals[i];
rentals[i] = rentals[j];
rentals[j] = temp;
}
}
}
System.out.println("Details of the Rentals sorted in ascending order by Contract number : ");
System.out.println();
for (int i = 0; i < 8; i++) {
System.out.println(" Details of Rental " + (i + 1));
System.out.println("--------------------------------------------------- ");
displayDetails(rentals[i]);
}
System.out.println();
break;
case 2:
for (int i = 0; i < 8; i++)
{
for (int j = i + 1; j < 8; j++)
{
if (rentals[i].getPrice() > rentals[j].getPrice())
{
temp = rentals[i];
rentals[i] = rentals[j];
rentals[j] = temp;
}
}
}
System.out.println();
System.out.println("Details of the Rentals sorted in ascending order by Price : ");
for (int i = 0; i < 8; i++) {
System.out.println(" Details of Rental " + (i + 1));
System.out.println("--------------------------------------------------- ");
displayDetails(rentals[i]);
}
System.out.println();
break;
case 3:
for (int i = 0; i < 8; i++)
{
for (int j = i + 1; j < 8; j++)
{
if (rentals[i].getEquipment().compareTo(rentals[j].getEquipment()) > 0)
{
temp = rentals[i];
rentals[i] = rentals[j];
rentals[j] = temp;
}
}
}
System.out.println();
System.out.println("Details of the Rentals sorted in ascending order by Equipment Type : ");
for (int i = 0; i < 8; i++) {
System.out.println(" Details of Rental " + (i + 1));
System.out.println("--------------------------------------------------- ");
displayDetails(rentals[i]);
}
System.out.println();
break;
case 4:
System.out.println("Thanks for using this.");
System.exit(0);
default:
System.out.println("Please select the correct choice.");
}
}
}
//method to show the menu
public static void showMenu() {
System.out.println("Please select the option : ");
System.out.println("1. Sort by Contact number.");
System.out.println("2. Sort by Price.");
System.out.println("3. Sort by Equipment Type.");
System.out.println("4. Exit");
}
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 phoneNumber;
Scanner input = new Scanner(System.in);
System.out.print("Enter contact phone number >> ");
phoneNumber = input.nextLine();
return phoneNumber;
}
public static int getMinutes() {
int minutes;
Scanner input = new Scanner(System.in);
System.out.print("Enter minutes >> ");
minutes = input.nextInt();
return minutes;
}
public static int getEquipment() {
int equipment;
Scanner input = new Scanner(System.in);
System.out.print("Enter equipment >>");
equipment = input.nextInt();
return equipment;
}
public static void displayDetails(Rental r) {
System.out.println(" Contract #" + r.getContractNumber());
System.out.print("For a rental of " + r.getHours() + " hours and " + r.getExtraMinutes());
System.out.printf(" minutes, at a rate of $%.2f", r.HOUR_RATE);
System.out.printf(" the price is $%.2f ", r.getPrice());
System.out.println("Contact phone Number is :" + r.getContactPhoneNumber());
System.out.println("The equipment you rented is :" + r.getEquipment());
}
public static Rental getLongerRental(Rental r1, Rental r2) {
Rental longer = new Rental();
int minutes1;
int minutes2;
minutes1 = r1.getHours() * Rental.MINUTES_IN_HOUR + r1.getExtraMinutes();
minutes2 = r2.getHours() * Rental.MINUTES_IN_HOUR + r2.getExtraMinutes();
if (minutes1 >= minutes2)
longer = r1;
else
longer = r2;
return longer;
}
public static void displayCoupon(Rental r1) {
int loopcount = r1.getHours();
for (int i = 0; i < loopcount; i++) {
System.out.println("Coupon good for 10 percent off next rental with your rental of contract number "
+ r1.getContractNumber() + ".");
}
}
}
Output
Enter details of Rental 1
---------------------------------------------------
Enter contract number >> 12
Enter minutes >> 1232
Enter contact phone number >> 98787787876
Enter equipment >>2
Enter details of Rental 2
---------------------------------------------------
Enter contract number >> 32
Enter minutes >> 43
Enter contact phone number >> 32324232
Enter equipment >>1
Enter details of Rental 3
---------------------------------------------------
Enter contract number >> 2121
Enter minutes >> 3233
Enter contact phone number >> 3232323
Enter equipment >>4
Enter details of Rental 4
---------------------------------------------------
Enter contract number >> 212
Enter minutes >> 333
Enter contact phone number >> 2223221323
Enter equipment >>8
Enter details of Rental 5
---------------------------------------------------
Enter contract number >> 323
Enter minutes >> 22
Enter contact phone number >> 4448222
Enter equipment >>7
Enter details of Rental 6
---------------------------------------------------
Enter contract number >> 7
Enter minutes >> 3232
Enter contact phone number >> 243242332
Enter equipment >>3
Enter details of Rental 7
---------------------------------------------------
Enter contract number >> 1
Enter minutes >> 24242
Enter contact phone number >> 21323442424
Enter equipment >>5
Enter details of Rental 8
---------------------------------------------------
Enter contract number >> 324242
Enter minutes >> 21
Enter contact phone number >> 323242
Enter equipment >>3
Please select the option :
1. Sort by Contact number.
2. Sort by Price.
3. Sort by Equipment Type.
4. Exit
6
Please select the correct choice.
Please select the option :
1. Sort by Contact number.
2. Sort by Price.
3. Sort by Equipment Type.
4. Exit
1
Details of the Rentals sorted in ascending order by Contract number :
Details of Rental 1
---------------------------------------------------
Contract #A000
For a rental of 20 hours and 32 minutes, at a rate of $40.00 the price is $832.00
Contact phone Number is :(000)000-0000
The equipment you rented is :rowboat
Details of Rental 2
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 43 minutes, at a rate of $40.00 the price is $40.00
Contact phone Number is :(000)000-0000
The equipment you rented is :pontoon boat
Details of Rental 3
---------------------------------------------------
Contract #A000
For a rental of 53 hours and 53 minutes, at a rate of $40.00 the price is $2160.00
Contact phone Number is :(000)000-0000
The equipment you rented is :kayak
Details of Rental 4
---------------------------------------------------
Contract #A000
For a rental of 5 hours and 33 minutes, at a rate of $40.00 the price is $233.00
Contact phone Number is :(222)322-1323
The equipment you rented is :other
Details of Rental 5
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 22 minutes, at a rate of $40.00 the price is $22.00
Contact phone Number is :(000)000-0000
The equipment you rented is :other
Details of Rental 6
---------------------------------------------------
Contract #A000
For a rental of 53 hours and 52 minutes, at a rate of $40.00 the price is $2160.00
Contact phone Number is :(000)000-0000
The equipment you rented is :canoe
Details of Rental 7
---------------------------------------------------
Contract #A000
For a rental of 404 hours and 2 minutes, at a rate of $40.00 the price is $16162.00
Contact phone Number is :(000)000-0000
The equipment you rented is :beach chair
Details of Rental 8
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 21 minutes, at a rate of $40.00 the price is $21.00
Contact phone Number is :(000)000-0000
The equipment you rented is :canoe
Please select the option :
1. Sort by Contact number.
2. Sort by Price.
3. Sort by Equipment Type.
4. Exit
2
Details of the Rentals sorted in ascending order by Price :
Details of Rental 1
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 21 minutes, at a rate of $40.00 the price is $21.00
Contact phone Number is :(000)000-0000
The equipment you rented is :canoe
Details of Rental 2
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 22 minutes, at a rate of $40.00 the price is $22.00
Contact phone Number is :(000)000-0000
The equipment you rented is :other
Details of Rental 3
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 43 minutes, at a rate of $40.00 the price is $40.00
Contact phone Number is :(000)000-0000
The equipment you rented is :pontoon boat
Details of Rental 4
---------------------------------------------------
Contract #A000
For a rental of 5 hours and 33 minutes, at a rate of $40.00 the price is $233.00
Contact phone Number is :(222)322-1323
The equipment you rented is :other
Details of Rental 5
---------------------------------------------------
Contract #A000
For a rental of 20 hours and 32 minutes, at a rate of $40.00 the price is $832.00
Contact phone Number is :(000)000-0000
The equipment you rented is :rowboat
Details of Rental 6
---------------------------------------------------
Contract #A000
For a rental of 53 hours and 52 minutes, at a rate of $40.00 the price is $2160.00
Contact phone Number is :(000)000-0000
The equipment you rented is :canoe
Details of Rental 7
---------------------------------------------------
Contract #A000
For a rental of 53 hours and 53 minutes, at a rate of $40.00 the price is $2160.00
Contact phone Number is :(000)000-0000
The equipment you rented is :kayak
Details of Rental 8
---------------------------------------------------
Contract #A000
For a rental of 404 hours and 2 minutes, at a rate of $40.00 the price is $16162.00
Contact phone Number is :(000)000-0000
The equipment you rented is :beach chair
Please select the option :
1. Sort by Contact number.
2. Sort by Price.
3. Sort by Equipment Type.
4. Exit
3
Details of the Rentals sorted in ascending order by Equipment Type :
Details of Rental 1
---------------------------------------------------
Contract #A000
For a rental of 404 hours and 2 minutes, at a rate of $40.00 the price is $16162.00
Contact phone Number is :(000)000-0000
The equipment you rented is :beach chair
Details of Rental 2
---------------------------------------------------
Contract #A000
For a rental of 53 hours and 52 minutes, at a rate of $40.00 the price is $2160.00
Contact phone Number is :(000)000-0000
The equipment you rented is :canoe
Details of Rental 3
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 21 minutes, at a rate of $40.00 the price is $21.00
Contact phone Number is :(000)000-0000
The equipment you rented is :canoe
Details of Rental 4
---------------------------------------------------
Contract #A000
For a rental of 53 hours and 53 minutes, at a rate of $40.00 the price is $2160.00
Contact phone Number is :(000)000-0000
The equipment you rented is :kayak
Details of Rental 5
---------------------------------------------------
Contract #A000
For a rental of 5 hours and 33 minutes, at a rate of $40.00 the price is $233.00
Contact phone Number is :(222)322-1323
The equipment you rented is :other
Details of Rental 6
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 22 minutes, at a rate of $40.00 the price is $22.00
Contact phone Number is :(000)000-0000
The equipment you rented is :other
Details of Rental 7
---------------------------------------------------
Contract #A000
For a rental of 0 hours and 43 minutes, at a rate of $40.00 the price is $40.00
Contact phone Number is :(000)000-0000
The equipment you rented is :pontoon boat
Details of Rental 8
---------------------------------------------------
Contract #A000
For a rental of 20 hours and 32 minutes, at a rate of $40.00 the price is $832.00
Contact phone Number is :(000)000-0000
The equipment you rented is :rowboat
Please select the option :
1. Sort by Contact number.
2. Sort by Price.
3. Sort by Equipment Type.
4. Exit
4
Thanks for using this.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.