Bookオブジェクトを追加/削除できるShoppingCartアプリがあります。Bookにはisbn属性があります。誰かが同じ本の複製をカートに追加したかどうかを確認する必要があります。
Book b1 = new Book("isbn222");
Book b2 = new Book("isbn222");
Book b3 = new Book("isbn333");
Book b4 = new Book("isbn444");
Book b5 = new Book("isbn444");
Book b6 = new Book("isbn444");
Book b7 = new Book("isbn555");
//add these to cart
この場合、isbn222で2つのコピーを複製し、isbn444で3つのコピーを追加するという警告をユーザーに生成したいと思います。CartValidatorを以下のように作成することを考えましたが、以下のロジックを実装できませんでした。Javaでサブリストを作成するにはどうすればよいですか。これに関するどんな助けも大いに感謝します。
ありがとう
マーク。
public class CartValidator {
public static String validate(ShoppingCart<Book> cart) {
StringBuffer warning = new StringBuffer("duplicates");
List<Book> items = cart.getItems();
/*
* take first item from list, temp= items.get(0)
* check against all the rest for duplicates and build warning compare with item1,item2..
* take second item temp= items.get(1)
* check against all the rest for duplicates and build warning compare with item2,item3..
*/
return warning.toString();
}