Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

2. 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.

================

Rental:

public class Rental { public final static int Minutes = 60; public final static double Rate = 40; private String contractNumber; private String contactPhoneNumber; private int rentalHours; private int rentalMinutes; private double price; private int equipmentType; private final String[] EQUIPMENT_NAMES = {"jet ski", "pontoon boat", "rowboat", "canoe", "kayak", "beach chair", "umbrella", "other"}; /** * @param num * @param mins * sets the values of string and int */ public Rental(String num, int mins,String phoneNumber, int equipType) { setHoursAndMinutes(mins); setContractNumber(num); setPhoneNumber(phoneNumber); setEquipmentType(equipType); } /** * @param phonenumber * sets phone number */ public void setPhoneNumber(String number) { //length of the phone number int length = number.length(); String newNumber=""; //Extract only digits from the entered number and store it in a newNumber for (int i = 0; i < length; i++) { char c = number.charAt(i); if (Character.isDigit(c)) { newNumber = newNumber+c; } } //if the phone number is less than 10 digits we are re setting it with 10 zero's if(newNumber.length() < 10 || newNumber.length() > 10){ contactPhoneNumber = "0000000000"; return; } contactPhoneNumber = newNumber; } /** * @return * returns phone number */ public String getPhoneNumber() { String phoneNumber; //Modify the phone number such that it holds the country code in () followed by space followed by 3 digits followed by "-" followed by 4 digits phoneNumber = "("+contactPhoneNumber.substring(0, 3)+")"+" "+contactPhoneNumber.substring(3, 6)+"-"+contactPhoneNumber.substring(6); return phoneNumber; } /** * @param number * sets contract number also does the Hours and mins */ public void setContractNumber(String number) { //char c = contractNumber.charAt(0); char c; //Declare a Character to store the upperCase letter char C; //length of the input string int length = number.length(); //checking whether the contractnumber is of length 4 if(length == 4){ c = number.charAt(0); //Check whether the first charcater is of Alphabet if(Character.isLetter(c)){ //checking remaining 3 characters are of digits if(number.substring(1).matches("\\d+")){ //Checking whether the first charcater is of Uppercase letter if (Character.isUpperCase(c)){ contractNumber = number; }else{ C = Character.toUpperCase(c); contractNumber = C+number.substring(1); } }else{ contractNumber="A000"; } }else{ contractNumber="A000"; } }else{ contractNumber="A000"; } } public void setContractNumber2(String number) { //char c = contractNumber.charAt(0); char c; //Declare a Character to store the upperCase letter char C; //length of the input string int length = number.length(); //checking whether the contractnumber is of length 4 if(length == 4){ c = number.charAt(0); //Check whether the first charcater is of Alphabet if(Character.isLetter(c)){ //checking remaining 3 characters are of digits if(number.substring(1).matches("\\d+")){ //Checking whether the first charcater is of Uppercase letter if (Character.isUpperCase(c)){ contractNumber = number; }else{ C = Character.toUpperCase(c); contractNumber = C+number.substring(1); } }else{ contractNumber="A000"; } }else{ contractNumber="A000"; } }else{ contractNumber="A000"; } } public void setContractNumber3(String number) { //char c = contractNumber.charAt(0); char c; //Declare a Character to store the upperCase letter char C; //length of the input string int length = number.length(); //checking whether the contractnumber is of length 4 if(length == 4){ c = number.charAt(0); //Check whether the first charcater is of Alphabet if(Character.isLetter(c)){ //checking remaining 3 characters are of digits if(number.substring(1).matches("\\d+")){ //Checking whether the first charcater is of Uppercase letter if (Character.isUpperCase(c)){ contractNumber = number; }else{ C = Character.toUpperCase(c); contractNumber = C+number.substring(1); } }else{ contractNumber="A000"; } }else{ contractNumber="A000"; } }else{ contractNumber="A000"; } } public void setHoursAndMinutes(int minutes) { rentalHours = minutes / Minutes; //hours rentalMinutes = minutes % Minutes; // minutes price = rentalHours * Rate + rentalMinutes; //price } /** * @return * returns contract number */ public String getContractNumber() { return contractNumber; } public String getContractNumber2() { return contractNumber; } public String getContractNumber3() { return contractNumber; } /** * @return * returns hours */ public int getHours() { return rentalHours; } /** * @return * returns minutes */ public int getMinutes() { return rentalMinutes; } /** * @return * returns Price */ public double getPrice() { return price; } public int getEquipmentType() { return equipmentType; } public void setEquipmentType(int equipType) { if(equipType < 0 || equipType >= EQUIPMENT_NAMES.length) equipType = EQUIPMENT_NAMES.length - 1; this.equipmentType = equipType; } public String getEquipmentName() { return EQUIPMENT_NAMES[equipmentType]; } }

===============

RentalDemo:

import java.util.Scanner; public class RentalDemo { /*creates an array of 3 rental objects and gets inputs and displays the objects*/ public static void main(String args[]) { Scanner scan = new Scanner(System.in); Rental[] rent = new Rental[3]; //an array of 3 objects String num,phone; int mins, type; //get inputs for 3 objects for(int i = 0; i < 3; i++) { System.out.println("Enter details of Rental No. " + (i+1)); System.out.print("\t Contract #? "); num = scan.next(); System.out.print("\t Minutes? "); mins = scan.nextInt(); System.out.print("\t Phone number? "); phone = scan.next(); System.out.print("\t Type (0-8)? "); type = scan.nextInt(); rent[i] = new Rental(num, mins,phone,type); } //display the rental objects for(int i = 0; i < 3;i++) { System.out.println((i+1) + ". Contract #"+ rent[i].getContractNumber() + " for " + rent[i].getEquipmentName() + " for " + rent[i].getHours() + " hours and " + rent[i].getMinutes() +" minutes costing $" + rent[i].getPrice()+ ". The phone number is " + rent[i].getPhoneNumber()); } } }

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

XML Data Management Native XML And XML Enabled Database Systems

Authors: Akmal Chaudhri, Awais Rashid, Roberto Zicari, John Fuller

1st Edition

0201844524, 978-0201844528

More Books

Students also viewed these Databases questions