创建Book类,下面是方法的作用。我目前仍然坚持如何添加有关通过名称和电子邮件删除作者的方法。我无法上传UML图片,因为我没有足够的重复点。
我的代码:
class Book {
private String name;
private double price;
// private Author[] authors = new Author[5];
//priavte authors =new ArrayList<Author>();
private Map authors = new HashMap<String, Author>();
// private ArrayList<Author> authors = new ArrayList<Author>();
private int qtyInStock = 0;
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public Book(String name, double price, int qtyInStock) {
this.name = name;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getName() {
return this.name;
}
public double getPrice() {
return this.price;
}
public Collection<Author> getAuthors() {
return authors.values();
}
public void setPrice(double price) {
this.price = price;
}
public int getQtyInStock() {
return this.qtyInStock;
}
public void setQtyInStock(int qtyInStock) {
this.qtyInStock = qtyInStock;
}
public void printAuthors() {
authors.values().forEach(System.out::println);
}
public void addAuthor(Author author)
{
authors.put(author.getName(name), author);
}
public void removeAuthorByName(String name) {
authors.remove(authors.get(name));
}
public void removeAuthorByEmail(String email){
authors.remove(authors.get(email));
}
public void removeAuthor(String author){
authors.remove(authors.get(author));
}
public String toString() {
return "'" + name +"' by " + authors + " authors";
}
}
测试用例
Author a = new Author("Adam", "adam@gmail.com", 'm');
Author b = new Author("Ben", "ben@gmail.com", 'm');
Author c = new Author("Calvin", "calvin@gmail.com", 'm');
Author d = new Author("Danielle", "Danielle@gmail.com", 'f');
Book book1 = new Book("The House", 70.00, 5);
book1.addAuthor(a);
book1.addAuthor(b);
book1.addAuthor(c);
book1.addAuthor(d);
book1.removeAuthorByName("Ben");
System.out.println(book1);
book1.printAuthors();
输出:
The House by 3 authors
Adam
Calvin
Danielle
在此链接上添加了UML:https://gyazo.com/4afc1bafa03210044fafe06650859cb0
答案 0 :(得分:0)
我建议将arraylist更改为HashMap。
for(i=0; i<5; i++){
if(stkcommon[i]<50){
printf("The Number %d",i+1);
}
}
到
private ArrayList<Author> authors = new ArrayList<Author>();
更改以下方法 -
private Map authors = new HashMap<String, Author>();