繁体   English   中英

字符串和另一个数组列表的 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".

我唯一不能做的部分是最后一行,显示每个客户购买的产品,我正在考虑创建一个包含客户名称字符串和书籍类数组列表的类,然后有一个数组列表每个客户的那个班级,但我不知道如何做到这一点,可能有更好的方法,谢谢

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

类客户端类常规扩展客户端类频繁扩展客户端类高级扩展客户端

我有书店类,在那里我有一个书籍和客户端对象的数组列表。

您可以将 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);
        }
    }
}

销售一本书可以这样实现:

List<Client> clients = ...

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

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

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

如果您愿意,此方法还允许您一次添加所有书籍或单个书籍。

这门课没有经过测试,但它应该让你知道如何继续。

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

因此,我正在编写一个管理书店的程序,以下是有关以下主要内容的示例:

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

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

编辑:我有这个课:课本课tecnicalbook扩展书

类客户类常规扩展客户类频繁扩展客户类高级扩展客户

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

暂无
暂无

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

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