0
votes

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.

2
I guess you are overwriting the data for your 3 customers, in the first for loop, 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
Thanks. I have removed the statement details = new CarRental(); and added a do-while loop but still it doesn't work. - Fareeha

2 Answers

0
votes

For starters, you are retrieving the values of minRent and maxRent from the same details.getRent() method.... therefore they come out as the same..

for (int i = 0; i < N; i++) {
        if (maxRent < details.getRent()) {
            maxRent = details.getRent();
            high = details.getName();
        }
        if (i == 0) {
            temp = details.getRent();
        }
        if (temp >= details.getRent()) {
            minRent = details.getRent();
            temp = details.getRent();
            low = details.getName();
        }
        if (maxRent < details.getRent()) {
            maxRent = details.getRent();
            high = details.getName();
        }
    }
0
votes

Thanks @TheJavaCoder16 the code is absolutely correct except the for-loop in question was placed below the summary.. Here's the corrected code.

    import java.util.Scanner; // program uses Scanner
    public class CarRentalTest {
    public static void main( String[] args )
    {
        System.out.println("\t\t ******Welcome to Rental Portal******");
        Scanner input = new Scanner( System.in );
        CarRental details=new CarRental(); // create a CarRental object
        int N=3; // Total number of passengers
        String summary="\t\t\t Summary of Car Rentals \t\n"+"\t\t========================================\n"+"\tName \t\t Days \t\t Special Offer \t\t Charge \n";
        double maxRent=0.0;//to display maximum rent value
        double minRent=0.0; //To display minimum rent value
double temp=0.0; // A temp variable to compare minimum and maximum rent values
  int count1=0; // Variable to count days 
  int count2=0; //Variable to count days
  String high=""; // Displays user with highest rent 
  String low=""; // Displays user with least rent

  for(int i=0;i<N;i++){ //Reading data from Scanner and Calculating rent

      System.out.println( "Enter the name of Passenger: " ); // prompt for input
      details.passenger = input.next(); // read customer name
      details.setName(details.passenger); //set passenger name

      System.out.println( "Enter Number of days you wish to rent a car: " ); // prompt for input
      details.days = input.nextInt(); // read number of days
          if(details.days>365){
           // if user enters invalid number of days ask for entering again
           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(); //calculate rent
      System.out.println( "Do you want to use the special offer?" ); // prompt for input
      details.userResponse = input.next();// read user response and based on that calculate rent
      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("-----------------------------------------------");

    //Calculate the maximum and minimum rent values
      // and the associated passenger names
      for(int j=0;j<=N;j++){ 
        if(maxRent < details.getRent()){
            maxRent = details.getRent();
            high = details.getName();
        }

        if(i==0){
            temp = details.getRent() ;
        }
        if(temp >= details.getRent()){
            minRent = details.getRent();
            temp = details.getRent();
            low = details.getName();
        }

        if(maxRent < details.getRent()){
            maxRent = details.getRent();
            high = details.getName();
        }
    }
  }
System.out.println(summary+"\n");
System.out.println("-------------------------------------------------");
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();
}
}//end method main