抱歉,这可能是一个简单的解决方案,但我在编写用于创建一个对象的代码时遇到了麻烦,该对象将在 Java 中使用来自另一个类构造函数参数的参数。本质上,我有这个具有某些参数的 Inv 对象,并且我正在尝试仅使用 Inv 对象中的某些参数创建一个 Warehouse 对象,然后将在 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;
}
}