gpt4 book ai didi

java - 销售点 - 卡在阵列上

转载 作者:行者123 更新时间:2023-12-01 13:20:28 26 4
gpt4 key购买 nike

基本上,我有多个类,并且我正在尝试为客户购买的每个项目获取 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;
}

最佳答案

我会像这样删除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 类不可变。

关于java - 销售点 - 卡在阵列上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com