创建一个invoiceline类

时间:2016-03-02 19:41:29

标签: java

发票行类表示发票中记录所售商品和数量的某一行。发票行应包含以下属性:商品已售出:这是类型为Item的实例变量。 数量:一个整数,表示此项目的购买单位数。在您的发票行类中包括以下方法:一个构造函数,它接受以下四个输入参数:项目编号,项目描述,项目价格和数量。然后,构造函数使用输入参数将两个实例变量初始化为两个实例变量的getter和setter方法,因此获取Line Total:计算并返回此行总价的方法(即,数量*项目的价格)。 to String方法,返回发票行的单行描述。描述应包括项目的详细信息,购买的数量和生产线的总成本。

***我试图将其他变量从名为item class的不同类中获取到invoiceline类,但我似乎无法做到。到目前为止,我有这个。任何帮助!?

public class InvoiceLine {
//Instance Variables
private Item itemSold;
private int quantity;

// Constructors
public InvoiceLine(){}

public InvoiceLine(String itemSold, int quantity){
    itemSold = new Item(itemSold);
    this.quantity = quantity;   
}
//Getters 
public Item getItemSold(){
    return itemSold;
}
public int getQuantity(){
    return quantity;
}
//Setters
private void setItemSold(Item itemSold){
    this.itemSold = itemSold;
}
private void setQuantity(int quantity){
    this.quantity = quantity;
}
// Methods
public double getLineTotal(double total){
    total = itemPrice * quantity;
    return total;
}
public String toString(){
    String invoiceLine = "";
    invoiceLine = (itemSold + " brought "+ quantity + ", which the total cost is "+ total);
    return invoiceLine;
}

}

项目类别:

public class Item {
//Instance Variables
private int itemNum;
private String itemDescription;
private double itemPrice;

//Constructors
public Item(){}

//Initialize the three variables
public Item(int itemN, String itemDesc, double itemPri){
    this.itemNum = itemN;
    this.itemDescription = itemDesc;
    this.itemPrice = itemPri;
}
public Item(String itemDesc){
    this.itemDescription = itemDesc;
    this.itemNum = 0;
    this.itemPrice = 0;
}
//Getters
public int getItemNum(){
    return itemNum;
}
public String getItemDescription(){
    return itemDescription;
}
public double getItemPrice(){
    return itemPrice;
}
//Setters
private void setItemNum(int itemN){
    itemNum = itemN;
}
private void setItemDescription(String itemDesc){
    itemDescription = itemDesc;
}
private void setItemPrice(double itemPri){
    itemPrice = itemPri;
}
// toString method output description of the item
public String toString(){
    String itemDetail = "";
    itemDetail = ("Item number is "+ itemNum + ", which is a "+ itemDescription + " and price is " + itemPrice);
    return itemDetail;
}

}

1 个答案:

答案 0 :(得分:0)

尝试将itemSold = new Item(itemSold);更改为this.itemSold = new Item(itemSold); 在这种情况下,参数itemSold会遮蔽属性itemSold,因此要访问该属性,您必须在其前面添加this