Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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.

Java Programming 8th Edition

Chapter 9 Case Problem 2

*** Rental.java ***

class Rental { public static final int MINUTES_IN_HOUR = 60; public static final double HOUR_RATE = 40; public static final int CONTRACT_NUM_LENGTH = 4; // Added string of equipment types public static final String[] EQUIP_TYPES = {"jet ski", "pontoon boat", "rowboat", "canoe", "kayak", "beach chair", "umbrella", "other"}; private String contractNumber; private int hours; private int extraMinutes; private double price; private String contactPhone; private int equipType; public Rental(String num, int minutes) { setContractNumber(num); setHoursAndMinutes(minutes); } public Rental() { this("A000", 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) 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 double getPrice() { return price; } 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; } // Added equipment type, uses an integer value public void setEquipType(int eType) { if(eType < EQUIP_TYPES.length) equipType = eType; else equipType = EQUIP_TYPES.length - 1; } public int getEquipType() { return equipType; } public String getEquipTypeString() { return EQUIP_TYPES[equipType]; } }

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

Students also viewed these Databases questions