0

I need to write a method that reads from a .txt file and creates a new "InventoryItem" object and adds it to a "Warehouse" object. The Warehouse class creates an arraylist of InventoryItem. I am using scanner to read from the text file, but do not understand how to bring those values into the new Warehouse object I've created.

The text file looks like:

Purple Widget

1234 17.95 150

My method:

public Warehouse readInventoryFile(File inputFile) throws FileNotFoundException {
    Warehouse warehouse1 = new Warehouse();
    Scanner in = new Scanner(inputFile);

    while(in.hasNext()) {
    String item = in.nextLine();
    int sku = in.nextInt();
    double price = in.nextDouble();
    int stock = in.nextInt();
    String nextLine = in.nextLine();

}
    return warehouse1;

}

The InventoryItem class:

public class InventoryItem{
    private final int sku;
    private final String item;
    private double price;
    private int stock;

public InventoryItem(int sku, String item, double price, int stock){
    this.sku = sku;
    this.item = item;
    this.price = price;
    this.stock = stock;

  }//InventoryItem constructor

  public int getSku(){
    return sku;
  }//getSKU

  public String getItem(){
    return item;
  }//getItem

  public void setPrice(double price){
    this.price = price;
  }//setPrice

  public double getPrice(){
    return price;
  }//getPrice

  public void setStock(int stock){
    this.stock = stock;
  }//setStock

  public int getStock(){
    return stock;
  }//getStock


        @Override
  public String toString(){
    return String.format("[%d, %s, %1.2f, %d]", sku, item, price, stock);
  }//toString

  public static void main(String[] args){
    InventoryItem itemName = new InventoryItem(1234, "crackers", 2.50, 4);
    itemName.toString();

  }//main

}//InventoryItem

The Warehouse class:

import java.util.ArrayList;

public class Warehouse {

    private final ArrayList<InventoryItem> inventory;

    public Warehouse() {
        inventory = new ArrayList<>();
    }//constructor

    public void addItem(InventoryItem item) {
        inventory.add(item);
    }//addItem

    private int findHelper(int sku) {
        int index = 0;
        while (index < inventory.size()) {
            if (sku == inventory.get(index).getSku()) {
                return index;
            }//if
            index++;
        }//while
        return -1;
    }//findHelper

    public InventoryItem findItem(int sku) {
        int index = findHelper(sku);
        return inventory.get(index);
    }//findItem

    public InventoryItem removeItem(int sku) {
        int index = findHelper(sku);
        return inventory.remove(index);
    }//removeItem

    public void updateItemQuantity(int sku, int stock) {
        int index = findHelper(sku);
        inventory.get(index).setStock(stock);
    }//updateItemQuantity

    public void updateItemPrice(int sku, double price) {
        int index = findHelper(sku);
        inventory.get(index).setPrice(price);
    }//updateItemPrice

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < inventory.size(); i++) {
            sb.append(inventory.get(i).toString()).append(", ");
        }//for
        return this.inventory.toString();
    }//toString

    private int seqSearchRecHelper(int sku, int index) {
        if (index < inventory.size()) {
            if (sku == inventory.get(index).getSku()) {
                return index;
            }//if sku ==
            return seqSearchRecHelper(sku, index + 1);
        }//if index
        return -1;
    }//seqSearchHelper

    public InventoryItem seqSearchRec(int sku) {
        int i = seqSearchRecHelper(sku, 0);
        if (i == -1) {
            return null;
        }//if
        return inventory.get(i);
    }//seqSearchRec

    public void selectSort() {
        int largestIndex;
        int largest;
        //for (int i = inventory.size() - 1; i > 0; i--) {
        for(int i = 0; i<inventory.size(); i++){
            largest = inventory.get(0).getSku();
            largestIndex = i;
            for (int j = i+1; j < inventory.size(); j++) {
                if (largest < inventory.get(j).getSku()) {
                    largest = inventory.get(j).getSku();
                    largestIndex = j;
                    //largest++;
                }//if
            }//for j
            InventoryItem temp = inventory.get(i);
            inventory.set(i, inventory.get(largestIndex));
            inventory.set(largestIndex, temp);

        }//for i
    }//selectSort 

    public InventoryItem binSearch(int sku) {
        return binSearchHelper(sku, 0, inventory.size()-1);
    }//binSearch

    private InventoryItem binSearchHelper(int sku, int start, int end) {
        if(start>end){return null;}
        int mid=(start+end)/2;
        if(sku == inventory.get(mid).getSku()){return inventory.get(mid);}
        if(sku<inventory.get(mid).getSku()){return binSearchHelper(sku, start, mid-1);}
        return binSearchHelper(sku, mid+1, end);



    }//binSearchHelper

    public int size() {
        return inventory.size();
    }

    /**
     *
     * @param index
     * @return 
     */
    public InventoryItem getItem(int index) {
        return inventory.get(index);
    }

    public static void main(String[] args) {

        Warehouse item = new Warehouse();
        Warehouse item2 = new Warehouse();
        item.addItem(new InventoryItem(10, "crackers", 2.00, 10));
        item.addItem(new InventoryItem(11, "chips", 4.99, 300));
        item.addItem(new InventoryItem(1, "toothpicks", 0.39, 600));

        System.out.println("All items (toString): " + item);
        System.out.println("Item sku 10: " + item.findItem(10));
        System.out.println("Item sku 11: " + item.findItem(11));
        System.out.println("Item sku 1: " + item.findItem(1));

        item.updateItemQuantity(10, 11);
        item.updateItemQuantity(11, 400);
        item.updateItemQuantity(1, 599);

        System.out.println();

        System.out.println("Quantity updated (sku 10): " + item.findItem(10));
        System.out.println("Quantity updated (sku 11): " + item.findItem(11));
        System.out.println("Quantity updated (sku 1): " + item.findItem(1));

        item.updateItemPrice(10, 3.00);
        item.updateItemPrice(11, 3.99);
        item.updateItemPrice(1, 0.49);

        System.out.println();

        System.out.println("Price updated - sku 10: " + item.findItem(10));
        System.out.println("Price updated - sku 11: " + item.findItem(11));
        System.out.println("Price updated - sku 1: " + item.findItem(1));

        System.out.println();

        System.out.println("seqSearchRec - sku 10: " + item.seqSearchRec(0));
        System.out.println("seqSearchRec - sku 11: " + item.seqSearchRec(1));
        System.out.println("seqSearchRec - sku 1: " + item.seqSearchRec(2));

        System.out.println();

        System.out.println("All items updated (toString): " + item);
        //System.out.println("Items to remove: " + item.removeItem(10) + item.removeItem(11));
        //System.out.println("Items left in inventory: " + item);

        //item.selectSort();
        //System.out.println("SelectSort: " + item);
        System.out.println("seqSearchRec (expected null): " + item.seqSearchRec(2));
        System.out.println("seqSearchRec - sku 1: " + item.seqSearchRec(1));
        System.out.println("seqSearchRec - sku 10: " + item.seqSearchRec(10));
        System.out.println("seqSearchRec - sku 11: " + item.seqSearchRec(11));

        System.out.println("binSearch 10: " + item.binSearch(10));
        System.out.println("binSearch 11: " + item.binSearch(11));

        item.selectSort();
        System.out.println("selectSort: " + item);
        System.out.println(item.size());
        System.out.println(item.getItem(1));
    }//main
}//Warehouse

1 Answer 1

1

You need to create an InventoryItem before add it to the Warehouse object:

public Warehouse readInventoryFile(File inputFile) throws FileNotFoundException {
Warehouse warehouse1 = new Warehouse();
Scanner in = new Scanner(inputFile);

while(in.hasNext()) {
String item = in.nextLine();
int sku = in.nextInt();
double price = in.nextDouble();
int stock = in.nextInt();
String nextLine = in.nextLine();

InventoryItem myItem = new InventoryItem(sku, item, price, stock);
warehouse1.addItem(myItem);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Do I have to declare sku, item, price, and stock as instance variables? Java isn't recognizing those inputs from InventoryItem.
You have these variables declared inside of the while. Maybe you need to declare them before the while and set their values inside.

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.