0

私には2つのクラスがあります。1つ目は完了し、Itemオブジェクトを作成しています。

public class Item {

private String name; // The Name of the item.
private int unitPrice; // The price of each unit of this item, in cents.
private int quantity; // The number of units of this item.

// CONSTRUCTOR

/*
 * Constructs an item with specified name, price, and quantity.
 */
public Item(String name, int unitPrice, int quantity) {
    this.name = name;
    this.unitPrice = unitPrice;
    this.quantity = quantity;
}

// METHODS

/*
 * Gets the name for the item. Returns the name for this item.
 */
public String getName() {
    return name;
}

/*
 * Gets the price of each unit of a given item, in cents. Return the price.
 */
public int getUnitPrice() {
    return unitPrice;
}

/*
 * Gets and returns the number of units of this item.
 */
public int getQuantity() {
    return quantity;
}

/*
 * Increases or decreases the quantity of this item. If value is positive,
 * quantity increases. If negative, quantity decreases.
 */
public void changeQuantity(int value) {
    quantity = quantity + value;
}

/*
 * Gets the total price, in cents. Returns the productof quantity and
 * unitPrice as the total price of this item.
 */
public int getTotalPrice() {
    return quantity * unitPrice;
}

/*
 * Returns a string of this item, including name, price($)
 */
public String toString() {
    return name + ": " + quantity + " ($" + unitPrice / 100 + "."
            + unitPrice % 100 + " per unit)";
}

}

今、私はfindItemメソッドを実行しようとしています。ここで検索します。それを行う方法が正確にはわかりません。すべての在庫を調べて、名前を一致させたいと思います。getName()を使用する必要があると思いますが、各名前の確認方法がわかりません。

public class Store {
private ArrayList<Item> inventory;

//MOTHODS

/*
 * Finds an item by its name if it is part of the store's inventory
 * Name is case-insensitive
 * Returns the Item object corresponding to the given name if the item was found. If an item with the given name was not found, then this method returns null.
 */
public Item findItem(String name){
    for(int i=0; i<inventory.size();i++){
        if(name.toUpperCase().equals(?????))
    }
}

}

助けてくれてありがとう。

4

4 に答える 4

1

for-eachループを使用しないのはなぜですか?

public Item findItem(String name)
{
    for(Item item : inventory)
    {
        if(item.getName().equalsIgnoreCase(name))
        {
            return item;
        }
    }

    return null;
}
于 2013-02-05T02:59:32.447 に答える
0

nameオブジェクトにはすでに getter があるItemので、残りは...

 if(name.equalsIgnoreCase(use-your-getter-here()))
于 2013-02-05T02:59:12.427 に答える
0
public Item findItem(String name){
  for(int i=0; i<inventory.size();i++){
    if(name.toUpperCase().equals(inventory.get(i)))
  }
于 2013-02-05T02:59:31.633 に答える
0

必要なコードは次のとおりです。

import java.util.ArrayList;

public class Store {
    private ArrayList<Item> inventory;

    // MOTHODS

    /*
     * Finds an item by its name if it is part of the store's inventory Name is
     * case-insensitive Returns the Item object corresponding to the given name
     * if the item was found. If an item with the given name was not found, then
     * this method returns null.
     */
    public Item findItem(String name) {
        for (int i = 0; i < inventory.size(); i++) {
            if (name.toUpperCase().equals(inventory.get(i).getName().toUpperCase())) {
                return inventory.get(i);
            }
        }
        return null;
    }
}
于 2013-02-05T03:01:24.410 に答える