2014-02-27 56 views
0

基本上我有多個類,我試圖爲客戶購買的每個項目獲取一個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; 
} 
+1

我只剔除了你的問題,但是從描述中可以考慮使用'ArrayList',因爲'Arrays'是固定長度的,你可以追加到'ArrayList'的末尾。 – Dan

+1

爲什麼你不能使用'ArrayList'? –

+0

該項目的要求要求我使用一個數組。這是爲我的java類 – JStudz

回答

0

我不知道你爲什麼要做兩個新的數組。你只需要一個...

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; 
} 
+0

第一個數組lineItem沒有長度,並且每當客戶添加一個新產品數組將增加。這就是爲什麼我有兩個陣列,但我發現你的代碼有一個捷徑。謝謝。 – JStudz

1

我會刪除addItemToSaleList()和實施addProductToTotalSale(LineItem)像這樣

public void addProductToTotalSale(LineItem li) { 
    // Allocate the memory. 
    LineItem[] tempLineItem = new LineItem[1 + lineItem.length]; 
    // Copy the array. 
    if (lineItem.length > 0) { 
    System.arraycopy(lineItem, 0, tempLineItem, 0, lineItem.length); 
    } 
    // add the new item to the new slot. 
    tempLineItem[lineItem.length] = li; 
    // update the internal array reference. 
    lineItem = tempLineItem; 
} 

接下來,你應該保護你的構造從null;

public Receipt(LineItem[] lineItem) { 
    // Try and protect from bad calls, removes need to check for nulls in 
    // add (addProductToTotalSale) routine. 
    if (lineItem != null) { 
    this.lineItem = lineItem; 
    } 
} 

因爲你提供一個默認的0大小的數組你的代碼似乎是安全的,繼續包含默認構造函數。但是,你可能會考慮讓你的Receipt類不可變。

+0

感謝您的幫助! – JStudz