简体   繁体   中英

How can I get my Java program to output correctly using a final double and multiple discounts?

I am writing a Java program and I want to use a switch, but there is a set price or final double and multiple discounts. This is frustrating and I don't want to have to cast Int's either.

For full disclosure - I am a 37 yr old student, so any help would be great. Here is what I have. On a side note using if else if statements was easy, but this is a different animal with so many variable discounts. The code prints the total, but ignores all of the cases and falls through to default.

package example;

import java.util.Scanner;

public class AcmeShirtsSwitch {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        final double SHIRT_COUNT = 19.95;

        double shirt = SHIRT_COUNT;

        // Get number of shirts purchased.
        System.out.println("Enter the number of shirts "
                + "you have purchased to see your discount. ");

        int shirts = input.nextInt();


        int discount = (int)(shirts * shirt);
        switch (discount) {

        case 1:
            discount = (int) .40;
            break;
        case 2:
            discount = (int) .30;
            break;
        case 3:
            discount = (int) .20;
            break;
        case 4:
            discount = (int) .10;

        default:
            discount = 0;
            break;
        }
        double finalDiscount = (shirt * shirts) * (1.0 - discount);

        System.out.println("Your discount is " + discount);
        System.out.println("Your total is " + finalDiscount);
    }
}

This code has several issues:

  • variable names: try to find variable names that are telling a bit more about their purpose. helps a lot to understand the code.
  • (int) (shirt * shirts) will always be either 0 or atleast 19, so the switch(discount) doesn't make a lot of sense.
  • this kind of cast (int) .40 , same for .30 and all other doubles in the switch will always result in 0.
  • assigning SHIRT_COUNT , (which should be the price, i guess, see 1. point) to shirt is senseless, since shirt is never overwritten. Simply use SHIRT_COUNT directly instead.
  • the discount in the switch follows one simple rule:

     if(discount < 4) discount = .50 - 0.1 * discount; else discount = 0; 

Just to cover the most important issues.

Since shirt is 19.95, multiplying it by any integer will never result in 1, 2, 3 or 4. Instead of using the discount variable, try switching on shirts : this already contains the amount of shirts you're going to buy and can actually be in [1, 4] range.

double discount;
    switch (shirts) {

    case 1:
        discount = .40;
        break;
    case 2:
        discount = .30;
        break;
    case 3:
        discount = .20;
        break;
    case 4:
        discount = .10;

    default:
        discount = 0;
        break;
    }

Doubles are not exact enough for prices, check out BigDecimal instead.

(Also, it doesn't make much sense to give the biggest discount for the lowest possible amount of shirts, does it?)

Here is the solution I figured out to my problem. It is not perfect ie decimal places, but it works.

package example;

import java.util.Scanner;

public class AcmeShirtsSwitch {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    final double SHIRT_PRICE = 19.95;

    double shirt = SHIRT_PRICE;

    // Get number of shirts purchased.
    System.out.println("Enter the number of shirts "
            + "you have purchased to see your discount. ");

    int shirts = input.nextInt();

    double discount;
    switch (shirts) {

    case 1:
        discount = 0.0;
        break;
    case 2:
        discount = 0.0;
        break;
    case 3:
        discount = .10;
        break;
    case 4:
        discount = .10;
        break;
    case 5:
        discount = .20;
        break;
    case 6:
        discount = .20;
        break;
    case 7:
        discount = .20;
        break;
    case 8:
        discount = .30;
        break;
    case 9:
        discount = .30;
        break;
    case 10:
        discount = .30;
        break;
    case 11:
        discount = .30;
        break;
    case 12:
        discount = .40;
        break;
    default:
        discount = .40;
        break;

    }
    double finalDiscount = (shirt * shirts) * (1.0 - discount);

    System.out.println("Your discount is " + discount);
    System.out.println("Your total is " + finalDiscount);

    System.exit(1);

}

}

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