简体   繁体   中英

Java Lemonade Calculator

Assignment is to:

  • Display any welcome message at the top of the output screen

  • Create variables to hold the values for the price of a cup of lemonade.

  • Display the price per glass.

  • Ask the user for their name, and store it as a String object. Refer to the user by name, whenever you can.

  • Ask the user how many glasses of lemonade they would like to order. Save this as a variable with the appropriate data type.

  • Store the San Diego tax rate of 8% as a constant variable in your program.

  • Calculate the subtotal, total tax, and total price, and display it on the screen.

  • Ask the user how they would like to pay for the lemonade, and save the input as a char variable.

  • Ask the user to enter either 'm' for money, 'c' for credit card, or 'g' for gold

  • Using the DecimalFormat class, make all currency data printed to the screen display 2 decimal places, and also a '$" sign.

Need help figuring out how to get tax rate of 8% as a constant variable in my program that way I can calculate the subtotal, total tax, and total price, and display it on the screen

So far this is what I have:

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class FirstProgram {

    public static void main(String[] args) {
        double cost = 7.55;
        double amount = 7.55;
        final double CA_SALES_TAX = 0.08; 
        int tax, subtotal, total;
        subtotal = (int) (amount * cost);
        tax = (int) (subtotal * CA_SALES_TAX);
        total = tax + subtotal;

        Scanner input = new Scanner(System.in);
        double fnum = 7.55, tax1 = fnum * 0.08, answer = tax1 + fnum; 
        System.out.println("Welcome to the best Lemonade you'll ever taste! ");

        System.out.println("My lemonade would only cost you a measly: $" + amount);

        System.out.println("What is your name?");

        String first_name;
        first_name = input.nextLine();

        System.out.println("Hi " +first_name+ ", how many glasses of lemonade would you like?");
        fnum = input.nextDouble();
        System.out.println("Subtotal: $" + (amount * fnum));

        System.out.println("Tax:    $" + (tax1 * CA_SALES_TAX));
        tax1 = input.nextDouble();

Any help is appreciated

It looks like you already have the sales tax set as constant that is what the "final" keyword is being used for. As for your code i see some redundancies and am not sure as to why you are casting to integers. I made some mods for what I think you want it to do.

public static void main(String[] args) {
    double cost = 7.55;
    final double CA_SALES_TAX = 0.08;
    double subtotal,tax,total;

    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to the best Lemonade you'll ever taste! ");
    System.out.println("My lemonade would only cost you a measly: $" + cost);

    System.out.println("What is your name?");
    String first_name = input.nextLine();

    System.out.println("Hi " +first_name+ ", how many glasses of lemonade would you like?");
    int fnum = input.nextInt();

    //calc subtotal, tax, total 
    subtotal = fnum * cost;
    tax = subtotal *CA_SALES_TAX; 
    total = tax + subtotal;

    // print them all out 
    System.out.println("Subtotal: $" + (subtotal));
    System.out.println("Tax: $" + (tax));
    System.out.println("Total Price: $" + (total));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM