Reputation: 51
The problem is I don't know how to store my inputs from console.
Here is my code:
for (int j = 0; j < arr1.length; j++) {
System.out.println("");
System.out.print("Select product code: ");
arr1[j] = reader.next();
if (arr1[j].contains("A001")){
mouse = 100.00;
System.out.print("Enter quantity: ");
qty = reader.nextInt();
amount = qty * mouse;
System.out.print("Amount:\t\t\t " + amount);
}
if (arr1[j].contains("A002")) {
monitor = 2500.00;
System.out.print("Enter quantity: ");
qty = reader.nextInt();
amount = qty * monitor;
System.out.print("Amount:\t\t\t " + amount);
}
if (arr1[j].contains("A003")) {
keyboard = 200.00;
System.out.print("Enter quantity: ");
qty = reader.nextInt();
amount = qty * keyboard;
System.out.print("Amount:\t\t\t " + amount);
}
if (arr1[j].contains("A004")) {
flashdisk = 300.00;
System.out.print("Enter quantity: ");
qty = reader.nextInt();
amount = qty * flashdisk;
System.out.print("Amount:\t\t\t " + amount);
}
if (arr1[j].contains("A005")) {
harddisk = 1500.00;
System.out.print("Enter quantity: ");
qty = reader.nextInt();
amount = qty * harddisk;
System.out.print("Amount:\t\t\t " + amount);
}
and here is the code that must be the line of code where it will show the result of stored data.
System.out.println("");
System.out.print("Add item (y/n) " );
yn = reader.next().charAt(0);
if (yn == 'y' || yn == 'Y') {
continue;
}
else if (yn == 'n' || yn == 'N') {
System.out.println("Code\tDescription\tUnit Price\tQuantity\tAmount");
System.out.print(arr1[j]);
if (arr1[j].contains("A001")) {
System.out.print("\tMouse");
System.out.print("\t\t"+mouse);
System.out.print("\t\t"+qty);
System.out.print("\t\t"+amount);
if (arr1[j].contains("A002")) {
System.out.print("\t\nMonitor");
System.out.print("\t\t"+mouse);
System.out.print("\t\t"+qty);
System.out.print("\t\t"+amount);
}
}
}
The sample input must be:
Select product code: A004
Enter quantity: 2
Amount: 800
add item(y/n)?: y
Select product code: A001
Enter quantity: 1
Amount: 100
add item(y/n)?: n
then it will be stored in an array which would be the result but instead of getting both of the input arrays, I only get the 2nd input array instead both of them.
Code Description Unit Price Quantity Amount
A001 Mouse 100.0 1 100.0
Upvotes: 0
Views: 136
Reputation: 1403
Every time you are reading the user input, next you are overriding the information, because you are storing in variables instead of array.
Create an object like:
class Product {
private String code;
private String name;
private Long price;
private Integer amount;
public Long getPrice();
public Integer getAmount();
public void setPrice(Long price) { this.setPrice(price); }
public void setAmount(Integer amount) { this.setAmount(amount); }
...
}
Then, create an Product [] productArr = new Product[anyLength];
Now when you check if the input is A005 for example, you would store the object with the following information:
if (arr[j].contains("A005")) {
Product newProduct = new Product();
newProduct.setPrice(1500.00);
newProduct.setCode("A005");
qty = reader.nextInt();
amount = qty * newProduct.getPrice();
newInfo.setAmount(amount);
productArr[j] = newProduct;
}
Then when you need to print them, you just access by the index productArr[j].getAmount()
for example.
Try as a recommended, But in the future if you want a better approach, it would be better to use a Map<String, Product> to map the A001 to your product, A002 ..., A003, ... and then when you need to print them you just get by key.
Upvotes: 1