我有四节课。
Supplier
,其中包含name
,id
。SupplierDirectory
Arraylist
类型为Supplier
。Product
,其中包含name
。ProductDirectory
ArrayList
类型Product
。Product.java
public class Product {
//private int productId;
private String productName;
/* public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
*/
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
/* public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isAvailability() {
return availability;
}
public void setAvailability(boolean availability) {
this.availability = availability;
}*/
@Override
public String toString()
{
return productName;
}
}
ProductDirectory.java
public class ProductDirectory {
private ArrayList<Product> prodDir = new ArrayList<Product>();
public ArrayList<Product> getProdDir() {
return prodDir;
}
public void setProdDir(ArrayList<Product> prodDir) {
this.prodDir = prodDir;
}
public Product addProduct()
{
Product prod = new Product();
prodDir.add(prod);
return prod;
}
}
Supplier.java
public class Supplier {
String supplierName;
private ProductDirectory proddirectory = new ProductDirectory();
public String getSupplierName() {
return supplierName;
}
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}
public ProductDirectory getProddirectory() {
return proddirectory;
}
public void setProddirectory(ProductDirectory proddirectory) {
this.proddirectory = proddirectory;
}
}
SupplierDirectory.java
public class SupplierDirectory {
private ArrayList<Supplier> supplierDir = new ArrayList<Supplier>();
public ArrayList<Supplier> getSupplierDir() {
return supplierDir;
}
public void setSupplierDir(ArrayList<Supplier> supplierDir) {
this.supplierDir = supplierDir;
}
public Supplier addsupplier()
{
Supplier sup = new Supplier();
supplierDir.add(sup);
return sup;
}
public void deleteSupplier(Supplier sup)
{
//Supplier sup = new Supplier();
supplierDir.remove(sup);
}
}
以下是我的主要方法。
public static void main (String[] args){
int i ,j;
String supplierName;
String productName;
SupplierDirectory supDir = new SupplierDirectory();
ProductDirectory prodDir = new ProductDirectory();
Product p;
Supplier Sup = new Supplier();
Scanner user_input = new Scanner(System.in);
for (i = 0; i<=2; i++)
{
System.out.println("Enter Supplier to add");
supplierName = user_input.next();
supDir.addsupplier();
for (j=0;j<=3;j++)
{
System.out.println("Enter Product to add");
productName = user_input.next();
prodDir.addProduct();
System.out.println("You have entered Product"+ prodDir);
System.out.println("Product List :"+ Sup.getProddirectory().getProdDir());
}
}
// Scanner user_input = new Scanner(System.in);
System.out.println("You have entered "+ userInput());
System.out.println("You have entered "+ userInput());
supDir.addsupplier();
System.out.println("Suppliers "+ supDir.getSupplierDir());
}
public static String userInput ()
{
String supplierName;
Scanner user_input = new Scanner(System.in);
System.out.println("Enter Supplier to add");
supplierName = user_input.next();
return supplierName;
}
}
运行后,我得到以下结果:
输入供应商以添加
ABC
输入要添加的产品
戴尔
您已输入ProductBusiness.ProductDirectory@647e05
产品清单:[]
我该如何解决这个问题?
答案 0 :(得分:1)
您应该更改这些行:
prodDir.addProduct();
System.out.println("You have entered Product"+ prodDir);
System.out.println("Product List :"+ Sup.getProddirectory().getProdDir());
代替:
Product newProduct = new Product();
newProduct.setName(productName);
prodDir.getProdDir().put(newProduct);
System.out.println("You have entered Product"+ Arrays.toString(prodDir.getProdDir().toArray()));
Sup.setProddirectory(prodDir);
System.out.println("Product List :"+ Arrays.toString(Sup.getProddirectory().getProdDir().toArray()));
还会向您的toString()
类添加一个新的Product
方法,如下所示:
@Override
public String toString() {
return name;
}
来自productDirectory
的{{1}}列表始终为空,因为您实例化了这样的新Supplier
:
Supplier
但你从不打电话给Supplier Sup = new Supplier();
你应该添加一行
.setProddirectory()
行前
Sup.setProddirectory(prodDir);