-5
\$\begingroup\$

Write a Java application that prompts the user for pairs of inputs of a product number (1,2,3,4,5) and quantity of units sold (any integer)?

Use a switch statement and a sentinel-controlled loop (i.e., a loop that stops execution when an out-of-range value, such as -1, is input).

All 15 items below are for a single purchase. There are five sets of inputs as follows:

Product 1 1 unit (cost is $2.98 per unit) Product 2 2 units (cost is $4.50 per unit) Product 3 3 units (cost is $9.98 per unit) Product 4 4 units (cost is $4.49 per unit) Product 5 5 units (cost is $6.87 per unit) 

Your application must calculate and display the total retail value for all the five pair of products sold.


import java.util.Scanner; public class Program3 { Scanner input = new Scanner (System.in); public static void main(String[] args) { int productNo = 0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalSales = 0; while(productNo !=0 ) System.out.println("Enter product number 1-5 "); productNo=input.nextInt(); System.out.println("Enter quantity sold "); quantity = input.nextInt(); switch (productNo) { case 1: product1 = 2.98; totalSales+=(2.98*quantity); break; case 2: product2 = 4.50; totalSales+=(4.50*quantity); break; case 3: product3 = 9.98; totalSales+=(9.98*quantity); break; case 4: product4 = 4.49; totalSales+=(4.49*quantity); break; case 5: product5 = 6.87; totalSales+=(6.87*quantity); break; } System.out.println(totalSales); } 
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

Errors

  • You had some misstyped curly brackets. Please double check your code next time before posting it here.

  • productNo had 0 as initial value, the while checked productNo != 0 meaning it will never perform an iteration. Instead the while should check against -1 as was specified.

  • You refered to input which was a non-static member. Meaning it can not be refered to from a static method. (As your main is.)


double not for currency

double should not be used to represent currency. You can read more about why here. A short version; double (and float for that matter) is not accurate enough.


Switch

Now I can understand why they homework wanted you to study switch and case however you should know that there are better ways to solve the problem. Adding a class Product with an id and a price. You could also use a Map<K, V> to bind productNo to a price.


Consistency

Is there a reason why Scanner input = new Scanner (System.in); is a field and not the productNo variables? If you want it as a field you can use the final keyword, to make sure that it never gets overwritten.

Final version

import java.math.BigDecimal; import java.util.Scanner; public class Test { private final static Scanner input = new Scanner (System.in); private final static BigDecimal product1 = new BigDecimal("2.98"); private final static BigDecimal product2 = new BigDecimal("4.50"); private final static BigDecimal product3 = new BigDecimal("9.98"); private final static BigDecimal product4 = new BigDecimal("4.49"); private final static BigDecimal product5 = new BigDecimal("6.87"); public static void main(String[] args) { BigDecimal totalSales = new BigDecimal(0); int productNo = 0; while (productNo != -1 ) { System.out.println("Enter product number 1-5 "); productNo = input.nextInt(); System.out.println("Enter quantity sold "); BigDecimal quantity = new BigDecimal(input.nextInt()); switch (productNo) { case 1: totalSales.add(product1.multiply(quantity)); break; case 2: totalSales.add(product2.multiply(quantity)); break; case 3: totalSales.add(product3.multiply(quantity)); break; case 4: totalSales.add(product4.multiply(quantity)); break; case 5: totalSales.add(product5.multiply(quantity)); break; } } System.out.println(totalSales.toString()); } } 

Disclaimer

There might be other areas to improve on. This is by no means a perfect fix of the code.

\$\endgroup\$
2
  • \$\begingroup\$ No need to write a disclaimer - partial reviews are just as good :) See Is it ok to review only a part of the code? \$\endgroup\$ Commented Aug 9, 2015 at 10:19
  • \$\begingroup\$ It was more a note to the readers that there might be other areas to improve upon. That they shouldn't expect these improvements to perfect, if such a thing exists, the code. \$\endgroup\$ Commented Aug 11, 2015 at 1:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.