The Artima Developer Community
Sponsored Link

Java Answers Forum
Can't get output to perform corretly

1 reply on 1 page. Most recent reply: Oct 2, 2006 6:31 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
sherri statham

Posts: 2
Nickname: iwlu10101
Registered: Sep, 2006

Can't get output to perform corretly Posted: Sep 29, 2006 5:57 AM
Reply to this message Reply
Advertisement
Ok i've gotten most of the code written, and a big headache too, lol
This what I can't get to be the printout as:

Program input:
Enter product number, or <ctrl>z to stop:1
quantity sold for product 1:1
Enter product number, or <ctrl>z to stop:2
quantity sold for product 2:10
Enter product number, or <ctrl>z to stop:3
quantity sold for product 3:100
Enter product number, or <ctrl>z to stop:4
quantity sold for product 4:1000
Enter product number, or <ctrl>z to stop:5
quantity sold for product 5:10000
Enter product number, or <ctrl>z to stop: ^Z

Program output:
product Quantity unit price total sales

1 1 $ 2.98 $ 2.98
2 10 $ 4.50 $ 45.00
3 100 $ 9.98 $ 998.00
4 1000 $ 4.49 $ 4,490.00
5 10000 $ 6.87 $ 68,700.00

total sales $ 74,235.98

end of program


here the Pseudo code,
MailOrder

+main(args [ ] : String)

-PrintSalesReport( quantitySoldOfProduct1: int,

quantitySoldOfProduct2: int,

quantitySoldOfProduct3: int,

quantitySoldOfProduct4: int,

quantitySoldOfProduct5: int)




DEFINE a local variable input, a Scanner

DEFINE a local variable quantitySoldOfProduct1, an integer and initialize it to zero

DEFINE a local variable quantitySoldOfProduct2, an integer and initialize it to zero

DEFINE a local variable quantitySoldOfProduct3, an integer and initialize it to zero

DEFINE a local variable quantitySoldOfProduct4, an integer and initialize it to zero

DEFINE a local variable quantitySoldOfProduct5, an integer and initialize it to zero

DEFINE a local variable productId, an integer

DEFINE a local variable quantitySoldOfProduct, an integer

DISPLAY a blank line followed by the task id and programmer (your name) identification line.

DISPLAY a blank line followed by the “Program input:” line

DISPLAY the initial “Enter product number…” prompt

DO

Using nextInt() method of input variable, ASSIGN the entered value to productId

Using printf() method and appropriate horizontal tab(s), DISPLAY “Quantity sold” prompt

Using nextInt() method of input variable, ASSIGN the enter value to quantitySoldOfProduct

the switch statement, summarize quantitySoldOfProduct into appropriate

quantitySoldOfProduct1 through quantitySoldOfProduct5, depending on value of productId.

Within the switch statement, using default case, if an invalid productId has been entered,

DISPLAY an appropriate error message and ignore the input

DISPLAY the “Enter product…” prompt

WHILE (input.hasNext())

Using PrintSalesReport() method, print the sales report

DISPLAY a blank line followed by the “End of Program” line

DEFINE a local variable totalSalesAllProducts, an double and initialize it to zero

DEFINE a local variable unitPriceOfProduct1, an double and initialize it to 2.98

DEFINE a local variable unitPriceOfProduct2, an double and initialize it to 4.50

DEFINE a local variable unitPriceOfProduct3, an double and initialize it to 9.98

DEFINE a local variable unitPriceOfProduct4, an double and initialize it to 4.49

DEFINE a local variable unitPriceOfProduct5, an double and initialize it to 6.87

DEFINE a local variable product1Sales, a double and initialize it to

quantitySoldOfProduct1 * unitPriceOfProduct1

DEFINE a local variable product2Sales, a double and initialize it to

quantitySoldOfProduct2 * unitPriceOfProduct2

DEFINE a local variable product3Sales, a double and initialize it to

