如何比较ArrayList中对象的属性与Scanner输入?

时间:2017-12-08 01:55:49

标签: java eclipse arraylist

我有一个简单的供应商/产品'在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;
}

然后我有另一个包含Supplier对象&#34; supDatabase&#34;

的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()的结构有关。

任何人都可以推荐任何重组或者我可以采用的任何其他技术吗?(我意识到我现在使用的代码是非常基本的,所以我对任何新的开放我可以带来整理的功能!)

1 个答案:

答案 0 :(得分: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");
    }
}

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