• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Im screwed and floundering, HELP PLEASE!!!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic