help with java
Okay, so I am going to be lazy and post my problem typed from another message board, but bassically I have to write a program that takes a integer to determine a type of product (the cost) using a case statement, and then multiply the cost by the quanity and then print them after the user is done inputting information (which is controlled with setinal value)
Okay, So I have my program going, but instead of the results being printed at the end in a list, it is printing them after you enter the item type, and the quanity, so say if you enter a 1 and another 1, it will print the results underneath that, before it asks you to enter two new integers, then it will ask you for two more integers, and then print the new results and so on...untill you enter the sentinal value 0.
Any suggestions?
Thanks
here is sales.java
import java.util.Scanner;
public class Sales
{
int price, quanity, type, value;
double setPrice, price1, price2, price3, price4, price5, totalprice;
public void setPrice ()
{
type = 1;
Scanner input = new Scanner ( System.in );
while (type != 0 )
{
System.out.printf("Enter Number (1-5) (0 to stop): ");
type = input.nextInt();
System.out.printf("Enter quanity sold: ");
quanity = input.nextInt();
price1 = 2.91;
price2 = 4.50;
price3 = 9.98;
price4 = 4.49;
price5 = 6.87;
value = type;
switch (value)
{
case 1:
setPrice = price1 * quanity;
//System.out.println(setPrice);
break;
case 2:
setPrice = price2 * quanity;
//System.out.println(setPrice);
break;
case 3:
setPrice = price3 * quanity;
//System.out.println(setPrice);
break;
case 4:
setPrice = price4 * quanity;
//System.out.println(setPrice);
break;
case 5:
setPrice = price5 * quanity;
//System.out.println(setPrice);
}//end of switch
//System.out.println(setPrice);
}// end of while loop
}// end of method setprice
public double getPrice()
{
return setPrice;
}//end of getprice
}//end of method class Sales
and here is salesTest.java
public class SalesTest
{
public static void main ( String args [] )
{
double setPrice;
Sales mySales = new Sales();
mySales.setPrice();
System.out.println(mySales.setPrice);
}
}
A few things about your program.
Why are you reassigning values to price1 etc each time around the loop. Just assign values to those variables once when you declare them. Also, if their values never change you might want to think about making them final.
What is the point of type variable? You assign the value of type to value and never use type again. Why not just assign the input to value directly and get rid of type.
Since you have commented out all the System.out.println lines of code I don't see how you code performs sa you have described.
Lastly, there is a logic problem. If a user does input a zero, the program(loop) doesn't terminate immediately. It continues to ask the user to input a quantity as well. I would assume this is not supposed to happen.