I’m trying to make a java application that’s allows the user to make a catalog for a supermarket, and then display all the products that the user entered to the catalog. Now I have a problem with filling the array of objects the should be fill in by the user’s input.
The output should be like the following (user input in Bold):
Enter product description (or # to stop): Condensed Powdered water
Enter product code: P3487
Enter product unit price: $2.50
Enter product unit phrase: per packet
Enter product description (or # to stop): Distilled Moonbeams
Enter product code: K3876
Enter product unit price: $3.00
Enter product unit phrase: a dozen
Enter product description (or # to stop): Anti-Gravity Pills
Enter product code: Z9983
Enter product unit price: $12.75
Enter product unit phrase: for 60
Enter product description (or # to stop): #
Your catalog:
P3487, Condensed Powdered water, $2.50 per packet.
K3876, Distilled Moonbeams, $3.00 a dozen.
Z9983, Anti-Gravity Pills, $12.75 for 60.
The code that I wrote : 2 classes
class 1:
public class Catalog {
private String description ;
private String code ;
private double price ;
private String phrase ;
int counter = 0;
private Catalog [] list = new Catalog [100];
public Catalog (String productDescription , String productCode , double productPrice , String productPhrase)
{
description = productDescription;
code = productCode;
price = productPrice;
phrase = productPhrase;
}
public void setDescription (String productDescription)
{
description = productDescription;
}
public String getDescription ()
{
return description;
}
public void setCode (String productCode)
{
code = productCode;
}
public String getCode ()
{
return code;
}
public void setPrice (double productPrice)
{
price = productPrice;
}
public double getPrice ()
{
return price;
}
public void setPhrase (String productPhrase)
{
phrase = productPhrase;
}
public String getPhrase ()
{
return phrase;
}
class 2:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CatalogTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String name = null;
String code = null;
double price = 0.0;
String phrase = null;
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
Catalog product = new Catalog(name,code,price,phrase);
Catalog [] productsArray = new Catalog [100];
for (int i = 0 ; i < productsArray.length ; i ++)
{
System.out.println("Enter product description (or # to stop): ");
name = input.readLine();
if (!("#".equals(name)))
{
productsArray [i] = product;
product.setDescription(name);
System.out.println("Enter product code: ");
code = input.readLine();
productsArray [i] = product;
product.setCode(code);
System.out.println("Enter product unit price: ");
price = Double.parseDouble(input.readLine());
productsArray [i] = product;
product.setPrice(price);
System.out.println("Enter product unit phrase: ");
phrase = input.readLine();
productsArray [i] = product;
product.setPhrase(phrase);
productsArray [i] = new Catalog (name,code,price,phrase);
}
else
{
System.out.println("Your Catalog:");
System.out.printf("%s, %s,$%.2f %s",product.getCode(),product.getDescription(),product.getPrice(),product
.getPhrase());
break;
}
}
}
}
The output I get (user input in Bold) :
Enter product description (or # to stop):
condensed powdered water
Enter product code:
p3487
Enter product unit price:
2.50
Enter product unit phrase:
per packet
Enter product description (or # to stop):
distilled moonbeams
Enter product code:
k3876
Enter product unit price:
3
Enter product unit phrase:
a dozen
Enter product description (or # to stop):
#
Your Catalog:
k3876, distilled moonbeams,$3.00 a dozen
So any help PLEASE ??
Here is the updated code that works…