interface - stuck on how to add an item to a stock list java -
i have created stock controlling program in java, stuck on user input part. user needs able select "add item" main menu of interface, , there, enter id, description, price, quantity, , re-order level of item, , choose if want add item, or exit main menu.
i have coded in set of scanners read input items attributes, unsure how add them stock list, , not on type of loop should use if user wants add item or navigate main menu.
anybody have idea how best solve either of these problems? advice/solutions appreciated. thanks
stockitem code:
public class stockitem { string itemid; string itemdesc; double price; int quantity; int reorderlevel; //declaring instance variables public stockitem (string itemid, string itemdesc, double price, int quantity, int reorderlevel) { this.itemid = itemid; this.itemdesc = itemdesc; this.price = price; this.quantity = quantity; this.reorderlevel = reorderlevel; } //declaring contructor 5 parameters instance variables public string getitemid() { return itemid; } public string getitemdesc() { return itemdesc; } public double getprice() { return price; } public int getquantity() { return quantity; } public int getreorderlevel() { return reorderlevel; } //creating getters instance variables public void setprice(double price) { this.price = price; } public void setquantity(int quantity) { this.quantity = quantity; } public void setreorderlevel(int reorderlevel) { this.reorderlevel = reorderlevel; } //creating setters 3 instance variables public string tostring() { return getclass().getname() + "itemid: " + itemid + "itemdesc: " + itemdesc + "price: " + price + "quantity: " + quantity + "reorderlevel: " + reorderlevel; } //public string format() {} }
stocklist:
import java.util.*; public class stocklist { public static linkedlist<stockitem> stock = new linkedlist<stockitem>(); public stocklist() {}; // adds item end of stock list public void additem(stockitem item) { stocklist.stock.addlast(item); } // removes item identified productid stock list public void deleteitem(string itemid) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { itr.remove(); break; } } } // updates price of existing item public void updateitemprice(string itemid, double price) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setprice(price); break; } } } // updates quantity of existing item public void updateitemquantity(string itemid, int quantity) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setquantity(quantity); break; } } } // updates re-order level of existing item public void updatereorderlevel(string itemid, int reorderlevel) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setreorderlevel(reorderlevel); break; } } } @override public string tostring() { return "" + stock; } // returns formatted representation of stock list // public string formatstocklist() // {…} // returns formatted representation of re-order list // items on list if quantity < reorderlevel // public string formatreorderlist() // {…} listiterator listiterator() { throw new unsupportedoperationexception("not yet implemented"); } }
stocklistinterface:
import java.util.*; public class stocklistinterface { private stocklist stock = null; public stocklistinterface(stocklist stock){} // displays main menu , gets valid option user public void domenu() { system.out.println("stocklist main menu"); system.out.println("*******************"); system.out.println("1. add item"); system.out.println("2. delete item"); system.out.println("3. update item price"); system.out.println("4. update item quantity"); system.out.println("5. update reorder level"); system.out.println("6. print stock list"); system.out.println("7. print reorder list"); system.out.println("8. exit"); system.out.println("select option [1-8] :>"); } // obtain input stock list operation // , invoke operation private void doadditem(stockitem item) { stocklist.stock.addlast(item); } private void dodeleteitem(string itemid) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { itr.remove(); break; } } private void doupdateitemprice(string itemid, double price) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setprice(price); break; } } } private void doupdateitemquantity(string itemid, int quantity) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setquantity(quantity); break; } } } private void doupdatereorderlevel(string itemid, int reorderlevel) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setreorderlevel(reorderlevel); break; } } } // display contents of stock list private void doprintstocklist() {} // display contents of re-order list private void doprintreorderlist() {} }
stocklistapp:
import java.util.*; public class stocklistinterface { private stocklist stock = null; public stocklistinterface(stocklist stock){} // displays main menu , gets valid option user public void domenu() { system.out.println("stocklist main menu"); system.out.println("*******************"); system.out.println("1. add item"); system.out.println("2. delete item"); system.out.println("3. update item price"); system.out.println("4. update item quantity"); system.out.println("5. update reorder level"); system.out.println("6. print stock list"); system.out.println("7. print reorder list"); system.out.println("8. exit"); system.out.println("select option [1-8] :>"); } // obtain input stock list operation // , invoke operation private void doadditem(stockitem item) { stocklist.stock.addlast(item); } private void dodeleteitem(string itemid) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { itr.remove(); break; } } private void doupdateitemprice(string itemid, double price) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setprice(price); break; } } } private void doupdateitemquantity(string itemid, int quantity) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setquantity(quantity); break; } } } private void doupdatereorderlevel(string itemid, int reorderlevel) { listiterator itr = stock.listiterator(); while(itr.hasnext()) { stockitem item = (stockitem)itr.next(); if(item.getitemid().equals(itemid)) { item.setreorderlevel(reorderlevel); break; } } } // display contents of stock list private void doprintstocklist() {} // display contents of re-order list private void doprintreorderlist() {} }
a couple of comments first :
your instance variables in stockitem should private.
stocklist has public constructor uses static list - means instances of stocklist share same list - unless want (which guess not), should make list non static. make private member variable.
stocklistinterface should actual interface if intention.
public interface stocklist { public void additem(stockitem item); public void deleteitembyid(string itemid); // updates price of existing item public void updateitemprice(string itemid, double price); /// etc. }
note advise using deleteitembyid since provide method called deleteitem took stockitem , additem takes stockitem param not string. make implementation (this seems me overkill here, argued either way), if me simple stocklist class suffice here.
public class stocklistimpl implements stocklist { private list<stockitem> stocklist = new linkedlist<stockitem>(); public void additem(stockitem item) { stocklist.add(item); } //etc...
note should use generics avoid type casting :
private list<stockitem> = new linkedlist<stockitem>();
this way iterator returns stockitems , wont have cast. can implement removeitembyid :
public void removeitembyid(string itemid) { iterator<stockitem> itr = stocklist.listiterator(); while(itr.hasnext()) { stockitem item = itr.next(); if(item.getitemid().equals(itemid)) { itr.remove(); break; } } }
so question.
you want use scanner guess - see answer
how use readline() method in java?
i think want method create each submenu , method takes users input , decides menu display. should separate stocklist class.
you can make sophisticated like, since guess beginner basic example :
e.g.
class stocklistapp { private static scanner scanner = new scanner(system.in); private static final int add_item = 1; private static final int delete_item = 2; private stocklistapp() {} public static void displayrootmenu() { system.out.println("stocklist main menu"); system.out.println("*******************"); system.out.println("1. add item"); system.out.println("2. delete item"); system.out.println("3. update item price"); system.out.println("4. update item quantity"); system.out.println("5. update reorder level"); system.out.println("6. print stock list"); system.out.println("7. print reorder list"); system.out.println("8. exit"); system.out.println("select option [1-8] :>"); int choice = scanner.nextint(); displaysubmenu(choice); } private static void displayadditemmenu() { // display menu , read next char scanner. } private static void displaydeletemenu() { // display menu , read next char scanner. } private static void displaysubmenu(int option) { switch (option) { case stocklistapp.add_item: { displayadditemmenu(); break; } case stocklistapp.delete_item: { displaydeletemenu(); break; } } } }
remember call scanner.close() when user choose exit.
Comments
Post a Comment