mardi 30 octobre 2018

Q: Doing multiple loops and multiple if-statements and if-else-statements | RENTAL CAR CALCULATOR PROJECT

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:

The 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;
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);
    }
}

  1. How would I make this program loop back to prompting the user: "Press 1 to enter Rental Calculator or else press 0 to quit\". After the "days rented input is entered?

[Note: Once the days rented is input, I was wanting a total calculation but not a summary. However, I want the summary info when the program is exited.]

Aucun commentaire:

Enregistrer un commentaire