我正在尝试将一个ElementList中的对象(例如SupplierID)中的元素从Supplier ArrayList传递到Product ArrayList,其中ProductID列表中的SupplierID是Supplier List中的SupplierID。输入由用户给出。
这是我的供应商代码:
package main;
import java.util.ArrayList;
import java.util.Scanner;
public class Supplier {
private String s_SupplierID;
private String s_ContactName;
private String s_Company;
private String s_Email;
private String s_Phone;
ArrayList<Supplier> spl = new ArrayList<Supplier>();
Scanner keyboard = new Scanner(System.in);
public Supplier()
{
}
public Supplier (String s_SupplierID, String s_ContactName, String s_Company, String s_Email, String Phone){
this.s_SupplierID = s_SupplierID;
this.s_ContactName = s_ContactName;
this.s_Company = s_Company;
this.s_Email = s_Email;
this.s_Phone = Phone;
}
public void addSupplier(){
System.out.println("Please Enter Supplier ID: ");
String ID = keyboard.nextLine();
System.out.println("Please Enter Name: ");
String Name = keyboard.nextLine();
System.out.println("Please Enter Company Name: ");
String Company = keyboard.nextLine();
System.out.println("Please Enter Email: ");
String Email = keyboard.nextLine();
System.out.println("Please Enter Phone: ");
String Phone = keyboard.nextLine();
spl.add(new Supplier(ID,Name,Company,Email,Phone));
}
这是我的产品代码:
package main;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
public class Product{
private String p_ProductID;
private String p_Name;
private int p_SerialNum;
private double p_Price;
private int p_Quantity;
private Supplier p_Supplier;
private int p_ExpiryDate;
private double p_Discount;
private double p_BulkDiscount;
private String p_SupplierID;
Scanner keyboard = new Scanner(System.in);
ArrayList<Product> ppl = new ArrayList<Product>();
public Product(){
}
public Product(Supplier s,String p_ProductID, String p_Name, int p_SerialNum, double p_Price, int p_Quantity, double p_Discount, double p_BulkDiscount){
p_Supplier = s;
this.p_ProductID = p_ProductID;
this.p_Name = p_Name;
this.p_SerialNum = p_SerialNum;
this.p_Price = p_Price;
this.p_Quantity = p_Quantity;
this.p_SupplierID = p_Supplier.getSupplierID();
this.p_Discount = p_Discount;
this.p_BulkDiscount = p_BulkDiscount;
}
public Product(String p_ProductID, String p_Name, int p_SerialNum, double p_Price, double p_Discount, double p_BulkDiscount){
this.p_ProductID = p_ProductID;
this.p_Name = p_Name;
this.p_SerialNum = p_SerialNum;
this.p_Price = p_Price;
this.p_SupplierID = p_Supplier.getSupplierID();
this.p_Discount = p_Discount;
this.p_BulkDiscount = p_BulkDiscount;
}
public void addProduct(){
System.out.println("Please Enter ProductID: ");
String ID = keyboard.nextLine();
System.out.println("Please Enter Product Name: ");
String Name = keyboard.nextLine();
System.out.println("Please Enter Product Serial Number: ");
int SerialNum = keyboard.nextInt();
System.out.println("Please Enter Product Price: ");
Double Price = keyboard.nextDouble();
System.out.println("Please Enter Product Discount: ");
Double Discount = keyboard.nextDouble();
System.out.println("Please Enter Product Bulk Discount: ");
Double BulkDiscount = keyboard.nextDouble();
ppl.add(new Product( ID,Name,SerialNum, Price, Discount, BulkDiscount));
}
以下是我运行它的主要方法: UPDATE
package main;
import java.util.*;
public class main {
public static void main(String[] args) { // testing add/update/remove on supplier
//ArrayList<Supplier> spl = new ArrayList<Supplier>();
//Scanner scan = new Scanner(System.in);
//System.out.println("Supplier id?");
//Supplier.s_SupplierID = scan.nextInt();
Supplier s = new Supplier();
Product p = new Product();
s.addSupplier();
p.addProduct();
}
当供应商构造函数创建包含SupplierID,ContactName,Company,Email和phone的供应商时,我想在添加新产品时将此SupplierID传递到Product Constructor中的SupplierID字段。我想要实现的结果是在添加供应商之后立即将SupplierID分配给添加的产品。我该怎么做呢?感谢。