0

I am working on a project that involves creating a rental car calculator.

What I am trying to do is make it to where when asked: "What vehicle would you like to rent??". If a number that is not between 1-3 is entered when the user is prompted this, then I want the program to loop back to the point of being asked vehicle type again.

Similarly, when prompted for 'Please enter the number of days rented. (Example; 3) : ' I want to only allow the user to input whole positive numbers. for instance, not allowing input of 3.1, 2.35, 0.35 -2 and, etc...

here is what I have written and my attempt at these questions :

package inter;

import java.util.Scanner;

public class Inter {
    public static void main(String []args){
        int count=0;
        int days;
        double DailyFee=0, NontaxTotal, CarType, Total,FullTotal=0;
        Scanner in=new Scanner(System.in);
        System.out.println("If there are any customer press 1 else press 0");
        int cus=in.nextInt();

        while(cus!=0){
            count++;
            System.out.print("What vehical would you like to rent?\n");
            System.out.println("Enter 1 for an economy car\n");
            System.out.println("Enter 2 for a sedan car\n");
            System.out.println("Enter 3 for an SUV");
            CarType = in.nextInt();
            if (CarType == 1) {
                  DailyFee=31.76;
            }
            else if(CarType == 2) {
                  DailyFee=40.32;
            }
            else if(CarType == 3) {
                  DailyFee=47.56;
            }
            else if(CarType <= 0) {
                System.out.println("input is not a positive Integer ");
                System.out.println("Please enter a positive integer value: ");
                cus = 0; }
            else if(CarType > 4) {
                System.out.println("input is not a positive Integer ");
                System.out.println("Please enter a positive integer value: ");
                cus = 0; }

            System.out.print("Please enter the number of days rented. (Example; 3) : ");
            days = Integer.valueOf(in.nextLine());
            double x=days;
            NontaxTotal = (DailyFee * x);
            Total = (NontaxTotal * 1.06);
            FullTotal+=Total;

            System.out.printf("The total amount due is $ %.2f \n",Total);

            System.out.println("If there are any customer press 1 else press 0");
            cus=in.nextInt();
        }
        System.out.println("Count of customers : "+count);
        System.out.printf("Total of the Day : $ %.2f",FullTotal);
    }   
}
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
  • `do { //ask for and assign input; } while(CarType < 0 || CarType > 3);` – GBlodgett Oct 29 '18 at 19:02
  • @GBlodgett thank you this fixed my issue with allowing only input 1-3 for vehicle type. Anyway, would you know any tips to how I could only allow the input of 1 or 0 when prompted: "If there are any customer press 1 else press 0" anytime I try tweaking it i mess the loop up that i have that presents the summary data if 0 is entered.? – JavaNovi247 Oct 29 '18 at 19:27

1 Answers1

0

Let me help you with this, I made this code for you, i tried it and it worked this will check if both answers were whole numbers (integers) and more than zero and will also check if the answer was numeric in the first place so that if the user answered with letters he will be prompted to try again This is my suggestion: basically i used the try-catch block with InputMismatchException to detect if the answer was not an integer (whole number ) or was not numeric at all, every time a mistake is detected i flip a boolean to false and keep looping as long as this boolean is false (i flip the boolean back to true before checking otherwise once the user gives a wrong answer he will always be prompted to answer even if he gave a correct answer after)

    int vehicleType;
    int numberOfDays;
    double dailyFee;
    boolean validAnswer1 = false;
    boolean validAnswer2 = false;

    Scanner scan = new Scanner(System.in);

    while (validAnswer1 == false) {
        validAnswer1 = true;
        System.out.println("Choose Vehicle Type");
        try {
            vehicleType = scan.nextInt();
            if (vehicleType <= 0) {
                System.out.println("Number must be more than zero");
                validAnswer1 = false;
            } else if (vehicleType >= 4) {
                System.out.println("Number should be from 1 to 3");
                validAnswer1 = false;
            } else {
                if (vehicleType == 1) {
                    dailyFee=31.76;
                } else if(vehicleType == 2) {
                    dailyFee=40.32;
                }else if(vehicleType == 3) {
                    dailyFee=47.56;
                }
                while (validAnswer2 == false) {
                    validAnswer2 = true;
                    try {
                        System.out.println("Enter number of days rented ?");
                        numberOfDays = scan.nextInt();
                        if (numberOfDays <= 0) {
                            System.out.println("Number of days must be more than zero");
                            validAnswer2 = false;
                        } else {
                            // calculate your rent total here
                        }
                    } catch(InputMismatchException ex) {
                        System.out.println("Answer must be an Integer");
                        validAnswer2 = false;
                        scan.next();
                    }
                }                   
            }
        } catch (InputMismatchException ex) {
            validAnswer1 = false;
            System.out.println("Answer must be an Integer");     
            scan.next();
        }           
    }

