ArrayList书类

时间:2017-03-28 03:48:53

标签: java arraylist

创建Book类,下面是方法的作用。我目前仍然坚持如何添加有关通过名称和电子邮件删除作者的方法。我无法上传UML图片,因为我没有足够的重复点。

  • 在构造函数内创建一个新的作者ArrayList实例。
  • 实现方法addAuthor(作者作者)以将给定的Author实例添加到本书。
  • 实现一个公共方法removeAuthorByName(String name),它使用给定的String来搜索作者ArrayList。如果它找到名称与给定String匹配的Author对象,则将对该对象的引用传递给私有方法removeAuthor(作者作者)。
  • 实现使用给定String搜索作者ArrayList的公共方法removeAuthorByEmail(String email)。如果它找到一个带有与给定String匹配的电子邮件的Author对象,则会将对该对象的引用传递给私有方法removeAuthor(作者作者)。
  • 实现私有方法removeAuthor(作者作者),当给定对Author对象的引用时,将从authors ArrayList中删除对该对象的引用。
  • toString()方法应返回"书名由n作者",其中n是作者的数量。
  • printAuthors()方法应打印Arraylist中所有作者的姓名。

我的代码:

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

1 个答案:

答案 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>();