Don't worry about being a "noob". We all have to start somewhere. Start out with a BufferedReader to read from the command line:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Once you have that, then you can read the product number and that good stuff.
Then just use a switch statement to test the input.
Show me what you have so far, and I can help further.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Looks really good. I wouldn't change much at all. The only thing I would do is put the close() method in a finally clause, and add a decimalformat instance to the mix. Something like this:
import java.io.*;
import java.text.*;
public class Sales
{
public static void main(String[] args)
{
double total = 0.0;
BufferedReader br = null;
DecimalFormat dec = new DecimalFormat("###,###.##");
try
{
br = new BufferedReader(new InputStreamReader(System.in));
boolean readMore = true;
while(readMore)
{
System.out.print("Enter the product # --> ");
String prodNum = br.readLine();
System.out.print("Enter the quanity --> ");
String numDesired = br.readLine();
int number, quantity;
try
{
number = Integer.parseInt(prodNum);
quantity = Integer.parseInt(numDesired);
}
catch(NumberFormatException nfe)
{
System.err.println("cannot parse: " + nfe.getMessage());
continue;
}
switch(number)
{
case 1:
total += 2.98 * quantity;
break;
case 2:
total += 4.50 * quantity;
break;
case 3:
total += 9.98 * quantity;
break;
case 4:
total += 4.49 * quantity;
break;
case 5:
total += 6.87 * quantity;
}
System.out.print("Would you like to order more? 'y'/'n' --> ");
String decision = br.readLine();
if(!decision.equals("y"))
{
readMore = false;
}
}
}
catch(IOException ioe)
{
System.err.println("read: " + ioe.getMessage());
}
finally
{
try
{
br.close();
}
catch(IOException ioe)
{
System.out.println("Could not close stream");
}
}
System.out.println("total cost = $" + dec.format(total));
}
}
Other than that, You've done an excellent job.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20