2. Sammy\'s Seashore Supplies rents beach equipment to tourists. In previous cha
ID: 3722777 • Letter: 2
Question
2. Sammy's Seashore Supplies rents beach equipment to tourists. In previous chapters, you developed a class that holds equipment rental information and an application that tests the methods using four objects of the class. Now modify the RentalDemo class to do the following:
-Continously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive.
-For one of the Rental objects, create a loop that displays "Coupon good for 10 percent off next rental" as many times as there are full hours in the Rental.
Save the modified file as Rental Demo.java
Java Programming 8th Edition
*** Here is the Rental.java class ***
class Rental
{
public static final int MINUTES_IN_HOURS = 60;
public static final double HOUR_RATE = 40.00;
private String contractNumber;
private int hours;
private int extraMinutes;
private double price;
public Rental(String num, int minutes)
{
setContractNumber(num);
setHoursAndMinutes(minutes);
}
public Rental()
{
this("A000", 0);
}
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;
}
public String getContractNumber()
{
return contractNumber;
}
public int getHours()
{
return hours;
}
public int getExtraMinutes()
{
return extraMinutes;
}
public double getPrice()
{
return price;
}
}
Explanation / Answer
As I could not get your Rental Demo file .
I have created it .please modify as per your need.
RentalDemo .java
import java.util.ArrayList;
import java.util.Scanner;
public class RentalDemo {
public static void main(String[] args) {
//Create some rental objects
Rental r1=new Rental();
Rental r2=new Rental();
Rental r3=new Rental();
Rental r4=new Rental();
//Add rental object to Arraylist (So that we can iterate )
ArrayList<Rental> rentals=new ArrayList<>();
//Add objects to list
rentals.add(r1);
rentals.add(r2);
rentals.add(r3);
rentals.add(r4);
//Part 1 of your question
//loop to take minutes for each rental
Scanner sc=new Scanner(System.in);
for (Rental rental : rentals) {
int minutes=0;
do {
System.out.println("Please enter minutes : ");
minutes=sc.nextInt();
}while(minutes>=60 && minutes<=7200);
}
//part2 (We are taking object 1)
int hour=r1.getHours();
while(hour>0)
{
System.out.println("Coupon good for 10 percent off next rental");
hour--;
}
}
}
Rental.java
public class Rental
{
public final int MINUTES_IN_HOURS = 60;
public final double HOUR_RATE = 40.00;
private String contractNumber;
private int hours;
private int extraMinutes;
private double price;
public Rental(String num, int minutes)
{
setContractNumber(num);
setHoursAndMinutes(minutes);
}
public Rental()
{
this("A000", 0);
}
public void setContractNumber(String num)
{
contractNumber = num;
}
public void setHoursAndMinutes(int minutes)
{
hours = minutes / MINUTES_IN_HOURS;
extraMinutes = minutes % MINUTES_IN_HOURS;
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.