1

my instructions on the project were as followed:

Instructions: Use a sentinel value loop. To create a basic Rental Car Calculator

Ask each user for:

Type of vehicle (May use something other than strings, such as: 1 for an economy, 2 for a sedan, etc.) Days rented Calculate the (For each customer):

Rental cost, Taxes, Total Due. There are three different rental options with separate rates: Economy @ 31.76, sedan @ 40.32, SUV @ 47.56. [Note: only whole day units to be considered (no hourly rates)].

Sales tax is = to 6% on the TOTAL.

Create summary data with:

Number of customers Total money collected. Also, Include IPO, algorithm, and desk check values (design documents).

{WHAT I HAVE GOING AND MY QUESTION(S)}

package tests;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Tester {

public static void main(String []args){
int count=0;
int days;
int cus = 10; 
double DailyFee=0, NontaxTotal, CarType, Total,FullTotal=0;
boolean F1 = false, F2 = false, F3 = false;
Scanner in=new Scanner(System.in);


while (F3 == false) {
    F3 = true;
    System.out.print("Press 1 to enter Rental Calculator or else press 0 to quit\n");
    System.out.println("Please only enter 1 or 0. Also, please only enter number(s) not letter(s)");
    try {
        cus=in.nextInt();
        if (cus == 0 || cus == 1) {
            F3 = true;
        } else {
            F3 = false;
            System.out.println("Number must be either 1 or 0");
        }
    } catch (InputMismatchException ex) {
        F3 = false;
        System.out.println("Invalid entry");
        in.next();
    }
}

    if(cus == 1) { 
        while(F1 == false) {
            F1 = true;
            count++;
            System.out.print("What vehical would you like to rent?\n");
            System.out.println("Enter 1 for an economy car");
            System.out.println("Enter 2 for a sedan car");
            System.out.println("Enter 3 for an SUV");
            // 
            try {
                CarType = in.nextInt();
                if (CarType <= 0 || CarType >= 4) {
                    System.out.print("Number must be 1-3\n");
                    System.out.println("Please enter 1 for an economy car");
                    System.out.println("Enter 2 for a sedan car");
                    System.out.println("Enter 3 for an SUV");

                    F1 = false;
                } else {
                     if (CarType == 1) {
                         F1 = true;
                          DailyFee=31.76;
                } else if(CarType == 2) {
                        F1 = true;
                          DailyFee=40.32;
                } else if(CarType == 3) {
                        F1 = true;
                          DailyFee=47.56;
                }
                while (F2 == false) {
                    F2 = true;
                    try { 
                        System.out.print("Please enter the number of days rented. (Example; 3) : ");
                        days = in.nextInt();
                        if (days <= 0) {
                            System.out.println("Number of days must be more than zero");
                            F2 = false;
                        } else {

                            double x=days;
                            NontaxTotal = (DailyFee * x);
                            Total = (NontaxTotal * 1.06);
                            FullTotal+=Total;
                            F3 = true;

                        }
                    } catch(InputMismatchException ex) {
                        System.out.println("Answer must be a number");
                        F2 = false;
                        in.next();
                        }
                    }
                }
            } catch (InputMismatchException ex) {
                F1 = false;
                System.out.println("Answer must be a number"); 
            }
        }
    }
    in.close();
    System.out.println("Count of customers : " + count);
    System.out.printf("Total of the Day : $ %.2f", FullTotal);

    }
}