quantitySoldOfProduct3 * unitPriceOfProduct3

DEFINE a local variable product4Sales, a double and initialize it to

quantitySoldOfProduct4 * unitPriceOfProduct4

DEFINE a local variable product5Sales, a double and initialize it to

quantitySoldOfProduct5 * unitPriceOfProduct5

DISPLAY a blank line followed by the “Program output:” line

Using appropriate format specifiers and horizontal tabs, DISPLAY the column heading line

followed by a blank line

Using appropriate format specifiers and horizontal tabs, DISPLAY one line for each

product, its quantity sold, its unit price and its total sales

ASSIGN the sum of product1Sales through product5Sales TO totalSalesAllProducts

Using appropriate format specifiers and horizontal tabs, DISPLAY the “Total Sales” line


here is what I've written, but can't seem to get the readout to look like this example


import java.util.Scanner;
import java.text.*;

public class MailOrder
{

public static void main(String[] args)
{

Scanner input = new Scanner(System.in);

int quantitySoldOfProduct1 = 0;
int quantitySoldOfProduct2 = 0;
int quantitySoldOfProduct3 = 0;
int quantitySoldOfProduct4 = 0;
int quantitySoldOfProduct5 = 0;
int productId, quantitySoldOfProduct;


System.out.print("\nProgram input:\n");

System.out.print("\n\tEnter product number, or <ctrl>z to stop: ");
productId = input.nextInt();
do
{

System.out.printf("\t\t Quantity Sold for product ");
quantitySoldOfProduct = input.nextInt();

switch(productId)
{

case 1:quantitySoldOfProduct1 = quantitySoldOfProduct;
break;

case 2: quantitySoldOfProduct2 += quantitySoldOfProduct;
break;

case 3: quantitySoldOfProduct3 += quantitySoldOfProduct;
break;

case 4: quantitySoldOfProduct4 += quantitySoldOfProduct;
break;

case 5: quantitySoldOfProduct5 += quantitySoldOfProduct;
break;

default: System.out.println("ERROR: Invalid Product number enterd.");
break;

}//end switch

System.out.print("\n\tEnter product number, or <ctrl>z to stop: ");

}while(input.hasNext());

PrintSalesReport(quantitySoldOfProduct1,quantitySoldOfProduct2,quantitySoldOfPr oduct3,quantitySoldOfProduct4,quantitySoldOfProduct5);

System.out.println("End of Program");

}//end main

public static void PrintSalesReport(int quantitySoldOfProduct1, int quantitySoldOfProduct2, int quantitySoldOfProduct3, int quantitySoldOfProduct4, int quantitySoldOfProduct5)
{

int totalSalesAllProducts = 0;
double unitPriceOfProduct1 = 2.98;
double unitPriceOfProduct2 = 4.50;
double unitPriceOfProduct3 = 9.98;
double unitPriceOfProduct4 = 4.49;
double unitPriceOfProduct5 = 6.87;

double product1Sales = quantitySoldOfProduct1 * unitPriceOfProduct1;
double product2Sales = quantitySoldOfProduct2 * unitPriceOfProduct2;
double product3Sales = quantitySoldOfProduct3 * unitPriceOfProduct3;
double product4Sales = quantitySoldOfProduct4 * unitPriceOfProduct4;
double product5Sales = quantitySoldOfProduct5 * unitPriceOfProduct5;

System.out.println("\nProgram output:\n");

System.out.println("Product\tQuantity\tUnit Price\tTotal Sales\n");

//need more print statements here

}//end PrintSalesReport

}//end class MailOrder


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Can't get output to perform corretly Posted: Oct 2, 2006 6:31 AM
Reply to this message Reply
Rather than using escape characters to lay out your output, you might try using Java's inbuilt number formatting, as described at http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

V.

Flat View: This topic has 1 reply on 1 page
Topic: Arithmetic Expression Evaluation Previous Topic   Next Topic Topic: laptops for sale at an affordable rate

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use