java - 如何将 ArrayList 中对象的属性与 Scanner 输入进行比较?

标签 java eclipse arraylist

我有一个简单的“供应商/产品”Java 应用程序在 Eclipse 控制台中运行。

每个“供应商”对象都有一个“产品”对象的 ArrayList。

public class Supplier {

private int supCode;
private String supName;
private Address supAddress;
private SupRegion supRegion;
private ArrayList<Product> supProducts;

public Supplier(int supCode, String supName, Address supAddress, SupRegion supRegion,
        ArrayList<Product> supProducts) {
    super();
    this.supCode = supCode;
    this.supName = supName;
    this.supAddress = supAddress;
    this.supRegion = supRegion;
    this.supProducts = supProducts;
}

然后我有另一个包含供应商对象“supDatabase”的 ArrayList

运行时仅存在 2 个供应商,创建如下:

//Suppliers
Supplier hendys = new Supplier(101, "Hendersons" , add1, SupRegion.UNITED_KINGDOM, hendysPros); 
Supplier palasFoods = new Supplier(102, "Palas Foods", add2, SupRegion.UNITED_KINGDOM, palasPros);


ArrayList<Supplier> supDatabase = new ArrayList<Supplier>();

//method to add all products into ProductArrayList
//also to add all suppliers to SupplierArrayList
public ArrayList<Supplier> populateDatabase(ArrayList<Supplier> supDB) {

    hendysPros.add(remote);
    hendysPros.add(remote1);
    hendysPros.add(remote2);
    hendysPros.add(remote3);

    supDatabase.add(hendys);

    palasPros.add(strawberries);
    palasPros.add(raspberries);
    palasPros.add(blueberries);

    supDatabase.add(palasFoods);


    return supDatabase;
}

我现在正在尝试添加功能来向特定供应商添加新产品。

以下方法尝试将用户输入的供应商“代码”与 ArrayList (supDatabase) 中的数据进行比较:

private static void addNewProduct(Scanner sc)
{       
    System.out.println("Enter supplier code you would like to add a product to: ");
    int choice = getUserInput(sc);

    //***PROBLEM*** - trying to validate whether the user has entered an existing Supplier code i.e.
    //does the integer entered match a Suppliers's code, contained within a Supplier object
    //which are then contained within an ArrayList (supDatabase)


    for (int i=0; i<supDB.supDatabase.size();i++)           //for loop to cycle through ArrayList of Supplier objects??
    {
        if (choice == supDB.supDatabase.get(i).getSupCode())
        {
            System.out.println("You have chosen to add a product to: " + supDB.supDatabase.get(i).getSupName());

            Product newProduct  = new Product(0, "", "", 0, 0, false);

            System.out.println("Enter a code for the new product: ");
            newProduct.setProCode(sc.nextInt());

            System.out.println("Enter make: ");
            newProduct.setProMake(sc.next());

            System.out.println("Enter model: ");
            newProduct.setProModel(sc.next());

            System.out.println("Enter price: ");
            newProduct.setProPrice(sc.nextDouble());

            System.out.println("Enter the quantity available: ");
            newProduct.setProQtyAvailable(sc.nextInt());

            System.out.println("Is the product available? (Y/N)");
            newProduct.setProDiscontinued(sc.hasNext());                    

            //add the new created Product to the database ArrayList
            supDB.supDatabase.get(i).getSupProducts().add(newProduct);
        }
        else
        {
            System.out.println("Supplier code not present. Try again");
        }
    }

}

比较仅适用于 ArrayList 中的第一个供应商,并且无法识别它之后的任何内容。我知道这与 addNewProduct() 的结构有关。

任何人都可以推荐我可以为此采用的任何重组或任何其他技术吗?(我意识到我目前使用的代码非常基本,因此我对任何我可以采用的新功能持开放态度可以带进来收拾东西!)

最佳答案

我建议您使用 HashMap 来存储和查找供应商,而不是 ArrayList。以下是该函数的不同之处。

Map<Integer, Supplier> supDatabase = new HashMap<Integer, Supplier>();

//method to add all products into ProductArrayList
//also to add all suppliers to SupplierArrayList
public Map<Integer, Supplier> populateDatabase() {
    hendys.add(prod1);
    hendys.add(prod2);
    hendys.add(prod3);
    hendys.add(prod4);

    supDatabase.put(hendys.getSupCode(), hendys);

    palasFoods.add(strawberries);
    palasFoods.add(raspberries);
    palasFoods.add(blueberries);

    supDatabase.add(palasFoods.getSupCode(), palasFoods);


    return supDatabase;
}

假设您在供应商对象中为供应商代码定义了 getter。

这将使 addNewProduct 如下所示:

private static void addNewProduct(Scanner sc)
{       
    System.out.println("Enter supplier code you would like to add a product to: ");
    int choice = getUserInput(sc);

    Map<Integer, Supplier> supDatabase = new HashMap<Integer, Supplier>();

    if (supDatabase.containsKey(sc)) {
        System.out.println("You have chosen to add a product to: " + supDB.supDatabase.get(i).getSupName());

        Product newProduct  = new Product(0, "", "", 0, 0, false);

        System.out.println("Enter a code for the new product: ");
        newProduct.setProCode(sc.nextInt());

        System.out.println("Enter make: ");
        newProduct.setProMake(sc.next());

        System.out.println("Enter model: ");
        newProduct.setProModel(sc.next());

        System.out.println("Enter price: ");
        newProduct.setProPrice(sc.nextDouble());

        System.out.println("Enter the quantity available: ");
        newProduct.setProQtyAvailable(sc.nextInt());

        System.out.println("Is the product available? (Y/N)");
        newProduct.setProDiscontinued(sc.hasNext());                    

        //add the new created Product to the database ArrayList
        supDatabase.get(sc).getSupProducts().add(newProduct);
    }
    else
    {
        System.out.println("Supplier code not present. Try again");
    }
}

如果出现错误,如果您希望用户重试,您可能需要将其放入循环中,以便再次要求用户输入。如果没有供应商或用户想要退出,在其上放置一个计数器或退出循环可能是个好主意。

关于java - 如何将 ArrayList 中对象的属性与 Scanner 输入进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47706589/

相关文章:

java - Eclipse 开普勒 SR2 + Java 8

java - 如何在Java中创建文件列表?

java - 将 ArrayList<String> 转换为 byte[]

java - 如何使用jni将java中的Arraylist转换为c中的lpwstr

java - 自定义 RecordReader 初始化未调用

java - 为什么我的 Apache Tomcat 不能执行简单的 JSP 选择数据库程序?

java - 如何避免在 Java swing 代码中提供路径

c++ - 如何在以脚本启动的 Eclipse 中运行和调试 C++ 应用程序?

java - 我可以使用 logback 或其他方式登录到单独的 Eclipse 控制台吗?

java - 使用 POI 应用特定的单元格数据格式