Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sammys Seashore Supplies rents beach equipment to tourists. In previous chapters, you have developed a class that holds equipment rental information and an application that

Sammys Seashore Supplies rents beach equipment to tourists. In previous chapters, you have developed a class that holds equipment rental information and an application that tests the methods using four objects of the class. Now modify the Rental and RentalDemo classes as follows:

-Modify the method that sets the contract number in the Rental class so that if the argument passed to the method is not a four-character String that starts with a letter followed by three digits, then the contract number is forced to A000. If the initial letter in the contract number is not uppercase, force it to be so.

-Add a contact phone number field to the Rental class.

-Add a set method for the contact phone number field in the Rental class. Whether the user enters all digits or any combination of digits, spaces, dashes, dots, or parentheses for a phone number, store it as all digits. For example, if the user enters (920) 872-9182, store the phone number as 9208729182. If the user enters a number with fewer or more than 10 digits, store the number as 0000000000.

-Add a get method for the phone number field. The get method returns the phone number as a String constructed as follows: parentheses surround a three-digit area code, followed by a space, followed by the three-digit phone exchange, followed by a hyphen, followed by the last four digits of the phone number.

-Modify the RentalDemo program so that besides the contract number and minutes, the program also prompts the user for and retrieves a contact phone number for each of the sample objects. Display the phone number along with the other Rental details. Test the RentalDemo application to make sure it works correctly with valid and invalid contract and phone numbers.

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

/* Rental Class */

import java.util.*;

public class Rental {

public final static int MINUTES_IN_HOUR = 60;

public final static double HOUR_RATE = 40.00;

private double price;

private String contractNumber;

private int hours;

private int extraMinutes;

// default constructor

Rental() {

this("A000", 0);

}

// parameterized constructor

Rental(String num, int minutes) {

setContractNumber(num);

setHoursAndMinutes(minutes);

}

public void setContractNumber(String num) {

contractNumber = num;

}

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;

}

// get methods

public String getContractNumber() {

return contractNumber;

}

public double getPrice() {

return price;

}

public int getHours() {

return hours;

}

public int getExtraMinutes() {

return extraMinutes;

}

}

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

/*Rental Demo*/

import java.util.Scanner;

public class RentalDemo {

public static void main(String[] args) {

String contractNum;

int minutes;

contractNum=getContractNumber();

minutes=getMinutes();

Rental r1 = new Rental (contractNum, minutes);

contractNum=getContractNumber();

minutes=getMinutes();

Rental r2 = new Rental (contractNum, minutes);

contractNum=getContractNumber();

minutes=getMinutes();

Rental r3= new Rental (contractNum, minutes);

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 int getMinutes() {

int minutes;

Scanner input= new Scanner(System.in);

System.out.print("Enter minutes >> ");

minutes=input.nextInt();

do {

if(minutes<=60 || minutes>=7200)

System.out.println("Please enter a valid input for minuted (between 60 and 7200)");

System.out.print("Enter minutes >> ");

minutes=input.nextInt();

}while(minutes<=59 || minutes>=7201);

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.getPrice());

}

public static Rental getLongerRental (Rental r1, Rental r2) {

Rental longer = new Rental();

int minutes1;

int minutes2;

final double COUPON = 0.1;

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;

}

if(minutes1>=minutes2){

System.out.println("You've earned a reward coupon! Valid for 10% off your next rental. Coupon Value: " + (minutes1 * COUPON));

}else{

System.out.println("You've earned a reward coupon! Valid for 10% off your next rental. Coupon Value: " + (minutes2 * COUPON));

}

return longer;

}

}

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

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions

Question

5. Describe how contexts affect listening

Answered: 1 week ago