我必须为addToCart方法创建代码以将项目添加到itemList。我已经看过这里的其他ShoppingCart问题,但没有看到会增加项目数量并检查项目名称以查看其是否在数组中的问题。 问题是
1)如果itemList中已经存在带有参数传递名称的项目,则通过增加传入的数量来更新该项目的数量。
2)如果itemList中不存在任何项目名称,并且没有空间容纳新项目,请创建一个新的项目对象,并以参数形式传递给定的名称,数量和价格,将numItems更新为1。
3)两种情况均应返回true。
4)如果具有给定名称的项目不在数组中,并且数组没有容量,则该方法应返回false ...
5)完成付款方法,以打印出itemList中每个项目的名称,总价和数量
我做了一个getItemIndex方法,并且不确定是否需要在此方法中使用它。当我查看其他addtoCart方法时,我认为这是不同的。
我尝试了for循环,但是它没有返回我想要的东西,因此我创建了新对象,但是我无法使对象保持包含在对象数组中。
Items类
public class Items{
private String name;
private double price;
private int quantity;
public Items (String n, double p, int q){
name = n;
price = p;
quantity = q;
}
public double getPrice(){
return price;
}
public String getName(){
return name;
}
public int getQuantity(){
return quantity;
}
public void addQuantity(int amt){
int newQuantity = amt + quantity;
quantity = newQuantity;
}
public String toString(){
return "item name: " + name + ", item quantity: " + quantity +
", total price: " + (price * quantity);
}
}
购物车类
public class ShoppingCart{
//TODO: declare a cart of Items
private Items[] itemList;
//TODO: declare the number of distinct items in the cart
private int numItems = 0;
private static final int INITIAL_CAP = 5; // the initial size of the cart
private static final int GROW_BY=3;
// ---------------------------------------------------------
// Creates an empty shopping cart with a capacity for 5 items.
// ---------------------------------------------------------
public ShoppingCart(){
itemList = new Items[INITIAL_CAP];
numItems = 0;
}
public double getTotalPrice(){
double totalPrice = 0;
numItems = 0;
for(int i = 0; i<itemList.length; i++){
if(itemList[i]!= null){
totalPrice = totalPrice + (itemList[i].getQuantity()*itemList[i].getPrice());
numItems++;
}
}
return totalPrice;
}
private int getItemIndex(String nameOfItem){
for(int i = 0; i < itemList.length; i++){
if(itemList[i].getName().equals(nameOfItem))
return i;
}
return -1;
}
public boolean addToCart(String name, double price, int quantity){
Items n = new Items (name, price, quantity);
if(numItems == INITIAL_CAP)
return false;
itemList[numItems]=n;
if(itemList[numItems].getName().equals(name)){
quantity = quantity + 1;
return true;
}
else if (!itemList[numItems].getName().equals(name)){
numItems = numItems + 1;
return true;
}
return false;
}
public String display(){
for(int i = 0; i<numItems; i++){
System.out.println(itemList[i].toString());
}
return toString();
}
}
结果应将项目放入数组中并保留在其中,因此当我在main方法中调用它时,它将保留在数组中,并且在我调用它时应像这样打印
牛奶$ 3.00 1 $ 3.00
鸡蛋$ 3.00 1 $ 3.00
等... 这将通过我的模拟方法打印出来,但是我无法弄清楚如何让物品留在购物车中。
答案 0 :(得分:0)
这里有几个问题。首先,您要设置itemList[numItems]
,然后再检查项目列表中是否存在传递给addToCart
的项目名称。其次,您无需检查整个列表,只需检查索引为numItems
的元素,在这种情况下,由于您在检查名称是否为itemList[numItems]
到新项时将其设置为true已经存在。为了解决这个问题,我建议运行一个for循环,从i = 0到numItems,并检查是否存在新项的名称。确认它不存在后,添加新项并递增numItems
。但是,如果该物料已经存在,则更新数量并返回。最后,在遍历整个列表以查看项目是否已存在之后,应检查项目列表是否已满。这样做的原因是因为即使列表已满,也需要更新现有项目数量。因此,在尝试添加新项目之前,您应该检查列表中是否有足够的空间。
public boolean addToCart(String name, double price, int quantity){
Items n = new Items (name, price, quantity);
for(int i = 0; i < numItems; i++){
if(itemList[i].getName().equals(name)) {
itemList[i].quantity += quantity;
return true;
}
}
if(numItems == INITIAL_CAP)
return false;
itemList[numItems] = n;
numItems += 1;
return true;
}