字符串的arraylist和另一个arraylist

5cg8jx4n  于 2021-07-11  发布在  Java
关注(0)|答案(3)|浏览(365)

所以我在做一个程序来管理一个书店,下面是一个关于如何处理以下主要问题的例子:

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

我唯一做不到的部分是最后一行,显示每个客户购买的产品,我想制作一个包含客户名称字符串和book类arraylist的类,然后为每个客户创建一个该类的数组列表,但我不确定如何制作,可能还有更好的方法,谢谢
编辑:我有这个类:类图书类tecnicalbook扩展图书类adventurebook扩展图书
类客户端类常规扩展客户端类频繁扩展客户端类高级扩展客户端
我有一个bookstore类,其中有一个book和client对象的数组列表。

bnlyeluc

bnlyeluc1#

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

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

k7fdbhmy2#

重要的是要覆盖 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);
  }
}
wgx48brx

wgx48brx3#

您可以按以下步骤进行:

class BookStore {
    private List<Client> clientList = new ArrayList<>();
    //...
    public void getClientList() {
        return clientList;
    }
    public void addClient(Client c) {
        clientList.add(c);
    }
    public void sale(Book b, Client c) {
        //...
        c.getBookList().add(b);
    }
    public void showClientBooks(Client c) {
        System.out.print(c.getName() + "bought:");
        for (Book b : c.getBookList()) {
            System.out.print(" \"" + b.getName() + "\".");
        }
        System.out.println();
    }

    public void showAllClientsBooks() {
        for(Client c: clientList) {
            showClientBooks(c);
        }
    }
}

class Client {
    //..
    private List<Book> bookList = new ArrayList<>();
    public void getBookList() {
        return bookList;
    }
}

注意:始终遵循java命名约定,例如,根据约定,类应命名为 Book , BookStore 等等,而不是 book , bookStore .

相关问题