如何创建set枚举方法(java)

时间:2015-12-02 18:09:08

标签: java object enums

我是面向对象编程的新手,我正在尝试创建一个库系统模拟。我正在尝试创建set方法:

  1. 将图书馆书籍的状态设置为REFERENCE_ONLY(枚举)
  2. 将库书的状态设置为AVAILABLE_FOR_LENDING(枚举)
  3. 一个布尔值,决定该书是否为ON_LOAN(枚举)
  4. 我的代码:

    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;
        }
    }
    

1 个答案:

答案 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;
    }
}