2015-12-02 75 views
-3

我很新來面向對象編程,並且正在嘗試創建一個庫系統模擬。我試圖建立一套方法,其中:如何創建一個枚舉方法(java)

  1. 設置庫書的地位REFERENCE_ONLY(ENUM)
  2. 將圖書館的書作爲AVAILABLE_FOR_LENDING(ENUM)
  3. 一個布爾值,它決定的狀態如果這本書是ON_LOAN(ENUM)

我的代碼:

public class LibrarySimulation { 

    public static void main(String[] args) { 
    } 

    public static String getBookAuthor(){ 
     return null; 
    } 
} 

class LibraryBook { 
    public enum status {REFERENCE_ONLY, ON_LOAN, AVAILABLE_FOR_LENDING}; 
    String bookAuthor; 
    String bookTitle; 
    int bookPages; 
    String classification; 
    int timesBorrowed; 
    int reservations; 
    static int totalOnLoan; 

    /** 
    * Constructor with arguments for a LibraryBook’s author(s), 
    * title and number of pages 
    * @param bookAuthor the names of the author(s) of this 
    * LibraryBook 
    * @param bookTitle the title of this LibraryBook 
    * @param bookPages the number of pages of this 
    * LibraryBook 
    */ 
    public LibraryBook(String bookAuthor,String bookTitle, int bookPages){ 
     bookAuthor = null; 
     bookTitle = null; 
     bookPages = 0; 
    } 

    /** 
    * A method to reset the Library classification of this 
    * LibraryBook 
    * @param bookClass the proposed new classification 
    * @return true, if the proposed new 
    * classification has at 
    * least 3 characters to which 
    * the Library classification is 
    * reset. 
    * false, otherwise. 
    */ 
    public boolean setClassification(String bookClass){ 
     if(bookClass.length() >= 3){ 
      return false; 
     } 
     else{ 
      return true; 
     } 
    } 

    public String setAsReferenceOnly(){ 
     LibraryBook book = new LibraryBook(status.REFERENCE_ONLY); 
    } 

    //method for getting bookAuthor 
    public String getBookAuthor(){ 
     return bookAuthor; 
    } 

    //method for getting bookTitle 
    public String getBookTitle(){ 
     return bookTitle; 
    } 

    //method for getting bookPages 
    public int getBookPages(){ 
     return bookPages; 
    } 

    //method for getting classification 
    public String getClassification(){ 
     return classification; 
    } 

    //method for getting TimesBorrowed 
    public int getTimesBorrowed(){ 
     return timesBorrowed; 
    } 
} 
+0

我只想知道如何設置一個枚舉。如果你能教育我,將不勝感激。 –

+0

好的,我會試試。刪除評論。 (對不起) –

回答

0

在這裏,我不得不重新格式化它爲了與它一起工作,請隨時重新格式化爲您自己的風格。

public class LibrarySimulation 
{ 
    public static void main(String[] args) 
    { 
    } 

    public static String getBookAuthor() //XXX WTF is this? 
    { 
     return null; 
    } 
} 

class LibraryBook 
{ 
    public enum Status 
    { 
     REFERENCE_ONLY, 
     ON_LOAN, 
     AVAILABLE_FOR_LENDING 
    } 

    private final String author; //XXX replace with List<Author>? 
    private final String title; 
    private final int pageCount; 
    private String classification; //TODO: replace with "Classification" enum or object 
    private int borrowedCount; 
    private int reservationCount; 
    private static int totalOnLoan; //XXX why static? 
    private Status status; 

    /** 
    * Constructor. 
    * 
    * @param author the name(s) of the author(s) of this LibraryBook 
    * @param title the title of this LibraryBook 
    * @param pageCount the number of pages of this LibraryBook 
    */ 
    LibraryBook(String author, String title, int pageCount) 
    { 
     this.author = author; 
     this.title = title; 
     this.pageCount = pageCount; 
    } 

    //Gets the author 
    public String getAuthor() 
    { 
     return author; 
    } 

    //Gets the title 
    public String getTitle() 
    { 
     return title; 
    } 

    //Gets the page count 
    public int getPageCount() 
    { 
     return pageCount; 
    } 

    //Gets the borrowed count 
    public int getBorrowedCount() 
    { 
     return borrowedCount; 
    } 

    //Gets the classification 
    public String getClassification() 
    { 
     return classification; 
    } 

    /** 
    * Sets the classification of this {@link LibraryBook}, if the proposed new classification is at least 3 characters long. 
    * 
    * @param classification the proposed new {@link Classification} 
    * 
    * @return {@code true} if successful; {@code false} otherwise. 
    */ 
    public boolean setClassification(String classification) //XXX throw exception instead of returning result? 
    { 
     if(classification.length() >= 3) 
     { 
      return false; 
     } 
     else 
     { 
      return true; 
     } 
    } 


    /** 
    * Gets the {@link Status} of this {@link LibraryBook}. 
    * 
    * @return the status. 
    */ 
    public Status getStatus() 
    { 
     return status; 
    } 

    /** 
    * Sets the {@link Status} of this {@link LibraryBook}. 
    * 
    * @param status the new status. 
    */ 
    public void setStatus(Status status) 
    { 
     this.status = status; 
    } 
} 
+1

太好了。我想我明白如何設置枚舉背後的基本前提。我即將完成模擬。感謝您的幫助。 –