Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Im screwed and floundering, HELP PLEASE!!!

 
Mandy Thompson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am stuck on this big time, I have been floundering here for like 4 hours and if anyone can help I would be SO grateful, I have this program that I have to write that I cannot grasp. I dont expect anyone to write this for me, maybe some expert guidance or tips is all im asking.

I have to develop a program for a "small company" that sells 5 products with the prices procuct 1 $2.98, product 2 $4.50, product 3 $9.98, product 4 $4.49, and product 5 $6.87. The application has to read in the product number and the quantity sold. The program has to use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. It also has to use a sentinel-controlled loop to determine when the program should stop looping and display the final results.

Unfortunately I took this class as an elective and this is my final project. I have looked over this over and over and my book sucks!! Any help from you experts out there would be greatly appreciated. Thanks!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> The application has to read in the product number and the quantity sold.

start by doing this, and only this.

when it compiles and runs without error:-
use System.out.println([the data]) to check accuracy

when this compiles/runs/outputs accurately, add this part
"a switch statement to determine the retail price for each product."

when it compiles and runs without error:-
use System.out.println([the data]) to check accuracy

etc etc

if you're stuck at any point, post your code and what the problem is
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you figured out how to code the sentinel controlled loop? You can, for instance, terminate the loop if the quantity sold is a negative number.
 
Mandy Thompson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Havnt tested yet, did it at work. What do you think?



import java.io.*;

public class Sales
{
public static void main(String[] args)
{
double total = 0.0;
BufferedReader br = null;
try
{
br = new BufferedReader(
new InputStreamReader(System.in));
boolean readMore = true;
while(readMore)
{
System.out.println("enter product number");
String prodNum = br.readLine();
System.out.println("enter quantity");
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.println("order more? 'y' or 'n'");
String decision = br.readLine();
if(!decision.equals("y"))
readMore = false;
}
br.close();
}
catch(IOException ioe)
{
System.err.println("read: " + ioe.getMessage());
}
System.out.println("total cost = " + total);
}
}
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far so good. You have not exactly made your program Object Oriented but it nearly passes all the tests.
1).Try not to write all your program in the main method.
2). What happens if the user enters the number 6 for the item code?
3). If I enter an illegal product code should I really be able to enter a quantity before an exception is thrown?
4). Please use the UBB Code tags when posting your code as it make it a lot easier to read indented code
5). If you are learning using the JDK1.5 then you might want to look at the Scanner class for reading from System.in
6). Will you get extra marks for creating a GUI (Graphical User Interface) or for using Input Dialogs to capture the user input?

Keep up the good work
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic