购物车中允许重复商品,但需要警告消息

发布于 2024-11-09 09:50:52 字数 1138 浏览 2 评论 0 原文

我有一个 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 的副本。 我想创建一个 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();

}

I have a ShoppingCart app that can add/remove Book objects
A book has an isbn attribute.
I need to check if someone has added a duplicate copy of same book to the cart
ie,

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

In this case I want to generate a warning to the user that duplicates 2 copies with isbn222, three copies with isbn444 are added.
I thought of creating a CartValidator as below,However,I couldn't implement the logic given below..How do you create sub lists in java?
Any help regarding this much appreciated.

thanks

mark.

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();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-11-16 09:50:52

我建议将 Map 与 > 一起使用。其中字符串键为图书 ISBN,列表是带有 ISBN 的图书。

I would suggest to have Map with <String, List<Book>>. where String key the Book ISBN and the list is the books with the ISBN.

烙印 2024-11-16 09:50:52

如果您不希望同一 ISBN 出现多个警告,您可以 a) 对输入进行排序,然后检查它(简单循环),或者 b) 检查输出以确保它不包含给定 ISBN 的警告。

从性能的角度来看,排序应该更好(O(n log n + n) vs. O(n^2)),并且您也不需要额外的内存来存储哈希表或其他东西。

Well if you don't want several warnings for the same ISBN you either a) sort the input and then check it (simple loop) or b) check your output to make sure it doesn't already contains a warning of the given ISBN.

From a performance point of view the sorting should be better (O(n log n + n) vs. O(n^2)) and you also don't need extra memory for a hashtable or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文