Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the last chapter, you modified the RentalDemo program for Sammys Seashore Supplies to accept and display data for an array of three Rental objects.

In the last chapter, you modified the RentalDemo program for Sammys 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() + "."); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Example. Evaluate 5n+7 lim 7-00 3n-5

Answered: 1 week ago