gpt4 book ai didi

java - 停留在如何将项目添加到库存列表java上

转载 作者:行者123 更新时间:2023-12-01 14:37:22 24 4
gpt4 key购买 nike

我已经用java创建了一个库存控制程序,但卡在用户输入部分。用户需要能够从界面的主菜单中选择“添加商品”,然后输入商品的 ID、描述、价格、数量和重新订购级别,然后选择是否要添加添加另一个项目,或退出回到主菜单。

我已经在一组扫描仪中进行了编码,以读取商品属性的输入,但不确定如何将它们添加到我的库存列表中,并且也不确定如果用户想要,我应该使用哪种类型的循环添加另一个项目或导航回主菜单。

有人知道如何最好地解决这些问题吗?任何建议/解决方案将不胜感激。谢谢

库存代码:

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() {}





}

库存 list :

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");
}



}

StockList接口(interface):

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() {}
}

最佳答案

首先有几点评论:

StockItem 中的实例变量应该是私有(private)的。

StockList 有一个公共(public)构造函数,但使用静态列表 - 这意味着 StockList 的所有实例共享相同的列表 - 除非这是您想要的(我猜它不是),否则您应该将该列表设为非静态。所以只需将其设为私有(private)成员变量即可。

StockListInterface 也应该是一个实际的接口(interface)(如果这是您的意图)。

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.
}

注意,我建议使用deleteItemById,因为您可以轻松提供一个名为deleteItem的方法,该方法采用StockItem,而addItem采用StockItem作为参数而不是字符串。然后进行一个实现(在我看来,这似乎有点矫枉过正,但无论哪种方式都可以争论),如果是我,一个简单的 StockList 类就足够了。

public class StockListImpl implements StockList {

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

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

//etc...

请注意,您应该使用泛型来避免类型转换:

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

这样,您将获得一个返回 StockItems 的迭代器,并且无需进行强制转换。所以你可以像这样实现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;
}
}
}

所以回答你的问题。

我猜你可能想使用扫描仪 - 请参阅此答案

How to use readline() method in Java?

我认为您需要一种方法来创建每个子菜单,以及另一种方法来接受用户输入并决定显示哪个菜单。这应该与您的 StockList 类分开。

您可以根据自己的喜好使其复杂化,但由于我猜您是初学者,所以基本示例如下:

例如

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;
}
}

}

}

当用户选择退出时,请记住调用scanner.close()。

关于java - 停留在如何将项目添加到库存列表java上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16355944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com