Reading input from text file in Java -
i need write method reads .txt file , creates new "inventoryitem" object , adds "warehouse" object. warehouse class creates arraylist of inventoryitem. using scanner read text file, not understand how bring values 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(); (int = 0; < 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 = seqsearchrechelper(sku, 0); if (i == -1) { return null; }//if return inventory.get(i); }//seqsearchrec public void selectsort() { int largestindex; int largest; //for (int = inventory.size() - 1; > 0; i--) { for(int = 0; i<inventory.size(); i++){ largest = inventory.get(0).getsku(); largestindex = i; (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 }//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 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
you need create inventoryitem before add 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); }
Comments
Post a Comment