2015-12-31 76 views
-2

我在練習Java時遇到了一個問題。通過調用相應的方法創建一個新對象並將其添加到另一個對象中

我有一個類存儲以下信息:

ID(INT),作者和標題

,我有另一個類書架其中存儲的集合使用Vector/ArrayList的書籍並具有以下方法:

addBook :將書籍對象作爲輸入,將對象添加到書架中,方法不返回任何內容。

returnListOfBooks:不帶參數並返回按字母順序按標題排序的所有書籍的Vector/ArrayList。

returnListOfBooksByAuthor:發生在筆者作爲輸入,並通過筆者

,我停留在現在的問題返回書矢量/ ArrayList的是:

主程序,BookShelfApp,執行以下操作:

•創建Book對象的ArrayList。

•創建BookShelf對象。

•每次一本書放在書架:

○程序必須索要書(一)ID的用戶,(B)一書的作者,以及(c)標題這本書。

o創建一個新的Book對象。

o通過調用相應的方法將其添加到BookShelf對象中。

這是我迄今在主要的java上所做的。

public class BookShelfApp { 
public static void main(String[] args) { 
    Scanner sc = new Scanner (System.in); 

    ArrayList <Book> myList = new ArrayList<Book>(); 
    BookShelf myBookshelf = new BookShelf(myList); 


    System.out.println("Enter ID of the book: "); 
    int id = sc.nextInt(); sc.nextLine(); 

    System.out.println("Enter the author of the book: "); 
    String author = sc.nextLine(); 

    System.out.println("Enter title of the book: "); 
    String title = sc.nextLine(); 

    Book myBook = new Book(); //there is an error 

} 

我是新來的java所以我不太擅長它。任何幫助將不勝感激!

回答

0

我沒有看到你的書的對象,但我希望你與這個定義:

  1. 代表作者一組字段,標題等
  2. 一個constructor taking those parameters

例如

public class Book { 
    private String author; 
    public Book(String author) { 
     this.author = author; 
    } 
} 

,所以你可以調用

Book b = new Book("Jeffrey Archer"); 

請注意,如果你只定義的參數的構造函數,你不能然後使用一個默認的構造函數(如你在上面幹什麼)而沒有明確定義它。唯一可用的構造函數是你聲明的構造函數。

(注:我看你有構造採取這些領域,並制定者得我寧願前者優於後者通過提供製定者,你正在做的Book可變,我也不會想到。 。該書的作者來改變。通過提供一個無參數的默認構造方法,你可以創建一個Book沒有一個作者,這似乎有些奇怪太)

0

嘗試創建一個constructor有和沒有這樣的字段:

public class Book { 

    private String author; 
    //constructor without fields 
    public Book() {} 

    //constructor with fields 
    public Book(String author) { 
    this.author=author; 
    } 

    } 
+0

感謝您的回覆。我編輯了我的問題,並添加了迄今爲止創建的類java,這樣我的問題(希望)會更容易理解。 – Minchae

0

而不是使用boo的ArrayList KS使用列表,這樣就可以保持書的所有的實例在一個列表,然後創建一個設置表示ID,作者和書名領域的構造方法和新bookobjectlist添加到您的Bookshef:

例如:

List <Book> mylist = new List<Book>(); 

construcor

public class Book{ 
    private int id; 
    private String author; 
    private String title; 


     public Book(int ID ,Strind Author,String Title) 
     { 
      this.id =ID; 
      this.author= Author; 
      this.title =Title; 
      } 

    } 

收集的ID,作者和標題用戶輸入後,您可以使用您的setter方法或構造函數添加BOOH屬性

例如

System.out.println("Enter ID of the book: "); 
    int id = sc.nextInt(); sc.nextLine(); 
    System.out.println("Enter the author of the book: "); 
    String author = sc.nextLine(); 

    System.out.println("Enter title of the book: "); 
    String title = sc.nextLine(); 

Book myBook = new Book(id,author,title) 

那麼這本書添加到您的圖書清單 例如: mylist.add(mybook);

然後,將圖書添加到書架:BookShelf myBookshelf = new BookShelf(myList);

+0

謝謝你的回覆。我已經完成了所有這些工作,但仍然停留在他們要求我創建一個新的Book對象並通過調用相應的方法將其添加到BookShelf對象中的問題。我編輯了我的問題,並添加了我創建的類java,所以我的問題將更容易理解 – Minchae

+0

我只是添加了一些新提示,請看上面的內容 –

0

已更新。好點,Brian Agnew

Book類的最小常規實現看起來像這樣。

package tests; 

public class Book { 
    private final int id; 
    private final String author; 
    private final String title; 

    public Book(int id, String author, String title) { 
     this.id = id; 
     this.author = author; 
     this.title = title; 
    } 

    public int getId() { 
     return id; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public String getTitle() { 
     return title; 
    } 
} 

最小實現書架看起來是這樣的:

package tests; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.stream.Collectors; 

public class BookShelf { 

    private final ArrayList<Book> books = new ArrayList<Book>(); 

    public void addBook(Book book){ 
     books.add(book); 
    } 

    public List<Book> returnListOfBooks(){ 
     return books.stream() 
        .sorted(
          (book1, book2)->{ 
           return book1.getTitle().compareTo(book2.getAuthor()); 
          }) 
        .collect(Collectors.toList()); 
    } 


    public List<Book> returnListOfBooksByAuthor(String author){ 
     return books.stream() 
        .filter(book->{ 
           return author.equals(book.getAuthor()); 
          }) 
        .collect(Collectors.toList()); 
    } 
} 

回報有利用Java的8流和lambda表達式,你肯定可以使用你想要的任何其他排序/過濾方法。

現在BookShelfApp類:

package tests; 

import java.util.Scanner; 

public class BookShelfAll { 

    public static void main(String[] args) { 
     Scanner sc = new Scanner (System.in); 

     // I've removed the parameter here, as we do not need to know internal implementation of BookShelf 
     // from BookShelfAll 
     BookShelf myBookshelf = new BookShelf(); 


     System.out.println("Enter ID of the book: "); 
     int id = sc.nextInt(); sc.nextLine(); 

     System.out.println("Enter the author of the book: "); 
     String author = sc.nextLine(); 

     System.out.println("Enter title of the book: "); 
     String title = sc.nextLine(); 

     Book myBook = new Book(id, author, title); 

     //Now adding book to the bookshelf 
     myBookshelf.addBook(myBook); 

     //Now let's output all books in the bookshelf 
     for(Book book: myBookshelf.returnListOfBooks()){ 
      System.out.println(book.getId() + " " + book.getTitle() + " by " + book.getAuthor()); 
     } 
    } 

} 

編輯: 使用排序類別:

Collections.sort(books, (book1, book2)->{ 
     return book1.getTitle().compareTo(book2.getAuthor()); 
    }); 
    return books; 

注意,這將改變並返回書架的內部列表,這並不總是好主意。您可能會更好地複製列表並將其排序/返回。

+1

Book的最小實現非常龐大,有兩個構造函數,並提供setter –

+0

我的書中沒有構造函數;)setter非常重要,所以我決定包含它們。 – Oleksiy

+0

感謝您的回覆。然而,我忘記提及我正在處理的問題的一部分:這就是Book類應該包含構造函數,toString,get和set方法。如果Book類中有構造函數,有沒有辦法做到這一點? – Minchae

相關問題