0

申し訳ありませんが、これは簡単な解決策かもしれませんが、Java で別のクラス コンストラクターの引数からパラメーターを使用する 1 つのオブジェクトを作成するためのコードを書くのに問題があります。基本的に、特定のパラメーターを持つこの Inv オブジェクトがあり、Inv オブジェクトの特定のパラメーターのみを使用して Warehouse オブジェクトを作成しようとしています。これは Warehouse Transaction クラスで使用されます。Inv オブジェクト コンストラクター内で if ステートメントを実行できますか? とにかく、後でこのオブジェクトをスタックにプッシュしますが、その 2 番目のオブジェクトを作成するためのロジックを開発できませんでした。助けてください、私はこれに非常に慣れていません。

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;
    }
}
4

1 に答える 1

0

Factory パターンを使用できます。

InvFactory というクラスを作成します。このクラスは Warehouse にアクセスできます (ちなみに、Warehouse はすべてのアイテムを保管する場所です... Inv を作成するたびに新しいオブジェクト Warehouse を作成することは、私にはあまり意味がありません。代わりにこれらのオブジェクトを 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;
    }
}

メインメソッドでは、次に置き換えることができます

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);

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);

その時点で、呼び出すことでリスト内のすべてのアイテムを表示できます。

factory.getWarehouse();

これが役立つことを願っています

于 2016-04-20T06:36:48.467 に答える