我如何解决此加法

时间:2019-05-05 21:20:32

标签: java object indexing methods

我需要创建一个方法plus,该方法需要一个项目的名称和价格以及一个索引位置。如果项目数组在索引位置中已经有一个项目对象,则返回false;如果没有,则在索引位置中添加具有名称和价格的新项目,然后返回true。

我尝试对购物车执行类似于addtoCart方法的操作,但是我不了解索引位置部分。

物品类别:

    public class Item{
private double price;
private static String name;
    private int rating;

public Item (String n, double p){
    name= n;
    price = p;
}

public double getPrice(){
    return price;
}
public boolean equals(Item item2)
{
    if (item2.title.equals(Item.title)) {
        return true;
    }
    return false;

商品可用类别

     public class ItemAvailable{
 private Item [] itemList;

 public ItemAvailable(itemList.length){
    itemList = new Item[itemList.length];
 }


   public boolean plus(String name, double price, itemList[])
    Item n = new Item(title, price, bookList[]);
    for (int i = 0; i<itemList.length; i++){
       if(itemList[i].name.equals(name)){

           return true;

        }
       else if (itemList.length == INITIAL_CAP)
           return false;

如果有空间添加新项目,我希望输出为true,如果索引位置中已经有项目对象,则输出为false。

0 个答案:

没有答案