I can't figure out how to add taxes

I can't figure out how to add the taxes to the berry banana price. Would I need to use math.pow or is there a specific way to add fraction results to an decimal number? Probably such a silly question but I can't figure out it, my teacher wants me to use constants for everything. Please let me know if there's multiple ways to do this.

public static void main(String[] args) {
    // TODO Auto-generated method stub

    final double BERRY_BANANA_PRICE = 7.50;
    final double TROPICAL_PRICE = 6.75;
    final double GREEN_JOLT_PRICE = 5.00;
    final double TAX_RATE = 8.25 / 100;
    final double ADD_IN_PRICE = 1.50;

    Scanner input = new Scanner(System.in);

    System.out.println("CS1150 Beach Smoothie Bar!");
    System.out.println("Option   Type           Price");
    System.out.printf("1        Berry Banana   $%.2f\n", + BERRY_BANANA_PRICE);
    System.out.printf("2        Tropical    $%.2f\n", + TROPICAL_PRICE);
    System.out.printf("3        Green Jolt  $%.2f\n", + GREEN_JOLT_PRICE);


    System.out.print("Select a smoothie: 1, 2, or 3:");
    int selectasmoothie = input.nextInt();


    if (selectasmoothie >= 1 && selectasmoothie <= 3) {

        System.out.println("Option   Add-In              Price");
        System.out.println("0        No add-in       $0.00");
        System.out.printf("1        Almond Butter       $%.2f\n", + ADD_IN_PRICE);
        System.out.printf("2        Lime juice       $%.2f\n", + ADD_IN_PRICE);


        System.out.print("Select an add-in: ");
        int selectanaddin = input.nextInt();
        if (selectanaddin >= 0 && selectanaddin <= 1) {

            System.out.printf("Berry Banana Smoothie  $%.2f\n", BERRY_BANANA_PRICE);
            System.out.printf("Taxes                  $%.2f\n", BERRY_BANANA_PRICE * TAX_RATE);
            System.out.printf("Total Cost             $%.2f\n",BERRY_BANANA_PRICE +  );
        } else {
                    .printf(selectanaddin + " " + "is not a valid menu item. Please run program again, good bye!");
}// main

1 answer

  • answered 2020-02-13 00:30 Jonas Fagundes

    1. Separate your business logic from you view logic (calculate all your variable and just print them later), it will make your code easier to read and maintain.

    2. You have 3 kinds of smoothies (plus add-ins), save the value of the smoothie (plus add-ins) in a variable called totalPrice.

    3. System.out.printf("1 Berry Banana $%.2f\n", + BERRY_BANANA_PRICE); will not compile, you need a variable before the +. The same mistake is in several lines (probably just a copy and paste error).

    4. To increase an amount by a percentage use value * (1 + percentage) (- if you want to decrease it, like making a discount).

    5. It is not advisable to use floating points to represent money because it can introduce rounding errors, use BigDecimal instead.