2016-04-18 141 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(); 

我希望這有助於

相關問題