简体   繁体   中英

stuck on how to add an item to a stock list java

I have created a stock controlling program in java, but am stuck on the user input part. The user needs to be able to select "Add Item" from the main menu of the interface, and from there, enter an ID, Description, Price, Quantity, and Re-Order level of the item, and then choose if they want to add another item, or exit back to the main menu.

I have coded in a set of scanners to read the input for the items attributes, but am unsure how to then add them to my Stock List, and also a not certain on what type of loop i should use if the user wants to add another item or navigate back to the main menu.

Anybody have any idea how best to solve either of these problems? Any advice/solutions would be much appreciated. Thanks

STOCKITEM CODE:

public class StockItem {

String itemID;
String itemDesc;
Double price;
int quantity;
int reOrderLevel;
//declaring my 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 a contructor with 5 parameters for my 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 my getters for all 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 my setters for 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 to end of stock list
public void addItem(StockItem item) 
{
    StockList.stock.addLast(item);
}


// Removes item identified by productID from 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 the stock list
//  public String formatStockList()
// {…}


// Returns formatted representation of re-order list
// Items are on this 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 and gets valid option from user
public void doMenu()
{
    System.out.println("StockList Main Menu");
    System.out.println("*******************");
    System.out.println("1. Add an Item");
    System.out.println("2. Delete an 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 for stock list operation
// and 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 and gets valid option from user
public void doMenu()
{
    System.out.println("StockList Main Menu");
    System.out.println("*******************");
    System.out.println("1. Add an Item");
    System.out.println("2. Delete an 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 for stock list operation
// and 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 be private.

StockList has a public constructor but uses a static List - this means that all instances of StockList share the same list - unless this is what you want (which i guess it is not), you should make that list non static. So just make it a private member variable.

StockListInterface should be also be an actual interface if that is your 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 i would advise using deleteItemById since you could easily provide a method called deleteItem that took a StockItem and addItem takes a StockItem as a param not a string. Then make an implementation (this seems to me like overkill here, but that could be argued either way), if it were me a simple StockList class would suffice here.

public class StockListImpl implements StockList {

   private List<StockItem> stockList = new LinkedList<StockItem>();

   public void addItem(StockItem item) {
         stockList.add(item);
   }

  //etc...

Note you should use generics to avoid type casting :

private List<StockItem> = new LinkedList<StockItem>();

This way you will get an iterator that returns StockItems and you wont have to cast. So you can implement removeItemById like this :

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 to your question.

You probably want to use a Scanner I guess - see this answer

How to use readline() method in Java?

I would think you want a method to create each submenu and another method which takes the users input and decides which menu to display. This should be separate from your StockList class.

You can make this as sophisticated as you like, but since I guess you are a beginner a basic example would be :

eg

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 an Item");
     System.out.println("2. Delete an 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 and read next char from scanner.
}

private static void displayDeleteMenu() {
    // display menu and read next char from scanner.
}


private static void displaySubMenu(int option) {

    switch (option) {
      case StockListApp.ADD_ITEM: {
          displayAddItemMenu();
          break;
      }
      case StockListApp.DELETE_ITEM: {
          displayDeleteMenu();
          break;
      }
    }

}

}

Remember to call scanner.close() when the user choose to Exit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM