Question

In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now...

In previous chapters, you developed classes that hold rental contract information for Sammy's 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 Sammy's rents—personal watercraft, 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 rental's 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.

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;

private String contactPhoneNumber;

// default constructor

Rental() {

this("A000", 0);

}

// parameterized constructor

Rental(String num, int minutes) {

setContractNumber(num);

setHoursAndMinutes(minutes);

}

public void setContractNumber(String num) {

if(num.matches("[a-zA-Z][0-9]{3}"))

{

num = num.substring(0, 1).toUpperCase() + num.substring(1);

contractNumber = num;

}

else

{

setContractNumber("A000");

}

}

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;

}

public String getContactPhoneNumber() {

if(contactPhoneNumber !=null && !contactPhoneNumber.isEmpty())

return " (" + contactPhoneNumber.substring(0, 3) + ") " + contactPhoneNumber.substring(3, 6) + " - " + contactPhoneNumber.substring(6);

return "";

}

public void setContactPhoneNumber(String contactPhoneNumber) {

contactPhoneNumber= contactPhoneNumber.replaceAll("[^a-zA-Z0-9]", "");

if(contactPhoneNumber.length()<10)

{

setContactPhoneNumber("0000000000");

}

else

{

this.contactPhoneNumber = contactPhoneNumber;

}

}

public class RentalDemo {

   public static void main(String[] args) {

   String contractNum;

   String contactPhoneNumber;

   int minutes;

   contractNum = getContractNumber();

   contactPhoneNumber = getContractPhoneNumber();

   minutes = getMinutes();

   Rental r1 = new Rental(contractNum, minutes);

   r1.setContactPhoneNumber(contactPhoneNumber);

   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,\n and Contract #" + r2.getContractNumber() + " with a time of "

   + r2.getHours() + " hours and " + r2.getExtraMinutes()

   + " minutes,\n 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,\n and Contract #" + r3.getContractNumber() + " with a time of "

   + r3.getHours() + " hours and " + r3.getExtraMinutes()

   + " minutes,\n 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,\n and Contract #" + r3.getContractNumber() + " with a time of "

   + r3.getHours() + " hours and " + r3.getExtraMinutes()

   + " minutes,\n 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 getContractPhoneNumber() {

   String num;

   Scanner input = new Scanner(System.in);

   System.out.print("Enter contract Phone 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("\nContract #" + r.getContractNumber());

   System.out.println("\nContact Phone Number#" + r.getContactPhoneNumber());

   System.out.print("For a rental of " + r.getHours() + " hours and " + r.getExtraMinutes());

   System.out.printf(" minutes, at a rate of $%.2f\n", 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;

   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

Rental.java

public class Rental
{
public final static int MINUTES_IN_HOUR = 60;
public final static double HOUR_RATE = 40.00;
public final static String typeNames[]={"personal watercraft","pontoon boat", "rowboat", "canoe", "kayak", "beach chair", "umbrella", "other"};
private double price;
private String contractNumber;
private int hours;
private int extraMinutes;
private String contactPhoneNumber;
private int type;
  
// default constructor
Rental()
{
this("A000", 0);
}
// parameterized constructor
Rental(String num, int minutes)
{
setContractNumber(num);
setHoursAndMinutes(minutes);
}

public int getType() {
return type;
}

public void setType(int type) {
if(type>typeNames.length)
this.type=typeNames.length;
else
this.type = type;
}
  
public String getTypeString()
{
return typeNames[type-1];
}
public void setContractNumber(String num)
{
if(num.matches("[a-zA-Z][0-9]{3}"))
{
num = num.substring(0, 1).toUpperCase() + num.substring(1);
contractNumber = num;
}
else
{
setContractNumber("A000");
}
}
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;
}
public String getContactPhoneNumber() {
if(contactPhoneNumber !=null && !contactPhoneNumber.isEmpty())
return " (" + contactPhoneNumber.substring(0, 3) + ") " + contactPhoneNumber.substring(3, 6) + " - " + contactPhoneNumber.substring(6);
return "";
}
public void setContactPhoneNumber(String contactPhoneNumber) {
contactPhoneNumber= contactPhoneNumber.replaceAll("[^a-zA-Z0-9]", "");
if(contactPhoneNumber.length()<10)
{
setContactPhoneNumber("0000000000");
}
else
{
this.contactPhoneNumber = contactPhoneNumber;
}
}
}

RentalDemo.java


import java.util.Scanner;

public class RentalDemo
{
public static void main(String[] args)
{
String contractNum;
String contactPhoneNumber;
int minutes,type;
Rental r[]=new Rental[3];
for(int i=0;i<r.length;i++)
{
System.out.println("\nEnter Informatio for item number #"+(i+1));
contractNum = getContractNumber();
contactPhoneNumber = getContractPhoneNumber();
minutes = getMinutes();
type=getType();
r[i]= new Rental(contractNum, minutes);
r[i].setContactPhoneNumber(contactPhoneNumber);
r[i].setType(type);
}
for(int i=0;i<r.length;i++)
{
System.out.println("\nRent item number #"+(i+1));
displayDetails(r[i]);
}
}

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 getContractPhoneNumber() {
String num;
Scanner input = new Scanner(System.in);
System.out.print("Enter contract Phone number >> ");
num = input.nextLine();
return num;
}
public static int getMinutes() {
int minutes;
Scanner input = new Scanner(System.in);
  
do {
System.out.print("Enter minutes >> ");
minutes = input.nextInt();
if (minutes <= 60 || minutes >= 7200)
System.out.println("Please enter a valid input for minuted (between 60 and 7200)");
} while (minutes <= 59 || minutes >= 7201);
return minutes;
}
public static void displayDetails(Rental r)
{
System.out.println("Contract #" + r.getContractNumber());
System.out.println("Contact Phone Number#" + r.getContactPhoneNumber());
System.out.println("Rent item: "+r.getTypeString());
System.out.print("For a rental of " + r.getHours() + " hours and " + r.getExtraMinutes());
System.out.printf(" minutes, at a rate of $%.2f\n", r.getPrice());
}

private static int getType()
{
int type;
Scanner input = new Scanner(System.in);
System.out.print("Enter rent item type (1 to 8) >> ");
type=input.nextInt();
return type;
}
}

outputs


If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...

    Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, new java.util.Date().toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutesmethods. (2) Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

    I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank. package edu.wit.cs.comp1050; /** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c {       /**    * Error message to display for negative amount    */    public static final String ERR_MSG =...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT