lundi 29 octobre 2018

Q : Not understanding loop process? or possible if statements?

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 numbers. for instance, not allowing input of 3.1, 2.35, 0.35 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);
}

}

Aucun commentaire:

Enregistrer un commentaire