{MY QUESTIONS}

  1. When a letter is entered to the prompt "Press 1 to enter Rental Calculator or else press 0 to quit" it displays, an error prompt then the console asks for input again. Similarly, when a letter is inputted at the prompt "What vehicle would you like to rent?" the console continues to print lines with no stop? I do not know how to fix this?

  2. I want my program to allow multiple calculation inputs to be made. However, after full calculation input (Days * Tax * Car Type) the console post summary data rather than looping? 2a. After the prompt "Please enter the number of days rented. (Example; 3) : " and following user input. How would I get my program to loop back to asking "Press 1 to enter Rental Calculator or else press 0 to quit"? with still making 0 prompt my summary data?

  • what are F1, F2 and F3 (logically)? what value do they have? what inputs do you provide? .... ? – Stultuske Oct 30 '18 at 06:56
  • @Stultuske I did edit question a little so I hope that helps? F1, F2, and F3 were like checkpoints for the loops? (I'm new) they carry no value other than true/false. true allowing the loop to move onward and false causing the loop to reset when checkpoints weren't met. such as not entering 1 or 0, etc... – JavaNovi247 Oct 30 '18 at 07:16
  • @Stultuske the only input I need from the user is the 1. car type, 2. rental length and, 3. if there are multiple rental calculations to be made? The summary data needs to have 1. Total of all car(s) 2. The grand total of all car(s) [day *tax *cartype] – JavaNovi247 Oct 30 '18 at 07:16
  • what you are saying that businesswise their value is either true or false, but logically: shouldContinue, dataReadSuccessFully, errorOccured are logical what they might mean, and it's easier to read code. Yes, you may only need that input, but what is the input you provided when you ran the code? – Stultuske Oct 30 '18 at 07:39
  • @Stultuske I'm kinda confused won't lie but The input I entered is followed by a = sign and it was: " "Press 1 to enter Rental Calculator or else press 0 to quit" " = 1 [NEXT] "What vehicle would you like to rent?" = one (this is where i got my first error and question) – JavaNovi247 Oct 30 '18 at 07:50
  • I recommend you going over your code, and trying to simplify it. that way you'll find you have a number of pointless statements that are obsolete. you can also make your code a lot easier to read/maintain – Stultuske Oct 30 '18 at 07:52
  • why would you have an '=' in your input? if you enter anything but integers, it'll crash – Stultuske Oct 30 '18 at 07:55
  • @Stultuske I would not know what to take out without messing it up. The program runs fine despite the two questions/errors I have asked/occurred. Was putting the = sign to detail what was entered. everything after = was entered. – JavaNovi247 Oct 30 '18 at 07:55
  • for your enter type of vehicle, it keeps printing, because you don't have in.next(); in your catch block. – Stultuske Oct 30 '18 at 11:08

2 Answers2

0

I have just "refactored" your code a bit, removed some obsolete code and placed some other code on other locations. I've also used more clear naming for variables, and followed the naming conventions.

The problem you had, was that you did not in each catch block had a in.next(); meaning that while iterating, the variable kept using the same variable (which was invalid) so kept looping over the error messages.

Now this code is far from perfect, it can easily be improved still, but this should get you started.

package tests;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Tester {

    public static void main(String []args){
        int count=0;
        int days;
        int cus;
        int carType;
        double dailyFee=0, nonTaxTotal, total,fullTotal=0;
        boolean checkRunOrQuit = false, chooseTypeVehicle = false, numberOfDAysChosen = false;
        Scanner in=new Scanner(System.in);


        while ( !checkRunOrQuit ) {
            System.out.print("Press 1 to enter Rental Calculator or else press 0 to quit\n");
            System.out.println("Please only enter 1 or 0. Also, please only enter number(s) not letter(s)");
            try {
                cus=in.nextInt();
                switch ( cus ) {
                    case 0: System.out.println("End of application");
                            System.exit(0); // This will actually end your application if the user enters 0, no need to verify later on
                    break;
                    case 1: checkRunOrQuit = true;
                    break;
                    default:
                            System.out.println("Number must be either 1 or 0");
                }
            } catch (InputMismatchException ex) {
                System.out.println("Invalid entry: ");
                in.next();
            }
        }

        while( !chooseTypeVehicle ) { // --> simplified comparison
            count++;
            System.out.print("What vehical would you like to rent?\n");
            System.out.println("Enter 1 for an economy car");
            System.out.println("Enter 2 for a sedan car");
            System.out.println("Enter 3 for an SUV");

            try {
                carType = in.nextInt();
                chooseTypeVehicle = true;
                switch ( carType ) {
                    case 1: dailyFee = 31.76;
                    break;
                    case 2: dailyFee = 40.32;
                    break;
                    case 3: dailyFee = 47.56;
                    break;
                    default:
                        System.out.print("Number must be 1-3\n");
                        System.out.println("Please enter 1 for an economy car");
                        System.out.println("Enter 2 for a sedan car");
                        System.out.println("Enter 3 for an SUV");
                        chooseTypeVehicle = false;
                        break;
                }
            } catch (InputMismatchException ex) {
                System.out.println("Answer must be a number");
                in.next(); // -> you forgot this one.
            }
        }

        while ( !numberOfDAysChosen ) {
            try {
                System.out.print("Please enter the number of days rented. (Example; 3) : ");
                days = in.nextInt();
                if (days <= 0) {
                    System.out.println("Number of days must be more than zero");
                } else {
                    nonTaxTotal = (dailyFee * days);
                    total = (nonTaxTotal * 1.06);
                    fullTotal+=total;
                    numberOfDAysChosen = true;
                }
            } catch(InputMismatchException ex) {
                System.out.println("Answer must be a number");
                in.next();
            }
        }
        in.close();
        System.out.println("Count of customers : " + count);
        System.out.printf("total of the Day : $ %.2f", fullTotal);
    }
}
Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • Thank you, I still can not figure out how to get it to loop back to the beginning? After the user has inputted the 'days rented' I can not figure out how to enable the "while ( !checkRunOrQuit ) {" without messing up other loops? – JavaNovi247 Oct 30 '18 at 18:22
  • You can't. you can, however, put the closing bracket of that first while loop so that that while block encloses the rest of the code – Stultuske Oct 31 '18 at 06:50
-1

Here you go, i modified it a bit and put the whole thing in a while loop (while (cus != 0)) now it is working perfectly, try this code and do let me know if you have questions

package inter;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Inter {

    public static void main(String []args){
    int count=0;
    int days;
    int cus = 10; // added
    double DailyFee=0, NontaxTotal, CarType, Total,FullTotal=0;
    boolean F1 = false, F2 = false;
    Scanner in=new Scanner(System.in);

    while (cus != 0) {

        while (true) {  
            System.out.println("If there are any customer press 1 else press 0");
        try {           
            cus=in.nextInt();
            if (cus == 0 || cus == 1) {  
                break;
            } else {
                System.out.println("Number must be either 1 or 0");
            }
        } catch (InputMismatchException ex) {
            System.out.println("Invalid entry");
            in.next();
        }
    }

        if(cus == 1) {           
            while(F1 == false) {
                F1 = true;
                count++;
                System.out.print("What vehical would you like to rent?\n");
                System.out.println("Enter 1 for an economy car");
                System.out.println("Enter 2 for a sedan car");
                System.out.println("Enter 3 for an SUV");
                try {
                    CarType = in.nextInt();
                    if (CarType <= 0 || CarType >= 4) {
                        System.out.print("Number must be 1-3\n");
                        System.out.println("Please enter 1 for an economy car");
                        System.out.println("Enter 2 for a sedan car");
                        System.out.println("Enter 3 for an SUV");
                        F1 = false;
                    } else {
                         if (CarType == 1) {
                             F1 = true;
                              DailyFee=31.76;
                    } else if(CarType == 2) {
                            F1 = true;
                              DailyFee=40.32;
                    } else if(CarType == 3) {
                            F1 = true;
                              DailyFee=47.56;
                    }
                    while (F2 == false) {
                        F2 = true;
                        try { 
                            System.out.print("Please enter the number of days rented. (Example; 3) : ");                           
                            days = in.nextInt();
                            if (days <= 0) {
                                System.out.println("Number of days must be more than zero");
                                F2 = false;
                            } else {
                                //days = in.nextInt();
                                double x=days;
                                NontaxTotal = (DailyFee * x);
                                Total = (NontaxTotal * 1.06);
                                FullTotal+=Total;
                            }
                        } catch(InputMismatchException ex) {
                            System.out.println("Answer must be a number");
                            F2 = false;
                            in.next();
                            }
                        }
                    F2 = false;
                    }
                } catch (InputMismatchException ex) {
                    F1 = false;
                    System.out.println("Answer must be a number"); 
                    in.next();
                }
            }
            F1 = false;
        }
    }
    System.out.println("Count of customers : " + count);
    System.out.printf("Total of the Day : $ %.2f", FullTotal);
    }
}
Zeyad
  • 537
  • 2
  • 7
  • 15
  • How is this an answer? It provides no explanation what-so ever, and the original error for which this question was created is still in it. – Stultuske Oct 31 '18 at 06:53
  • Hi @Stultuske Why ?, i tried it and it worked, it solved the error and about the explanation, all what the user needs is the (while (cus != 0)), and the while(true) & break; could be replaced with a boolean & an if-statement to check, like all others but i took it out for simplicity, but it really works good, did you try it to see if it gives the error, maybe i am mistaken. – Zeyad Oct 31 '18 at 14:22
  • yes, I did run the code. the first of the questions gave me the exact same issue as the original code. – Stultuske Nov 05 '18 at 07:41
  • Hi @Stultuske , I am pretty sure, i ran it and it didn't give me the same issue, well let me try it again when i go back, maybe i am mistaken. I didn't hear from the asker if it worked though, thank you for keeping up with me. – Zeyad Nov 05 '18 at 14:39
  • @Stultuske , I am sorry but i ran it again, and i couldn't see an issue at all, can you please elaborate more on what issue exactly are you facing with this code ? thank you. – Zeyad Nov 07 '18 at 02:31
  • just check the question and what the OP provided as input. – Stultuske Nov 07 '18 at 06:49
  • more specifically, this part: "Similarly, when a letter is inputted at the prompt "What vehicle would you like to rent?" the console continues to print lines with no stop? I do not know how to fix this?" – Stultuske Nov 07 '18 at 06:55
  • @Stultuske you are absolutely right, i was missing an in.next(), this is why the console kept printing after a letter is added. I fixed it and edited the answer, just added the in.next() and now it is working good i guess. I hope the asker would check this edit. Thank you very much for correcting me, i really appreciate that. – Zeyad Nov 07 '18 at 16:14