1

My problem is that in my code below, in OPTION 2, if the user Store = 1 and the item details overwrites the item[0] with the new details. I want it to store in item[7] and not to overwrite the current information. The Item is found in constructor method in another class. Please see how i can fix this problem and improve my code.

//constructor class

public class Item {

private String itemName;
private int itemNumber;
private int itemQuantity;
private double itemPrice;

public Item() {
    itemName = " ";
    itemNumber = 0;
    itemQuantity = 0;
    itemPrice = 0.0;
}

public Item(String name, int itemNum, int itemQty, double itemP) {
    this.itemName = name;
    this.itemNumber = itemNum;
    this.itemQuantity = itemQty;
    this.itemPrice = itemP;
}

public void setItemName(String name) {
    this.itemName = name;
}

public void setItemNumber(int itemNum) {
    this.itemNumber = itemNum;
}

public void setItemQuantity(int itemQty) {
    this.itemQuantity = itemQty;
}

public void setItemPrice(double itemP) {
    this.itemPrice = itemP;
}

public String getName() {
    return this.itemName;
}

public int getItemNumber() {
    return this.itemNumber;
}

public int getItemQuantity() {
    return this.itemQuantity;
}

public double getItemPrice() {
    return this.itemPrice;
}

public void displayData() {
    for (int i = 0; i < 1; i++) {
        System.out.println("Name: " + this.itemName + "\t\tItem number: " + this.itemNumber + "\t\tPrice: $" + this.itemPrice
                + "\t\tQuantity: " + this.itemQuantity);
    }
}

public boolean is_the_same(Item itemNum) {
    boolean same = (itemName.equals(itemNum.itemName) && itemNumber == itemNum.itemNumber);
    return same;
}
} //end of constructor

import java.util.Scanner;

public class test {

public static void main(String[] args) {
    Item ItemArray[] = new Item[6];
    Scanner input = new Scanner(System.in);

    String[] itemName = {"Eggs", "Chicken", "Coca-Cola", "Apple", "Water", "Chocolate"};
    int[] itemNumber = {1, 5, 11, 15, 12, 20};
    Double[] itemPrice = {0.2, 5.8, 1.4, 0.6, 1.0, 0.7};
    int[] itemQuantity = {60, 20, 12, 35, 80, 58};

    for (int x = 0; x < ItemArray.length; x++) {
        ItemArray[x] = new Item(itemName[x], itemNumber[x], itemQuantity[x], itemPrice[x]);
    }

    for (int i = 0; i < 1; i++) {
        System.out.println("Choose one of the options[] below: \n[1] Buy item. \n[2] Add an item to store. \n[3] List all items and their prices. \n[4] Search for an item.");
        System.out.print("Enter Option: ");
        int Option = input.nextInt();

        if (Option < 1 || Option > 4) {
            System.out.println("\nInvalid Option. Please enter option again.");
            System.out.println("Choose one of the options[] below: \n[1] Buy item. \n[2] Add an item to store. \n[3] List all items and their prices. \n[4] Search for an item.");
            System.out.print("Enter Option: ");
            Option = input.nextInt();
        }

        if (Option == 2) {
            System.out.print("\n--------------OPTION 2--------------");
            System.out.print("\nHow many items to add to store: ");
            int Store = input.nextInt();

            for (int c = 0; c < Store; c++) {
                System.out.print("Name: ");
                ItemArray[c].setItemName(input.next());
                System.out.print("Number: ");
                ItemArray[c].setItemNumber(Integer.parseInt(input.next()));
                System.out.print("Price: $");
                ItemArray[c].setItemPrice(Double.parseDouble(input.next()));
                System.out.print("Quantity: ");
                ItemArray[c].setItemQuantity(Integer.parseInt(input.next()));
            }

            input.close();

            System.out.print("\n\n");
            for (int c = 0; c < ItemArray.length; c++) {
                ItemArray[c].displayData();
            }
            System.exit(0);
        }
    }
}
}
Alexander Petrov
  • 9,204
  • 31
  • 70
Rohan
  • 17
  • 5
  • You have array of size 6 which mean index 0 to 5 so it’s unclear what you mean when write you want “store in item[7]” – Joakim Danielson Feb 10 '19 at 09:55
  • Only in some languages arrays can start with 1, java is not one of them so you should either code from a zero based point of view or consider other languages – Cleptus Feb 10 '19 at 10:03

2 Answers2

1

If you're inserting a new element at a particular index, using a List object or another collection would be simpler than using array primitives.

The List.add function lets you insert an item at a particular position.

jspcal
  • 50,847
  • 7
  • 72
  • 76
1

Hello in oreder to do what you want you need two 3 things.

  1. Do as the collegue above is saying, use a List. It may better to use a Set in your case to ensure that an Item can be added only once and then you can just modify the quantity of the existing items in the bag.
  2. Use while(true) cycle instead of for loop
  3. Add an option that corresponds to end of user input which will take you out of the while(true) cycle.

If you decide to keep the array. you need to do initialize large enough array and keep the index of the last added item. Again you need while(true) with exit condition to make this index sensible.

Alexander Petrov
  • 9,204
  • 31
  • 70