字符串的 ArrayList 和另一个 arraylist

ArrayList of string and another 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".

我唯一不能做的是最后一行,显示每个客户购买的产品,我正在考虑制作一个 class,其中包含一个带有客户名称的字符串和一个数组列表预订 class,然后为每个客户提供一个 class 的数组列表,但我不确定如何制作它,可能有更好的方法, 谢谢

编辑:我有这个 classes: class本书 class 技术书籍扩展书籍 class 冒险书扩展书本

class 客户端 class 常规扩展客户端 class 经常扩展客户端 class 高级扩展客户

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

在每个 class 中覆盖 toString 很重要。然后,当您打印 class 的实例时,将打印字符串而不是一些奇怪的值。

此方法还允许您一次添加所有图书或单独添加图书。

此 class 尚未经过测试,但应该可以让您了解如何进行。

class Client {
  String name;
  private List<Book> clientBooks = new ArrayList<>();
  public Client(String name) {
     this.name = name;
  }

  public addBooks(List<Book> books) {
      clientBooks.addAll(books);
  }

  public void addBook(Book bk) {
      clientBooks.add(bk);
  }

  public String toString() {
     StringBuilder sb = new StringBuilder(name).append(":");
     // add either the entire toString for book here or just selected
     // items like the title.  It's up to you.
     for (Book b :clientBooks) {
        sb.append(b.toString()).append(", ");
     }
     
     return sb.toString().substring(0,sb.length()-2);
  }
}

您可以将图书列表添加到您的客户 class 以跟踪销售情况:

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