【问题标题】:How do I compare attributes of an object in an ArrayList against Scanner input?如何将 ArrayList 中对象的属性与 Scanner 输入进行比较?
【发布时间】:2017-12-08 01:55:48
【问题描述】:

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

每个“供应商”对象都有一个“产品”对象的 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;
}

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

在下面创建的运行时只有 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() 的结构有关。

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

【问题讨论】:

  • 一些初步的想法...当我阅读整个内容时会添加更多内容..public ArrayList populateDatabase(ArrayList supDB).. subDB 的用途是什么?方法中没有提到。 addNewProduct(Scanner sc):它将为每个不匹配的供应商打印错误消息。看起来不太理想。

标签: java eclipse arraylist


【解决方案1】:

我建议您使用 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;
}

这是假设您在 Supplier 对象中为供应商代码定义了 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");
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多