首页 > 解决方案 > 字符串的ArrayList和另一个arraylist

问题描述

所以我正在制作一个管理书店的程序,下面是一个应该如何处理以下主要内容的示例:

        bookStore bookStore = new bookStore("Lelo");
        book l1 = new TecnicalBook("H. Schildt", "\"Java: The Complete Reference\"", 80, "Computer Science- Java");
        bookStore.addBook(l1);
        book l2= new AdventureBook("A. Christie", "\"The Man in the Brown Suit\"", 75, 14);
        bookStore.addBook(l2);
                
        
        Client c1 = new Premium("B. Antunes", "antunes@books.com");
        bookStore.addClient(c1);
        Client c2 = new Frequent("A. Oliveira", "oliveira@books.com");
        bookStore.addClient(c2);
        System.out.println("Initial stock:");
        bookStore.showBooks();
        bookStore.sale(l1, c1);
        System.out.println("Stock after selling:");
        bookStore.showBooks();
        System.out.println("Sales Volume:"+bookStore.SalesVolume);        
        bookStore.showClientsBooks(c1);

应该显示这个结果:

Initial stock:
Author:H. Schildt   Title:"Java: The Complete Reference"   Base Price:80.0
Author:A. Christie   Title:"The Man in the Brown Suit"   Base Price:75.0
B. Antunes bought "Java: The Complete Reference", Tecnical Book of Computer Science- Java, for 72.0
Stock after selling:
Author:A. Christie   Title:"The Man in the Brown Suit"   Base Price:75.0
Sales Volume:72.0
B. Antunes bought: "UML Distilled". "The Man in the Brown Suit".

我唯一不能做的部分是最后一行,显示每个客户购买的产品,我正在考虑创建一个包含客户名称字符串和书籍类的数组列表的类,然后有一个数组列表为每个客户提供该课程,但我不确定如何制作,可能有更好的方法,谢谢

编辑:我有这门课:课本课技术书扩展书课冒险书扩展书

客户类 普通类 扩展客户 类 频繁扩展 客户 高级 扩展客户

我有书店类,其中有书籍和客户对象的数组列表。

标签: javaooparraylistpolymorphismhierarchy

解决方案


您可以将书籍列表添加到您的客户类以跟踪销售:

class Client {

    ...

    private List<Book> books;

    ClientBooks(Client client) {
        ...

        this.books = new ArrayList<>();
    }

    public void addBook(Book book) {
        this.books.add(book);
    }

    public void printBooks() {
        for (Book book : this.books) {
            System.out.println(book);
        }
    }
}

卖书可以这样实现:

List<Client> clients = ...

String buyer = ...
Book book = ...

for(Client client : clients) {
    if (client.getName().equals(buyer)) {
        client.addBook(book);
    }
}

推荐阅读