こんにちは、どこが間違っているのか誰か教えてもらえますか?
このクラスの基本的な目的は、この場合は車に関するお気に入りアイテムの配列リストを定義することです。車のオブジェクトには、車の名前と車 1 ~ 5 の評価があります。
文字列が車のオブジェクトの評価と等しいかどうかを確認するにはどうすればよいですか? 文字列または整数を配列リストの車のオブジェクトと比較する部分を台無しにしています。equals() メソッドの何が問題になっていますか? contains() メソッドは同じように機能しますか?
numberOfItemsOfRating メソッドを使用すると、ユーザーは評価を指定できるため、このメソッドは評価のある車を返しません。searchForItems メソッドは、指定された文字列の説明が配列リスト内の車の名前と一致するかどうかをチェックするため、配列リスト内の車を返します。
コンストラクターと変数を使用した 2 つのメソッドの概要を次に示します。
public class FavouriteItems
{
private ArrayList<Item> cars;
/**
* Constructor for objects of class FavouriteItems
*/
public FavouriteItems()
{
cars= new ArrayList<Item>();
}
/**
* Add a new Item to your collection
* @param newItem The Item object to be added to the collection.
*/
public void addToFavourites(Item newItem)
{
cars.add(newItem);
}
/**
* Count the number of Items with a given rating
* @return The number of Items (Item objects)
* whose rating is rating (could be 0).
* If the rating parameter is outside the valid
* range 1..5 then print an error message and return 0.
*/
public int numberOfItemsOfRating(int rating)
{
int counter = 0;
if(rating >= 1 && rating <=5)
{
for ( int i =0; i < cars.size(); i++)
{
int num = rating;
String al = Integer.toString(rating);
if(cars.get(i).equals(al))
{
counter++;
}
}
}
else
{
System.out.println("No cars match your ratings");
counter = 0;
}
return counter;
}
/**
* Find the details of a Item given its description
* @return Item object if its description is in the collection
* or null if there is no item with that description
*/
public Item searchForItem(String description)
{
for(int i=0; i<cars.size(); i++)
{
if(cars.equals(description))
{
return cars.get(i);
}
else
{
return null;
}
}
}
}