|
-
Dec 10th, 2002, 01:08 PM
#1
Thread Starter
Addicted Member
Loop problem
i am trying to write a program to input details of several products and quantities and display
them.
After all the product data has been entered (terminated by 0), print the summary totals.:
The inputs will be inputed like so:
Kellogs
Cornflakes 750g
5000127062092
123
10
and should be outputed like so:
Kellogs:Cornflakes 750g:5000127062092:123:10:1230
Transactions 1
Total quantity 10
Total value 1230
Average cost 123
if a number of product details where entered the following would be added together:
The transactions is the number of times it has been around the loop
Total quantity is the quantity added together
Total value is the Total value added together
Average cost is the Average cost added together
e.g.
Input
Kellogs:Cornflakes 750g :5000127062092:123:10:1230
Kellogs:Rice Krispies 600g:5000127053090:123:16:1968
Quaker:Puffed Wheat 160g :1000108001010:78:11:858
Output
Transactions 3
Total quantity 37
Total value 4056
Average cost 109
But im havig trouble doing the above. I think i gotta use a loop or somthing.
Here is the code i have so far:
Code:
public class ProcessProduct{
public static void main(String[] argv) {
int barcodeLength, quantity, price, totalPrice;
String barcode, manufaturer, productName;
System.out.println ("Please enter manufaturer name");
manufaturer = UserInput.readString();
System.out.println ("Please enter product name");
productName = UserInput.readString();
System.out.println ("Please enter barcode");
barcode = UserInput.readString();
barcodeLength = barcode.length();
if (barcodeLength < 13){
System.out.println ("Error, invalid entry, barcode length is less than 13 characters");
System.exit (0);
}
if (barcodeLength > 13){
System.out.println("Error, invalid entry, barcode length is greater than 13 characters");
System.exit(0);
}
System.out.println ("Please enter quantity");
quantity = UserInput.readInt();
if (quantity <= 0 ){
System.out.println ("Error,invalid entry");
System.exit (0);
}
System.out.println ("Please enter price");
price = UserInput.readInt();
if (price <= 0 ){
System.out.println ("Error,invalid entry");
System.exit (0);
}
totalPrice = quantity * price;
Product a = new Product (manufaturer, productName, barcode, price);
System.out.println (a);
-
Dec 11th, 2002, 11:50 AM
#2
Here's what I would do, but I'm not sure if you're supposed to use Vectors or similar containers.
Code:
import java.util.*;
public class ProcessProduct {
public static void main(String[] args) {
ArrayList products;
for(;;) { // infinite loop
int barcodeLength, quantity, price, totalPrice;
String barcode, manufaturer, productName;
System.out.println ("Please enter manufaturer name or '0' to end.");
manufaturer = UserInput.readString();
if(manufaturer.equals("0"))
break; // This keyword exits the loop
System.out.println ("Please enter product name");
productName = UserInput.readString();
System.out.println ("Please enter barcode");
barcode = UserInput.readString();
barcodeLength = barcode.length();
if (barcodeLength < 13){
System.out.println ("Error, invalid entry, barcode length is less than 13 characters");
return; // System.exit is evil
}
if (barcodeLength > 13){
System.out.println("Error, invalid entry, barcode length is greater than 13 characters");
return;
}
System.out.println ("Please enter quantity");
quantity = UserInput.readInt();
if (quantity <= 0 ){
System.out.println ("Error,invalid entry");
return;
}
System.out.println ("Please enter price");
price = UserInput.readInt();
if (price <= 0 ){
System.out.println ("Error,invalid entry");
return;
}
totalPrice = quantity * price;
Product a = new Product (manufaturer, productName, barcode, price);
products.add(a);
}
// Now output the info we collected
int totalQuantity=0, totalValue=0;
for(int i=0;i<products.size();++i) {
Product p = (Product)products.get(i);
// You can do the rest yourself.
// Add the quantity of the current product to totalQuantity
// Same for value.
// And output this product.
}
// Now output the remaining stats
// Transactions is products.size()
}
}
ArrayList is part of the Java Collection libary, in the package java.util. All collections implement the interface Collection. Then they split into lists (implementing List), and sets (implementing Set). ArrayList is a class implementing List which represents a dynamically growable array. Unlike real arrays all collections only store Object references, which means that you have to cast the returned objects to what you need. For arrays the elements are accessed with [index], for lists it's .get(index).
Last edited by CornedBee; Dec 11th, 2002 at 06:12 PM.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 11th, 2002, 06:08 PM
#3
Thread Starter
Addicted Member
???????????????????????????????
Sorry to hassle u but as soon as I compile the program it stops and outputs
Illegal start of expression
It stops by
for (;
Please help..........again lol
ohhh yeah im using BlueJ Terminal Window
and i just want to use variables and a loop
-
Dec 11th, 2002, 06:12 PM
#4
Sorry, the forum converted something into a smilie. It should work now, though there could still be typos in there, I didn't check it.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 14th, 2002, 06:37 AM
#5
Thread Starter
Addicted Member
Sorry again
i edited your code coz i didn't want to use arrays...but thats not the problem.
Why doesnt my program finish when i press "0" (Zero)??????
it just goes to the next UserInput
This is my code
Code:
for(; ;) {
++ transaction ;
System.out.println ("Please enter manufaturer name");
manufaturer = UserInput.readString();
if(manufaturer ==("0"))
break; // This keyword exits the loop
System.out.println ("Please enter product name");
productName = UserInput.readString();
System.out.println ("Please enter barcode");
barcode = UserInput.readString();
barcodeLength = barcode.length();
if (barcodeLength < 13){
System.out.println ("Error, invalid entry, barcode length is less than 13 characters");
System.exit (0);
}
if (barcodeLength > 13){
System.out.println("Error, invalid entry, barcode length is greater than 13 characters");
System.exit(0);
}
System.out.println ("Please enter quantity");
quantity = UserInput.readInt();
if (quantity <= 0 ){
System.out.println ("Error,invalid entry");
System.exit (0);
}
System.out.println ("Please enter price");
price = UserInput.readInt();
if (price <= 0 ){
System.out.println ("Error,invalid entry");
System.exit (0);
}
totalPrice = quantity * price;
Product a = new Product (manufaturer, productName, barcode, price);
System.out.println (a);
i must be pissing you off now....lol
Last edited by NOTSOSURE; Dec 14th, 2002 at 02:22 PM.
-
Dec 14th, 2002, 01:34 PM
#6
When do you enter 0? It needs to be when the app asks for the manufacturer in order to work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|