基本上,我有多个类,并且尝试为客户购买的每个项目获取LineItem数组。 LineItem包括UPC,说明,价格,数量,小计和折扣,它们全部存储在单独的类中。我试图得到的结果是,当您使用方法addItemToSaleList时,它将添加到数组中。我需要使用数组而不是数组列表,因此我必须将数组复制到临时数组,然后重新创建一个新数组,将其添加到该数组可以存储的编号中,然后重新复制。我卡住了要生成的数组。下面是我的代码

public class Product {
private double price;
private String description;
private String ProductCode;
private DiscountStrategy discoutStrategy;

public Product(double price, String description, String ProductCode, DiscountStrategy discoutStrategy) {
    this.price = price;
    this.description = description;
    this.ProductCode = ProductCode;
    this.discoutStrategy = discoutStrategy;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getProductCode() {
    return ProductCode;
}

public void setProductCode(String ProductCode) {
    this.ProductCode = ProductCode;
}

public DiscountStrategy getDiscountStrategy() {
    return discoutStrategy;
}

public void setDiscoutStrategy(DiscountStrategy discoutStrategy) {
    this.discoutStrategy = discoutStrategy;
}
}


public class LineItem {
private Product product;
private double quantity;

public LineItem(Product product, double quantity) {
    this.product = product;
    this.quantity = quantity;
}

//Calculates the Discount Amount whether or not it's a percentage or dollar
//off
public double getDiscountAmount () {
    return product.getDiscountStrategy().getDiscount(product.getPrice(), quantity);
}

//Calculates the Subtotal, gets the quantity from the DiscountStrategy and then
//the price from the product
public double getSubTotal() {
    return quantity * product.getPrice();
}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

 public double getQuantity() {
    return quantity;
}

public void setQuantity(double quantity) {
    this.quantity = quantity;
}



public class Receipt {
private LineItem[] lineItem = new LineItem[0];

public Receipt(LineItem[] lineItem) {
    this.lineItem = lineItem;
}


public void addProductToTotalSale(LineItem li) {
    addItemToSaleList();
}

public void addItemToSaleList() {
   LineItem[] tempItemList = new LineItem[lineItem.length + 1];

   for (int i = 0; i < tempItemList.length; i++) {
       tempItemList[i] = lineItem[i];
   }

   lineItem = new LineItem[tempItemList.length];

   for (int j = 0; j < lineItem.length; j++) {
       lineItem[j] = tempItemList[j];
   }
}

public LineItem[] getLineItem() {
    return lineItem;
}

最佳答案

我不确定为什么要制作两个新数组。你只需要一个...

public void addProductToTotalSale(LineItem li) {
    addItemToSaleList();
    lineItem[lineItem.length-1] = li;
}

public void addItemToSaleList() {
   LineItem[] tempItemList = new LineItem[lineItem.length + 1];

   for (int i = 0; i < tempItemList.length; i++) {
     tempItemList[i] = lineItem[i];
   }
   lineItem = tempItemList;
}

08-06 20:56