The program runs perfectly, however I am having an issue adding a discounted price. Another thing I noticed when I run the program is that the switch statement doesn't calculate the discounts at all.

// DiscountPrices.java - This program calculates total cost for discounted items.
// Input:  Interactive.
// Output:  Original price, discount, discounted price, and total price for all items.

import javax.swing.*;

public class DiscountPrices
{
	public static void main(String args[])
	{
		final double DISCOUNT_ONE = .05;
		final double DISCOUNT_TWO = .10;
		final double DISCOUNT_THREE = .15;
		double price;
		String priceString;
		int quantity;
		String quantityString;

		priceString = JOptionPane.showInputDialog("Enter price of item: ");
		price = Double.parseDouble(priceString);
		quantityString = JOptionPane.showInputDialog("Enter quantity ordered: ");
		quantity = Integer.parseInt(quantityString);

		// Test price here and call calculatePrice method.
		calculatePrice(price, quantity);

		int discountValue = 3;
		switch(discountValue)
		{
		case 1: if(price < 5.00);
				price = price * DISCOUNT_ONE;
				break;
		case 2: if(price < 10.00);
				price = price * DISCOUNT_TWO;
				break;
		case 3: if(price > 10.00);
				price = price * DISCOUNT_THREE;
				break;
		}

		System.exit(0);

	} // End of main() method.


	// Write calculatePrice method here.
	public static void calculatePrice(double amount, int quantity)
	{
		double total;
		double discountPercent;
		double discountPrice;

		discountPercent = (amount / 100);
		total = (amount * quantity);

		System.out.println("Original Price:  " + amount + " Discount Percent:  " + discountPercent + " Quantity:  " + quantity + " Total Price:  " + total);
	}


} // End of DiscountPrices class.

Alright, lets go through what you need to output step by step.

You want to output the original price of the item, which works, although I would change

public static void calculatePrice(double amount, int quantity)

to

public static void calculatePrice(double price, int quantity)

This way it is easier to follow all of your variables.

Next you want to output the discount percent, which you calculated by dividing the price by 100.

discountPercent = (amount / 100);

So if someone spends 100 or more, they get a 100% discount? I think you meant to use the three variables you declared in your main method DISCOUNT_ONE DISCOUNT_TWO and DISCOUNT_THREE and the switch statements inside your main method to calculate the discount. As your program is now the output comes before the switch statements, so they are useless. You should move those into your calculatePrice() method and change price to discount price like so...

case 1: if(price < 5.00);
	discountPrice = price * DISCOUNT_ONE;
	break;
case 2: if(price < 10.00);
	discountPrice = price * DISCOUNT_TWO;
	break;
case 3: if(price > 10.00);
	discountPrice = price * DISCOUNT_THREE;
	break;

Next time follow the same basic steps I did here, go through what your program needs to do, break it down into simple steps, and follow your code, if you had gone through it line by line, command by command, you would have seen these pretty simple errors. There might still be some problems once you make those changes, from this point you should be able to figure it out on your own.

Edited 7 Years Ago by jasimp: n/a

Thank you for the step by step.
Now, I am getting more errors. I'll try and see what the problem is.

I was able to work out the code and you have no idea how excited I am despite two errors I am experiencing. I've added two switch statements for discountPrices and the discountPercent. When I compile and run the program discountedPrice and discountPercent are miscalculated. I think it may have something to do with the switch statements.

Here's the revised code:

// DiscountPrices.java - This program calculates total cost for discounted items.
// Input:  Interactive.
// Output:  Original price, discount, discounted price, and total price for all items.

import javax.swing.*;

public class DiscountPrices
{
	public static void main(String args[])
	{
		final double DISCOUNT_ONE = .05;
		final double DISCOUNT_TWO = .10;
		final double DISCOUNT_THREE = .15;
		double price;
		String priceString;
		int quantity;
		String quantityString;

		priceString = JOptionPane.showInputDialog("Enter price of item: ");
		price = Double.parseDouble(priceString);
		quantityString = JOptionPane.showInputDialog("Enter quantity ordered: ");
		quantity = Integer.parseInt(quantityString);

		// Test price here and call calculatePrice method.
		calculatePrice(price, quantity);

		System.exit(0);

	} // End of main() method.


	// Write calculatePrice method here.
	public static void calculatePrice(double price, int quantity)
	{
		double total;
		double salesPrice = 0;
		double discountPercent = 0;
		double discountPrice = 0;
		final double DISCOUNT_ONE = .05;
		final double DISCOUNT_TWO = .10;
		final double DISCOUNT_THREE = .15;

		int discountValue = 3;
		switch(discountValue)
		{
			case 1: if(price < 5.00);
					discountPrice = (price * DISCOUNT_ONE);
					break;
			case 2: if(price < 10.00);
					discountPrice = (price * DISCOUNT_TWO);
					break;
			case 3: if(price > 10.00);
					discountPrice = (price * DISCOUNT_THREE);
					break;
		}

				int percentValue = 3;
				switch(percentValue)
				{
					case 1: if(price < 5.00);
							discountPercent = (100 * DISCOUNT_ONE);
							break;
					case 2: if(price < 10.00);
							discountPercent = (100 * DISCOUNT_TWO);
							break;
					case 3: if(price > 10.00);
							discountPercent = (100 * DISCOUNT_THREE);
							break;
				}

		salesPrice = price - discountPrice;
		total = salesPrice * quantity;

		System.out.println("Original Price:  " + price + " Discount Percent:  " + discountPercent + " Discounted Price:  " + discountPrice + " Quantity:  " + quantity + " Total Price:  " + total);

		return;
	}

} // End of DiscountPrices class.

despite two errors I am experiencing.

What errors? I don't see any in the code. However you do declare DISCOUNT_ONE DISCOUNT_TWO and DISCOUNT_THREE in your main method, which you don't need because they are used in declared in calculatePrice().

When I compile and run the program discountedPrice and discountPercent are miscalculated.

Say something costs 15 dollars. The way your code is written it would be eligible for the DISCOUNT_THREE percentage, so each item would get 2.25 dollars off. Now each item costs 12.75 dollars. If I bought ten of them the total price would be 127.50 dollars. That is the way your code works. I ran those numbers into your program and got this output Original Price: 15.0 Discount Percent: 15.0 Discounted Price: 2.25 Quantity: 10 Total Price: 127.5 If those numbers aren't what you think they should be, how do you think the discountPercent and discountPrice and totalPrice should be calculated?

Edited 7 Years Ago by jasimp: n/a

If those numbers aren't what you think they should be, how do you think the discountPercent and discountPrice and totalPrice should be calculated?

Okay. Say for instance the price item is $7.00 and the quantity of items are 5. When the program runs the result is:
Original Price: 7.0 Discount Percent: 15.0 Discounted Price: 1.05 Quantity: 5 Total Price: 29.75

Because 7 is less than 10.00, the discount percent, discount price, and total price should actually be:
Discount Percent: 10.0 Discounted Price: 0.70 Total Price: 31.50

I really feel like sweat dropping at my stupidity.
I finally figured out that there isn't an error.
All I needed was to change the number of the switch . . . ) =

I really feel like sweat dropping at my stupidity.

Stupid me for not testing more than once, I was kind of in a rush to get to bed :$

Nice job figuring it out yourself, and thank you for marking this thread as solved.

This question has already been answered. Start a new discussion instead.