Hope this was useful, do let me know if you still need help

Zeyad
  • 537
  • 2
  • 7
  • 15
  • Thank you @Zeyad, I do have to go to class right now, but will be back online tonight or tomorrow. If I have any other questions should I post as a comment or a new answer? I'm kinda new here sorry. Also, was thinking of posting work when finished so people can review? is this a good thought? – JavaNovi247 Oct 29 '18 at 20:05
  • @JavaNovi247 Hi! Welcome to SO. If you have another question, [ask a new one](https://stackoverflow.com/questions/ask), providing the link to this question in the body if it helps provide context. Code reviews should be posted to [codereview.se]. – grooveplex Oct 30 '18 at 00:42
  • @JavaNovi247, your welcome if you get a question answered then mark the answer (using the tick next to it) that satisfied or worked for you as accepted, so that it shows that your question was answered. then you ask another question as another post, because, people here usually search first for some one having a similar question and they look if it has been answered, if not then they ask. So your questions as different posts would help others to look up more questions. beware there is a limit though for how many questions to ask within an interval depending on your score i guess. – Zeyad Oct 30 '18 at 01:44
  • @Zeyad This is what I have done but it is still not fully working and I don't know why? when prompted for car type the program does nothing even if 1-3 is entered? – JavaNovi247 Oct 30 '18 at 01:56
  • @Zeyad I dont know where to post my work to show you what i have ? – JavaNovi247 Oct 30 '18 at 01:58
  • @JavaNovi247, the code doesn't appear in the comment, I honestly don't know because i'm quite new too but from what grooveplex says, i think you will somehow post it in here, but this is a different community i think https://codereview.stackexchange.com/ usually if the code doesn't work i would just post a question again here on stackoverflow just as you did – Zeyad Oct 30 '18 at 02:05
  • @Zeyad https://codereview.stackexchange.com/questions/206544/q-loops-along-with-if-statements-else-if-statements-rental-car-calculator (This is what I have done with your help) my questions are reposted on that community if it helps with the clarity of issues I'm currently having. – JavaNovi247 Oct 30 '18 at 02:17
  • @JavaNovi247, ok i am checking it now – Zeyad Oct 30 '18 at 02:29
  • @JavaNovi247, sorry i don't have enough reputation in the codereview community so i cannot comment there,i can just answer, i already see the error but regarding the customer 1 or 0 What would you like to happen if the user entered another number that is not 1 or 0 or entered an invalid entry (like a word) would you like to prompt him again or just set it to 0 in that case – Zeyad Oct 30 '18 at 02:33
  • @Zeyad Regarding the case of "the customer 1 or 0" I want the user to be prompted until either a 1 or 0 is entered. 0 = exit. 1 = start the calculator. – JavaNovi247 Oct 30 '18 at 02:46
  • @Zeyad I wouldn't mind you posting the answer on here and I will try to edit that question with a link to here. Also with an upvote or whatever I can do to help you, as you have helped me immensely already. – JavaNovi247 Oct 30 '18 at 02:48
  • @JavaNovi247, thank you very much i appreciate it because i still have low score too, i am not sure if you can upvote, but i think you can tick this question or the other one as answer satisfied, i don't know anyway i solved it and i posted the answer on your question on codereview rather than here so that people can benefit from the answer to the question there too as i can answer there but don't have enough points yet to comment there – Zeyad Oct 30 '18 at 02:59
  • @Zeyad The one you sent me, It still just pulls the summary after one input. it does not loop for multiple entries and idk how to fix it. – JavaNovi247 Oct 30 '18 at 05:40
  • @Zeyad I did pretty much just reask this question, https://stackoverflow.com/questions/53058847/q-doing-multiple-loops-and-multiple-if-statements-and-if-else-statements-re – JavaNovi247 Oct 30 '18 at 07:21
  • @JavaNovi247 yes i got you now, sorry for misunderstanding, so everything else is now working but on pressing 1 it doesn't loop yes you are right, ok i see that someone answered you already, check if his answer works or not and let me know, if it doesn't i will try to work on it today later – Zeyad Oct 30 '18 at 14:35
  • @JavaNovi247 Ok done, Check it on your other question, This should be fully working, do let me know if you have questions – Zeyad Oct 30 '18 at 15:40
  • @JavaNovi247 did my solution on your other question work as required ? – Zeyad Nov 01 '18 at 00:13