Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

In previous chapters, you developed classes that hold rental contract information for Sammys Seashore Supplies. Now modify the Rental and RentalDemo classes as follows: Modify

In previous chapters, you developed classes that hold rental contract information for Sammys Seashore Supplies. Now modify the Rental and RentalDemo classes as follows: Modify the Rental class to include an integer field that holds an equipment type. Add a final String array that holds names of the types of equipment that Sammys rentsjet ski, pontoon boat, rowboat, canoe, kayak, beach chair, umbrella, and other. Include get and set methods for the integer equipment type field. If the argument passed to the method that sets the equipment type is larger than the size of the array of String equipment types, then set the integer to the element number occupied by other. Include a get method that returns a rentals String equipment type based on the numeric equipment type. To keep the RentalDemo class simple, remove all the statements that compare rental times and that display the coupon Strings. Modify the RentalDemo class so that instead of creating three single Rental objects, it uses an array of three Rental objects. Get data for each of the objects, and then display all the details for each object. Save the files as Rental.java and 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; public Rental(String num, int minutes, String phone) { setContractNumber(num); setHoursAndMinutes(minutes); setContactPhoneNumber(phone); } public Rental() { this("A000", 0, "0000000000"); } 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; } } import java.util.Scanner; public class RentalDemo { public static void main(String[] args) { String contractNum; int minutes; String phoneNum; contractNum = getContractNumber(); minutes = getMinutes(); phoneNum = getContactPhoneNumber(); while (minutes < 60 || minutes > 7200) minutes = getMinutes(); Rental r1 = new Rental(contractNum, minutes, phoneNum); contractNum = getContractNumber(); minutes = getMinutes(); phoneNum = getContactPhoneNumber(); while (minutes < 60 || minutes > 7200) minutes = getMinutes(); Rental r2 = new Rental(contractNum, minutes, phoneNum); contractNum = getContractNumber(); minutes = getMinutes(); phoneNum = getContactPhoneNumber(); while (minutes < 60 || minutes > 7200) minutes = getMinutes(); Rental r3 = new Rental(contractNum, minutes, phoneNum); displayCoupon(r1); displayDetails(r1); displayDetails(r2); displayDetails(r3); System.out.println("Of Contract #" + r1.getContractNumber() + " with a time of " + r1.getHours() + " hours and " + r1.getExtraMinutes() + " minutes, and Contract #" + r2.getContractNumber() + " with a time of " + r2.getHours() + " hours and " + r2.getExtraMinutes() + " minutes, the one with the longer time is Contract #" + getLongerRental(r1, r2).getContractNumber()); System.out.println("Of Contract #" + r1.getContractNumber() + " with a time of " + r1.getHours() + " hours and " + r1.getExtraMinutes() + " minutes, and Contract #" + r3.getContractNumber() + " with a time of " + r3.getHours() + " hours and " + r3.getExtraMinutes() + " minutes, the one with the longer time is Contract #" + getLongerRental(r1, r3).getContractNumber()); System.out.println("Of Contract #" + r2.getContractNumber() + " with a time of " + r2.getHours() + " hours and " + r2.getExtraMinutes() + " minutes, and Contract #" + r3.getContractNumber() + " with a time of " + r3.getHours() + " hours and " + r3.getExtraMinutes() + " 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 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 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()); } 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 with AI-Powered 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

Students also viewed these Databases questions