This is my Assignment:
A mail-order house sells five different products whose retail prices are as follows: product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49; and product 5, $6.87. Write an application that reads a series of pairs of numbers as follows:
a) Product number

Quantity sold for one day
Your program should use a switch structure to help determine the retail price for each product. It should calculate and display the total retail value of each products sold last week. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.
This is my PROBLEM:
I spent a lot of time browsing the web and reading how to do such things as a switch statement and a sentinel-controlled loop and now that I have the code completed it runs and as it goes through the info I have a double product that ends with a zero and I think it is messing up my code...take a look...I would like to know how to get the value to stay at for example $28.82 where I'm getting $28.820000000000004
also am I missing anything?
Thank you
CODE
package ch5;
import java.util.Scanner;
public class Ex517 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inp = new Scanner(System.in);
int product,quantity;
double total=0.00;
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
while(product !=0){
System.out.print("Enter quantity: ");
quantity=inp.nextInt();
switch( product ){
case 1:total+=quantity*2.98;
break;
case 2:total+=quantity*4.50;
break;
case 3:total+=quantity*9.98;
break;
case 4:total+=quantity*4.49;
break;
case 5:total+=quantity*6.87;
break;
default:System.out.println("Invalid Product Number");
System.out.println("Product Number Does not Exist");
if(product<0 && product>=6){
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
System.out.print("Enter quantity: ");
quantity=inp.nextInt();}
break;
}
System.out.print("Enter product #(1-5)(0 to stop): ");
product=inp.nextInt();
}
{
System.out.println( "The total retail value was: "+total );
}
}
{
}
{
}}
This is the answer I get once Ive entered the product number and the quantity sold:
Enter product #(1-5)(0 to stop): 1
Enter quantity: 1
Enter product #(1-5)(0 to stop): 2
Enter quantity: 1
Enter product #(1-5)(0 to stop): 3
Enter quantity: 1
Enter product #(1-5)(0 to stop): 4
Enter quantity: 1
Enter product #(1-5)(0 to stop): 5
Enter quantity: 1
Enter product #(1-5)(0 to stop): 6
Enter quantity: 1
Invalid Product Number
Product Number Does not Exist
Enter product #(1-5)(0 to stop): 0
The total retail value was: $28.820000000000004
This post has been edited by J.A.LrnQuic: 14 Oct, 2008 - 07:52 PM