- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须为 addToCart 方法创建代码才能将项目添加到 itemList。我在这里查看了其他 ShoppingCart 问题,但我没有看到一个会增加商品数量并检查商品名称以查看其是否在数组中的问题...问题是
1) 如果 itemList 中已存在具有传入参数的名称的项目,则通过将其增加传入的数量来更新该项目数量。
2) 如果 itemList 中不存在商品名称,并且有空间容纳新商品,则创建一个新的商品对象,并将给定名称、数量和价格作为参数传入,并将 numItems 更新为 1。
3) 两种情况都应返回 true。
4) 如果给定名称的项目不在数组中并且数组没有容量,则该方法应返回 false...
5)完成dispay方法,打印出itemList中每件商品的名称、总价、数量
我创建了一个 getItemIndex 方法,但不确定是否需要在该方法中使用它。当我查看其他 addtoCart 方法时,我认为这个方法是不同的。
我尝试了 for 循环,但它没有返回我想要的内容,因此我创建了新对象,但我无法让对象保留在对象数组中。
元素类别
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();
}
}
结果应该将项目放入数组中并保留在其中,因此当我在主方法中调用它时,它将保留在数组中,并且当我调用它时它应该像这样打印
牛奶 $3.00 1 $3.00
鸡蛋 $3.00 1 $3.00
等等...这将通过我的模拟方法打印出来,但我不知道如何让元素留在购物车中。
最佳答案
这里有几个问题。首先,您需要设置 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;
}
关于java - 如何修复该类的 addToCart 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835377/
我必须为 addToCart 方法创建代码才能将项目添加到 itemList。我在这里查看了其他 ShoppingCart 问题,但我没有看到一个会增加商品数量并检查商品名称以查看其是否在数组中的问题
我有一个表格,其中每行/项目都有一个 addtocart 按钮 $( $table ).delegate(".Addtocart", "click", function() {
我正在为一个类做一个 Java 应用程序,需要帮助解决运行该应用程序后出现的错误。 该应用程序应该允许用户将商品添加到购物车(商品名称、价格、数量)。用户描述的商品会添加到购物车数组中,添加成功后会打
我正在尝试制作一个组件,但这是说我没有定义“addToCart”。我已经在同一组件中定义了它,但是我仍然遇到相同的错误吗? Vue.component("solid-back",{ templ
我是一名优秀的程序员,十分优秀!