2016-04-18 140 views
0

对不起,这可能是一个简单的决心,但我有麻烦编写代码创建一个对象将使用来自另一个类的构造参数参数在Java中。基本上,我有这个Inv对象有一定的参数,我试图创建一个Warehouse对象,只使用来自Inv对象的特定参数,然后在Warehouse Transaction类中使用它。我可以在Inv对象构造函数中执行if语句吗?任何人,我会稍后将这个对象推入堆栈,但是我无法开发创建第二个对象的逻辑。请帮助,我对此很感兴趣。试图从一个类的构造函数使用一些参数从另一个类的构造函数

public class InvTrans 

{

private int InvTransIndex = 0; 
private Inv[] transactionArray; 

public InvTrans() 
{ 
    this.transactionArray = new Inv[100]; 
} 

public static void main(String args[]) 
{ 
    InvTrans widget = new InvTrans(); 



    Inv order1 = new Inv("March 5th, 2016", "Received", 20, 0001, 2.10, 2.20); 
    Inv order2 = new Inv("March 6th, 2016", "Received", 100, 0002, 2.10, 2.50); 
    Inv order3 = new Inv("March 7th, 2016", "Received", 100, 0003, 2.10, 2.50); 
    Inv order4 = new Inv("March 12th, 2016", "Sold", 140, 0004, 2.40, 2.60); 


    widget.addLine(order1); 
    widget.addLine(order2); 
    widget.addLine(order3); 
    widget.addLine(order4); 



    for (int counter = 0; counter < widget.transactionArray.length; counter++) 
    { 
     widget.transactionArray[counter].display(); 
    } 

} 

public void addLine(Inv a) 
{ 

    transactionArray[InvTransIndex] = a; 
    InvTransIndex = InvTransIndex + 1; 
} 
} 

class Inv 
{ 
String date, type; 
int units, referenceNumber; 
double costPerUnit, pricePerUnit; 

Inv(String d, String t, int u, int r, double c, double p) { 
    this.date = d; 
    this.type = t; 
    this.units = u; 
    this.referenceNumber = r; 
    this.costPerUnit = c; 
    this.pricePerUnit = p; 


    Warehouse warehouseTransaction = new Warehouse(this.date, this.type, this.units, this.costPerUnit); 

} 

public void display() 
{ 
    System.out.println(this.date + "\t" + this.type + "\t" + this.units + "\t\t\t\t\t" + this.referenceNumber + "\t\t\t\t\t" + this.costPerUnit + "\t\t\t" + this.pricePerUnit); 
} 
}  

public class Warehouse 
{ 
    String date, type = ""; 
    int units = 0; 
    double costPerUnit = 0; 
    Warehouse(String d, String t, int u, double c) 
    { 
     date = d; 
     type = t; 
     units = u; 
     costPerUnit = c; 
    } 
} 
+0

我不知道你想要完成的任务。通常情况下,您有一个仓库以及存储在仓库中的物品。 –

+0

如果您想在每次创建一个Inv时创建Warehouse对象,那么当前的实现有什么问题?我想让仓库成为一个类变量,以便稍后可以访问它 –

+0

@Toaster我想知道如何创建一个仓库对象,每次创建Inv对象时,Inv的参数都可以满足它。我不知道该怎么去做。 –

回答

0

可以使用工厂模式:

创建一个名为InvFactory类。这个类将有权访问仓库(顺便说一句,仓库是你存储所有物品的地方......创建一个新的对象仓库每次创建一个Inv对我来说都没有意义,将改为调用这些对象WarehouseItem)。当你想创建一个INV,你将调用工厂,而不是INV构造

public class InvFactory 
{ 
    private LinkedList<WarehouseItem> _warehouse = new LinkedList<WarehouseItem; 

    public CreateInv(String d, String t, int u, int r, double c, double p) 
    { 
     Inv inv = new Inv(d, t, u, r, c, p); 
     _warehouse.add(new WarehouseItem(d, t, u, c); 
     return inv; 
    } 

    public LinkedList<WarehouseItem> getWarehouse() 
    { 
     return _warehouse; 
    } 
} 

在你的主要方法,然后你可以通过

InvFactory factory = new InvFactory(); 

Inv order1 = factory.CreateInv("March 5th, 2016", "Received", 20, 0001, 2.10, 2.20); 
Inv order2 = factory.CreateInv("March 6th, 2016", "Received", 100, 0002, 2.10, 2.50); 
Inv order3 = factory.CreateInv("March 7th, 2016", "Received", 100, 0003, 2.10, 2.50); 
Inv order4 = factory.CreateInv("March 12th, 2016", "Sold", 140, 0004, 2.40, 2.60); 

而更换

Inv order1 = new Inv("March 5th, 2016", "Received", 20, 0001, 2.10, 2.20); 
Inv order2 = new Inv("March 6th, 2016", "Received", 100, 0002, 2.10, 2.50); 
Inv order3 = new Inv("March 7th, 2016", "Received", 100, 0003, 2.10, 2.50); 
Inv order4 = new Inv("March 12th, 2016", "Sold", 140, 0004, 2.40, 2.60); 

此时,您可以通过调用来查看列表中的所有项目。

factory.getWarehouse(); 

我希望这有助于

相关问题