0

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
3
  • in the result part, where is the if checking this A004 ? Commented Nov 19, 2020 at 10:04
  • Where does j comes from in the second block? Commented Nov 19, 2020 at 10:04
  • I tried doing the if for A004 and still gets the same results Commented Nov 19, 2020 at 10:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

I don't get your idea, can you please enlighten me more. Sorry, I really do need help for this one. :(((
First, wour solution will not work, look carefully about the variable. It's not always harddisk, right? Second, he didn't ask about using new mechanism like map
I suggest change class with output properties: Code - Description - Unit - Price - Quantity - Amount
He can store the information he wants in Info Object. The map is just a suggestion, that means it's Optional.
@JiggyPalconit if this is not helping, I will delete my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.