In: Computer Science
Getting an error with my for loop. How do I correct it? I am supposed to: Continuously 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 10percent off next rental as many times as there are full hours in the Rental.
///////////////////////////////RentalDemo
package java1;
import java.util.Scanner;
public class RentalDemo {
public static void main(String[] args) {
Rental object1 = new Rental();
Rental object2 = new Rental();
Rental object3 = new Rental();
String contractNumber;
int hours;
int minutes;
int totalMins;
Scanner input = new Scanner(System.in);
System.out.println("RENTAL
1: ");
System.out.println("Enter
contract number: ");
contractNumber
= input.nextLine();
System.out.println("Enter
rental hours: ");
hours
= input.nextInt();
System.out.println("Enter
rental minutes: ");
minutes
= input.nextInt();
object1.setContractNumber(contractNumber);
object1.setRentalTimeHours(hours);
object1.setRentalTimeMins(minutes);
System.out.println("RENTAL
2: ");input.nextLine();
System.out.println("Enter
contract number: ");
contractNumber
= input.nextLine();
System.out.println("Enter
rental hours: ");
hours
= input.nextInt();
System.out.println("Enter
rental minutes: ");
minutes
= input.nextInt();
object2.setContractNumber(contractNumber);
object2.setRentalTimeHours(hours);
object2.setRentalTimeMins(minutes);
System.out.println("RENTAL
3: ");input.nextLine();
System.out.println("Enter
contract number: ");
contractNumber
= input.nextLine();
System.out.println("Enter
rental hours: ");
hours
= input.nextInt();
System.out.println("Enter
rental minutes: ");
minutes
= input.nextInt();
object3.setContractNumber(contractNumber);
object3.setRentalTimeHours(hours);
object3.setRentalTimeMins(minutes);
displayMotto();
System.out.println();
displayobject1(object1);
displayobject2(object2);
displayobject3(object3);
System.out.println("Get
largest rental between event # 1 and event # 2 is :");
displayobject1(getRentalWithLongerTime(object1,
object2));
System.out.println("Get
largest rental between event # 2 and event # 3 is :");
displayobject2(getRentalWithLongerTime(object2,
object3));
System.out.println("Get
largest rental from event # 1 and event # 3 is :");
displayobject3(getRentalWithLongerTime(object1,
object3));
System.out.println("");
for
(int r = 0; r < totalMins; r++) {
object4[r]
= new Rental();
object4[r].setEventNumber();
do
{
System.out.println("RENTAL:
");
System.out.println("Enter
contract number: ");
contractNumber
= input.nextLine();
System.out.println("Enter
the total time of the rental (in minutes): ");
totalMins
= input.nextInt();
object1.setContractNumber(contractNumber);
object1.setRentalTimeHours(totalMins);
System.out.println("The
rental time must be between 60 minutes and 7,200 minutes");
}while(!(totalMins
>= 60 && totalMins <= 7200));
for(int
i=0;i
System.out.println("");
System.out.println("!!!!!
This coupon is good for 10 percent off your next rental !!!!!");
}}}
private
static void displayobject1(Rental object1) {
System.out.println("Rental
Contract number: "+object1.getContractNumber()+", Time:
"+object1.getRentalTimeHours()+"h+"+object1.getRentalTimeMins()+"m,
Price:
$"+object1.calculateRentalPrice()); }
private
static void displayobject2(Rental object2);{
System.out.println("Rental
Contract number: "+object2.getContractNumber()+", Time:
"+object2.getRentalTimeHours()+"h+"+object2.getRentalTimeMins()+"m,
Price:
$"+object2.calculateRentalPrice());
private
static void displayobject3(Rental object3) {
System.out.println("Rental
Contract number: "+object3.getContractNumber()+", Time:
"+object3.getRentalTimeHours()+"h+"+object3.getRentalTimeMins()+"m,
Price:
$"+object3.calculateRentalPrice()); }
public
static Rental getRentalWithLongerTime(Rental rental1, Rental
rental2){
double
totalTimeRental1 = rental1.getRentalTimeHours()+
rental1.getRentalTimeMins()/60;
double
totalTimeRental2 = rental2.getRentalTimeHours()+
rental2.getRentalTimeMins()/60;
if(totalTimeRental1>=
totalTimeRental2)
return
rental1;
else
return
rental2; }
public static void
displayMotto() {
System.out.println();
System.out.println("Motto");}}
public class Rental {
private String contractNum;
private int rentalTimeHours;
private int rentalTimeMins;
public String getContractNumber() {
return contractNum;
}
public void setContractNumber(String contractNumber)
{
this.contractNum = contractNumber;
}
public int getRentalTimeHours() {
return rentalTimeHours;
}
public void setRentalTimeHours(int rentalTimeHours)
{
this.rentalTimeHours = rentalTimeHours;
}
public int getRentalTimeMins() {
return rentalTimeMins;
}
public void setRentalTimeMins(int rentalTimeMins)
{
this.rentalTimeMins = rentalTimeMins;
}
public double
calculateRentalPrice(){
int pricePerHour = 40;
int pricePerMinute = 1;
int minutesThatCount;
if(this.getRentalTimeMins()<=40){
minutesThatCount = this.getRentalTimeMins();
}else{
minutesThatCount = 40;
}
int HoursPrice =
this.getRentalTimeHours()*pricePerHour ;
int MinutesPrice =
minutesThatCount*pricePerMinute;
int totalRentalPrice = HoursPrice +
MinutesPrice;
return totalRentalPrice;
}
}
Short Summary:
Source Code:
RentalDemo.java:
import java.util.Scanner;
public class RentalDemo {
public static void main(String[] args) {
Rental object1 = new
Rental();
Rental object2 = new Rental();
Rental object3 = new Rental();
// Create array of rentals with size 3
Rental rentals[] = new Rental[3];
// Store all the three objects into array
rentals[0] = object1;
rentals[1] = object2;
rentals[2] = object3;
String contractNumber;
int hours;
int minutes;
Scanner input = new Scanner(System.in);
System.out.println("RENTAL 1:
");
System.out.println("Enter contract
number: ");
contractNumber =
input.nextLine();
System.out.println("Enter rental
hours: ");
hours = input.nextInt();
System.out.println("Enter rental
minutes: ");
minutes = input.nextInt();
object1.setContractNumber(contractNumber);
object1.setRentalTimeHours(hours);
object1.setRentalTimeMins(minutes);
System.out.println("RENTAL 2:
");input.nextLine();
System.out.println("Enter contract
number: ");
contractNumber =
input.nextLine();
System.out.println("Enter rental
hours: ");
hours = input.nextInt();
System.out.println("Enter rental
minutes: ");
minutes = input.nextInt();
object2.setContractNumber(contractNumber);
object2.setRentalTimeHours(hours);
object2.setRentalTimeMins(minutes);
System.out.println("RENTAL 3:
");input.nextLine();
System.out.println("Enter contract
number: ");
contractNumber =
input.nextLine();
System.out.println("Enter rental
hours: ");
hours = input.nextInt();
System.out.println("Enter rental
minutes: ");
minutes = input.nextInt();
object3.setContractNumber(contractNumber);
object3.setRentalTimeHours(hours);
object3.setRentalTimeMins(minutes);
displayMotto();
System.out.println();
displayobject1(object1);
displayobject2(object2);
displayobject3(object3);
System.out.println("Get largest rental between event #
1 and event # 2 is :");
displayobject1(getRentalWithLongerTime(object1,
object2));
System.out.println("Get largest rental between event #
2 and event # 3 is :");
displayobject2(getRentalWithLongerTime(object2,
object3));
System.out.println("Get largest rental from event # 1
and event # 3 is :");
displayobject3(getRentalWithLongerTime(object1,
object3));
System.out.println("");
int totalMins;
for (int r = 0; r < rentals.length; r++) {
// get the total mintues until
>=60 and <= 7200
do {
System.out.println("RENTAL " + (r+1) + ": ");
System.out.println("Enter the total time of the rental
(in minutes): ");
totalMins = input.nextInt();
// Display error only when there is error
if(!(totalMins >= 60 && totalMins <=
7200)) {
System.out.println("The rental time
must be between 60 minutes and 7,200 minutes");
}
} while(!(totalMins >= 60 && totalMins
<= 7200));
int
totalRentalMints = (rentals[r].getRentalTimeHours() * 60) +
rentals[r].getRentalTimeMins();
while(totalRentalMints >= totalMins) {
System.out.println("");
System.out.println("$" +
(rentals[r].calculateRentalPrice() * .10) + " Off");
System.out.println("!!!!! This coupon is good for 10
percent off your next rental !!!!!");
totalRentalMints -= totalMins;
}
}
}
private static void displayobject1(Rental object1)
{
System.out.println(
"Rental Contract number: " +
object1.getContractNumber() + ", Time: " +
object1.getRentalTimeHours()
+ "h+" +
object1.getRentalTimeMins() + "m, Price: $" +
object1.calculateRentalPrice());
}
private static void displayobject2(Rental
object2){
System.out.println("Rental Contract number:
"+object2.getContractNumber()+
", Time:
"+object2.getRentalTimeHours()+"h+"
+object2.getRentalTimeMins()+"m, Price:
$"+object2.calculateRentalPrice());
}
private static void displayobject3(Rental object3)
{
System.out.println(
"Rental Contract number: " +
object3.getContractNumber() + ", Time: " +
object3.getRentalTimeHours()
+ "h+" +
object3.getRentalTimeMins() + "m, Price: $" +
object3.calculateRentalPrice());
}
public static Rental getRentalWithLongerTime(Rental
rental1, Rental rental2) {
double totalTimeRental1 =
rental1.getRentalTimeHours() + rental1.getRentalTimeMins() /
60;
double totalTimeRental2 =
rental2.getRentalTimeHours() + rental2.getRentalTimeMins() /
60;
if (totalTimeRental1 >=
totalTimeRental2)
return
rental1;
else
return
rental2;
}
public static void displayMotto() {
System.out.println();
System.out.println("Motto");
}
}
Rental.java:
public class Rental {
private String contractNum;
private int rentalTimeHours;
private int rentalTimeMins;
public Rental() {
contractNum = "";
rentalTimeHours = 0;
rentalTimeMins = 0;
}
public String getContractNumber() {
return contractNum;
}
public void setContractNumber(String
contractNumber) {
this.contractNum =
contractNumber;
}
public int getRentalTimeHours() {
return rentalTimeHours;
}
public void setRentalTimeHours(int rentalTimeHours)
{
this.rentalTimeHours =
rentalTimeHours;
}
public int getRentalTimeMins() {
return rentalTimeMins;
}
public void setRentalTimeMins(int rentalTimeMins)
{
this.rentalTimeMins =
rentalTimeMins;
}
public double calculateRentalPrice() {
int pricePerHour = 40;
int pricePerMinute = 1;
int minutesThatCount;
if (this.getRentalTimeMins() <=
40) {
minutesThatCount
= this.getRentalTimeMins();
} else {
minutesThatCount
= 40;
}
int HoursPrice =
this.getRentalTimeHours() * pricePerHour;
int MinutesPrice = minutesThatCount
* pricePerMinute;
int totalRentalPrice = HoursPrice +
MinutesPrice;
return totalRentalPrice;
}
}
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************