简体   繁体   English

字符串和另一个数组列表的 ArrayList

[英]ArrayList of string and another arraylist

so i'm making a program to manage a book store, heres an example of whats supposed to do with the the following main:所以我正在制作一个程序来管理一家书店,这里有一个例子,说明应该如何处理以下主要内容:

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

Should show this result:应该显示这个结果:

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".

The only part that i cant do is the last line, showing the products that each client has bought, i was thinking of making an class that contained a string with the clients name and an arraylist of a book class, and then having an array list of that class for each client, but i'm not sure how to make this and theres probably a better way, Thanks我唯一不能做的部分是最后一行,显示每个客户购买的产品,我正在考虑创建一个包含客户名称字符串和书籍类数组列表的类,然后有一个数组列表每个客户的那个班级,但我不知道如何做到这一点,可能有更好的方法,谢谢

edit: i have this classes: class book class tecnicalbook extends book class adventurebook extends book编辑:我有这门课:课本课技术书扩展书课冒险书扩展书

class client class regular extends client class frequent extends client class premium extends client类客户端类常规扩展客户端类频繁扩展客户端类高级扩展客户端

and i have the bookstore class where i have an array list of book and client objects.我有书店类,在那里我有一个书籍和客户端对象的数组列表。

You can add a list of Books to your Client class to keep track of sales:您可以将 Books 列表添加到您的 Client 类以跟踪销售情况:

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

Selling a book can be implemented like this:销售一本书可以这样实现:

List<Client> clients = ...

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

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

It is important to override toString in each class.在每个类中覆盖toString很重要。 Then when your print the instance of that class, the string will print instead of some strange value.然后当您打印该类的实例时,该字符串将打印而不是一些奇怪的值。

This method also allows you to add all the books at once or individual books if you choose.如果您愿意,此方法还允许您一次添加所有书籍或单个书籍。

This class has not been tested but it should give you an idea of how to procede.这门课没有经过测试,但它应该让你知道如何继续。

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

so i'm making a program to manage a book store, heres an example of whats supposed to do with the the following main:因此,我正在编写一个管理书店的程序,以下是有关以下主要内容的示例:

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

Should show this result:应显示此结果:

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".

The only part that i cant do is the last line, showing the products that each client has bought, i was thinking of making an class that contained a string with the clients name and an arraylist of a book class, and then having an array list of that class for each client, but i'm not sure how to make this and theres probably a better way, Thanks我唯一不能做的部分是最后一行,显示每个客户购买的产品,我正在考虑制作一个类,其中包含一个带有客户名称的字符串和一本书类的数组列表,然后有一个数组列表每个客户的课程,但我不确定如何制作,可能还有更好的方法,谢谢

edit: i have this classes: class book class tecnicalbook extends book class adventurebook extends book编辑:我有这个课:课本课tecnicalbook扩展书

class client class regular extends client class frequent extends client class premium extends client类客户类常规扩展客户类频繁扩展客户类高级扩展客户

and i have the bookstore class where i have an array list of book and client objects.我有书店类,其中有书和客户对象的数组列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM