【发布时间】: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):它将为每个不匹配的供应商打印错误消息。看起来不太理想。