【问题标题】:How to create a set enum method (java)如何创建一个集合枚举方法(java)
【发布时间】:2015-12-02 18:09:07
【问题描述】:

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

  1. 将图书馆图书的状态设置为 REFERENCE_ONLY(枚举)
  2. 将图书馆图书的状态设置为 AVAILABLE_FOR_LENDING(枚举)
  3. 一个布尔值,用于确定图书是否为 ON_LOAN(枚举)

我的代码:

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

【问题讨论】:

  • 我只想知道如何设置枚举。如果你能教育我,将不胜感激。
  • 好的,我试试。删除评论。 (对不起。)

标签: java object enums


【解决方案1】:

在这里,我必须重新格式化它才能使用它,请随时将其重新格式化为您自己的风格。

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

【讨论】:

  • 太好了。我想我现在了解如何设置枚举背后的基本前提。我即将完成模拟。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2011-04-09
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多