In this rental program I need to display the user with maximum and minimum rents. The program works fine for other methods, but it displays maximum value for both max and min. Here's the code I have written:
import java.util.Scanner; // program uses Scanner
public class CarRentalTest {
public static void main(String[] args) {
System.out.println("Welcome to Rental Portal");
Scanner input = new Scanner(System.in); // create Scanner to obtain
// input from command window
CarRental details = new CarRental();
int N = 8; // Total number of passengers
String summary =
"\t\t\t Summary of Car Rentals \t\n"
+ "\t\t\t========================================\n"
+ "\tName \t\t Days \t\t Special Offer \t\t Charge \n";
double maxRent = 0.0;
double minRent = 0.0;
double temp = 0.0;
int count = 0;
int count1 = 0;
int days = 0;
String high = "";
String low = "";
for (int i = 0; i <= 2; i++) {
details = new CarRental();
System.out.println("Enter the name of Passenger: ");
details.passenger = input.next(); // read customer name
details.setName(details.passenger);
System.out.println("Enter Number of days you wish to rent a car: ");
details.days = input.nextInt(); // read number of days
if (days > 365) {
System.out.print("\nNumber of Days must not exceed 365");
System.out.print("\nPlease enter number of days again: ");
details.days = input.nextInt();
details.setDays(details.days);
}
details.getRent();
System.out.println("Do you want to use the special offer?");
details.userResponse = input.next();
System.out.println("The total amount of rent for "
+ details.getName() + " is: $" + details.getRent());
summary = summary + "\t" + details.getName() + " \t\t "
+ details.getDays() + " \t\t " + details.userResponse
+ " \t\t\t $" + details.getRent() + "\n";
System.out
.println("-----------------------------------------------");
}
System.out.println(summary + "\n");
System.out.println("-------------------------------------------------");
for (int i = 0; i < N; i++) {
do {
details.cost = details.getRent();
if (minRent > details.cost)
minRent = details.cost;
if (maxRent < details.cost)
maxRent = details.cost;
} while (details.cost == 0);
}
System.out.println("The customer spending most rental is" + high + " "
+ maxRent);
System.out.println("The customer spending least rental is" + low + " "
+ minRent);
System.out.println();
for (int i = 0; i < N; i++) {
if (details.days < 7) {
count = count + 1;
} else {
count1 = count1 + 1;
}
}
System.out.print("The rental days < 7: ");
for (int j = 0; j < count; j++) {
System.out.print("*");
}
System.out.println();
System.out.print("The rental days >= 7: ");
for (int j = 0; j < count1; j++) {
System.out.print("*");
}
}
}// end method main
I have added a do-while loop and it calculates the max rent but doesn't calculate the min value. Any help will be much appreciated.
details = new CarRental();every time previous data is overwritten. Try to store it in list, check if all data is stored correctly, then just show us the logic for finding max and min, write now its lot of code to read and understand - Shrikant Havale