Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,714 people online right now. Registration is fast and FREE... Join Now!




Need help

 
Reply to this topicStart new topic

Need help

Icreery
17 Apr, 2007 - 07:54 PM
Post #1

New D.I.C Head
*

Joined: 17 Apr, 2007
Posts: 2


My Contributions
I am doing this problem and here is the question.

A mail order house sells five products whose retail prices are a s 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
B. quantity sold

Your program should use a switch statement to determine the retail pricce for each product. It should calculate and display the total retail value of all products sold. use a sentinel controlled loop to determine when the program should stop looping and display the final results.

Thats the problem here is what I have so far I think i did it completely wrong and in some places i am totally stuck

[import java.util.Scanner;

public class MailOrder
{

private String houseProducts;
private int total;
private int retailTotal;
private int product1 = 0;
private int product2 = 0;
private int product3 = 0;
private int product4 = 0;
private int product5 = 0;

public MailOrder(String name)
{
houseProducts= name;
}

public void sethouseProducts ( String name)
{
houseProducts = name;
}

public String gethouseProducts()
{
return houseProducts;
}

public void displayMessage()
{

System.out.printf( "Welcome to the product list for\n%s!\n]n", gethouseProducts() );
}

public void inputProduct()
{
Scanner input = new Scanner( System.in );

int product;


System.out.printf("%s\n%s\n",
"Enter the Product Number from 1-5.",
"Type <ctrl> z to use the end-of-file indicator to terminate input.");

while ( input.hasNext() )
{
product = input.nextInt();
total += product;
++retailTotal;


incrementLetterretailTotal ( product );
}
}

public void incrementLetterretailTotal ( int product)
{

switch (product)
{
case 1:
++ 2.98;
break;

case 2:
++product2;
break;

case 3:
++product3;
break;

case 4:
++product4;
break;

case 5:
++product5;
break;
}
}

public void displayReport()
{
System.out.println(" \n Retail Report:");

if ( retailTotal !=0)
{
double sum = (double) total + retailTotal;


System.out.printf(" Total of the %d grades entered is %d\n",
retailTotal, total);

System.out.printf("%s\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n",
"Quantity sold of each product;",
"Product1: ",product1,
"Product2: ",product2,
"Product3: ",product3,
"Product4: ",product4,
"Product5: ",product5);
}

else

System.out.println( " No Products were entered" );
}
}



]
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Need Help
18 Apr, 2007 - 01:08 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
I've not got a new enough version of Java to use the Scanner class, but I've got a working version of your programme using a buffered reader, which hopefully will help you, as now it's working you can tweak it to do exactly what you need. I think the main problem was that you were not multiplying the cost of each product as far as I could see, so your total wasn't right, and you couldn't enter the amount of each product at each time (which I think is what's meant in the question).

CODE

import java.io.*;
import javax.swing.*;
public class MailOrder
{

private String houseProducts;
private int total;
private double retailTotal;
private int product1 = 0;
private int product2 = 0;
private int product3 = 0;
private int product4 = 0;
private int product5 = 0;

private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

public MailOrder(String name)
{
houseProducts= name;
displayMessage();
inputProduct();
displayReport();
}

public void sethouseProducts ( String name)
{
houseProducts = name;
}

public String gethouseProducts()
{
return houseProducts;
}

public void displayMessage()
{

System.out.println( "Welcome to the product list for\n" + gethouseProducts() + "!\n]n"  );
}

public void inputProduct()
{
int product = 0;
String input = "";
int amount = 0;


System.out.println("Enter the Product Number from 1-5.\n" +
"Type -1 to use the end-of-file indicator to terminate input.");
while ( product!=-1 ) {
try {input = reader.readLine();
} catch (IOException e)
{
    System.out.println(e);
}
try {
    product = Integer.parseInt(input);
} catch (Exception e)
{
    System.out.println(e);
}
if (product == -1)
break;
System.out.println("Now enter the number of product " + product +
"sold\n");
//product = input.nextInt();
//total += product;
//++retailTotal;
try {
    input = reader.readLine();
    } catch (IOException e)
    {
        System.out.println(e);
    }
try {
    amount = Integer.parseInt(input);
} catch (Exception e)
{
    System.out.println(e);
}

incrementLetterretailTotal ( product, amount );
}
}

public void incrementLetterretailTotal ( int product, int amount)
{

switch (product)
{
case 1:
retailTotal = retailTotal + (2.98*amount);
product1 = product1 + amount;
break;

case 2:
retailTotal = retailTotal + (4.50*amount);
product2 = product2 + amount;
break;

case 3:
retailTotal = retailTotal + (9.98*amount);
product3 = product3+amount;
break;

case 4:
retailTotal = retailTotal + (4.49*amount);
product4 = product4 + amount;
break;

case 5:
retailTotal = retailTotal + (6.87*amount);
product5 = product5+amount;
break;
}
}

public void displayReport()
{
System.out.println(" \n Retail Report:");

if ( retailTotal !=0)
{
double sum = (double) total + retailTotal;


System.out.println(" Total of the cost is " + retailTotal + "\n");

System.out.println("Quantity sold of each product;\n" +
"Product1: " + product1 +
"\nProduct2: " + product2 +
"\nProduct3: " + product3 +
"\nProduct4: " + product4 +
"\nProduct5: " + product5);
}

else

System.out.println( " No Products were entered" );
}
public static void main (String args[])
{
    MailOrder tester = new MailOrder("Testing");

}
}


This post has been edited by Ellie: 18 Apr, 2007 - 01:10 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month