java - 字符串的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".
我唯一不能做的部分是最后一行,显示每个客户购买的产品,我正在考虑创建一个包含客户名称字符串和书籍类的数组列表的类,然后有一个数组列表为每个客户提供该课程,但我不确定如何制作,可能有更好的方法,谢谢
编辑:我有这门课:课本课技术书扩展书课冒险书扩展书
客户类 普通类 扩展客户 类 频繁扩展 客户 高级 扩展客户
我有书店类,其中有书籍和客户对象的数组列表。
解决方案
您可以将书籍列表添加到您的客户类以跟踪销售:
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);
}
}
推荐阅读
- database - ASP.NET 核心 + Heroku。数据库迁移
- c# - 检查字符串是否包含 C#r 中特定顺序的字符
- google-sheets - 我可以从 google sheet api 读取一次,但随后不会更新
- python - 用于检查特定标签是否不存在的 Lambda 函数-python
- c# - 更改使用泛型的抽象类的实例
- python - Python C 扩展引发分段错误
- javascript - 从 JSP 中的 URL 动态获取参数
- dax - DAX - YTD,列上有年份
- ios - 如何使用 URL 参数和 UITableview 执行干净的 JSON 解码
- java - 如何在 Java RS json 响应中为简单列表类型设置客户名称