Question: In Chapter 11, you created an interactive RentalDemo class that obtains all the data for four rentals from Sammys Seashore Rentals, including details about the

In Chapter 11, you created an interactive RentalDemo class that obtains all the data for four rentals from Sammys Seashore Rentals, including details about the contract number, length of the rental, and equipment type. Now, modify the class so that it becomes immune to user data entry errors by handling exceptions for each numeric entry. Each time the program requires numeric datafor example, for the rental periodcontinuously prompt the user until the data entered is the correct type. Save the revised program as RentalDemo.java.

import java.util.Scanner; public class RentalDemo { public static void main(String[] args) { int option; String contractNum; int minutes; final int QUIT = 9; int eType; Rental[] rentals = new Rental[1]; int x; for(x = 0; x < rentals.length; ++x) { contractNum = getContractNumber(); minutes = getMinutes(); eType = getType(); rentals[x] = new Rental(contractNum, minutes, eType); rentals[x].setContactPhone(getPhone()); } System.out.println(" Now display the rentals"); for(x = 0; x < rentals.length; ++x) { displayDetails(rentals[x]); } } 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 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 int getType() { int eType; Scanner input = new Scanner(System.in); System.out.println("Equipment types:"); for(int x = 0; x < Equipment.EQUIP_TYPES.length; ++x) System.out.println(" " + x + " " + Equipment.EQUIP_TYPES[x]); System.out.print("Enter equipment type >> "); eType = input.nextInt(); return eType; } public static void displayDetails(Rental r) { Equipment e = r.getEquipment(); System.out.println(" Contract #" + r.getContractNumber()); System.out.println("For a rental of " + r.getHours() + " hours and " + r.getExtraMinutes() + " minutes, at a rate of " + r.HOUR_RATE + " per hour and $1 per extra minute, the base price is $" + r.getBasePrice()); System.out.println("Contact phone number is: " + r.getContactPhone()); System.out.println("The equipment you rented is #" + e.getEquipType() + " " + e.getEquipName()); System.out.println(e.getLessonMessage()); System.out.printf("The equipment fee with lesson is $%.2f.%n", e.getFee()); System.out.printf("The total price is $%.2f.%n", r.getPrice()); System.out.println("-------------------------------"); } public static String getPhone() { String phone; Scanner input = new Scanner(System.in); System.out.print("Enter contact phone number >> "); phone = input.nextLine(); return phone; } }

-------------------

class Rental { public static final int MINUTES_IN_HOUR = 60; public static final int HOUR_RATE = 40; public static final int CONTRACT_NUM_LENGTH = 4; Equipment equipment; private String contractNumber; private int hours; private int extraMinutes; private double basePrice; private double price; private String contactPhone; public Rental(String num, int minutes, int eType) { setContractNumber(num); setHoursAndMinutes(minutes); if(eType <= Equipment.HIGH) equipment = new EquipmentWithLesson(eType); else equipment = new EquipmentWithoutLesson(eType); price = basePrice + equipment.getFee(); } public Rental() { this("A000", 0, 0); } public void setContractNumber(String num) { boolean numOk = true; if(num.length() != CONTRACT_NUM_LENGTH || !Character.isLetter(num.charAt(0)) || !Character.isDigit(num.charAt(1)) || !Character.isDigit(num.charAt(2)) || !Character.isDigit(num.charAt(3))) contractNumber = "A000"; else contractNumber = num.toUpperCase(); } public void setHoursAndMinutes(int minutes) { hours = minutes / MINUTES_IN_HOUR; extraMinutes = minutes % MINUTES_IN_HOUR; if(extraMinutes <= HOUR_RATE) basePrice = hours * HOUR_RATE + extraMinutes; else basePrice = hours * HOUR_RATE + HOUR_RATE; } public String getContractNumber() { return contractNumber; } public int getHours() { return hours; } public int getExtraMinutes() { return extraMinutes; } public double getPrice() { return price; } public double getBasePrice() { return basePrice; } public String getContactPhone() { String phone; phone = "(" + contactPhone.substring(0, 3) + ") " + contactPhone.substring(3, 6) + "-" + contactPhone.substring(6, 10); return phone; } public void setContactPhone(String phone) { final int VALID_LEN = 10; final String INVALID_PHONE = "0000000000"; contactPhone = ""; int len = phone.length(); for(int x = 0; x < len; ++x) { if(Character.isDigit(phone.charAt(x))) contactPhone += phone.charAt(x); } if(contactPhone.length() != VALID_LEN) contactPhone = INVALID_PHONE; } public Equipment getEquipment() { return equipment; } }

__________________

abstract class Equipment { public static final String[] EQUIP_TYPES = {"jet ski", "pontoon boat", "rowboat", "canoe", "kayak", "beach chair", "umbrella", "other"}; public final static int HIGH = 4; public static final double[] FEES = {50, 40, 15, 12, 10, 2, 1, 0}; private int equipType; private String equipName; protected double fee; public Equipment(int equipmentType) { if(equipType < EQUIP_TYPES.length) equipType = equipmentType; else equipType = EQUIP_TYPES.length - 1; } public int getEquipType() { return equipType; } public String getEquipName() { return equipName; } public double getFee() { return fee; } protected void setEquipType(int eType) { equipType = eType; } protected void setName() { equipName = EQUIP_TYPES[equipType]; } protected void setFee() { fee = FEES[equipType]; } public abstract String getLessonMessage(); }

------------------------

class EquipmentWithoutLesson extends Equipment { public static final String MSG = " This type of rental does not require a lesson."; EquipmentWithoutLesson(int equipmentType) { super(equipmentType); if(equipmentType > HIGH) setEquipType(equipmentType); else setEquipType(EQUIP_TYPES.length - 1); setName(); setFee(); } public String getLessonMessage() { return MSG; } }

---------------------

class EquipmentWithLesson extends Equipment { public static final double LESSON_FEE = 27.00; double tempFee; EquipmentWithLesson(int equipmentType) { super(equipmentType); if(equipmentType <= HIGH) setEquipType(equipmentType); else setEquipType(EQUIP_TYPES.length - 1); setName(); setFee(); tempFee = fee; fee += LESSON_FEE; } public String getLessonMessage() { return "This type of rental requires a lesson for $" + LESSON_FEE + "."; } }

----------------------

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!