类型库(类)未定义方法

时间:2014-03-07 22:56:26

标签: java

如何使用Loanable方法将setDueOn(calendar.getTime)和setLoanedTo(name)设为函数?

“您的图书馆借阅方法应该使用传入的Loanable项目来设置到期日和贷款到。您想要在Loanable实例中调用那些set方法。”

我收到了错误 方法setDueOn未定义类型库 方法setLoanedTo未定义类型库 对不起所有代码。

package src.edu.htc.java1.library;

import java.util.ArrayList;
import java.util.Calendar;


public class Library {

    /* The collection of Media owned by the library */
    private ArrayList<Media> collection = new ArrayList<Media>();
    public void addToCollection(Media item) {
    collection.add(item);
    }

    public void loan(Loanable item,String name) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, item.getDaysToLoan()); // Now this is asking the helper how many days to loan for
        setDueOn(calendar.getTime()); 
        setLoanedTo(name);
        return(setdueOn(Loanable));     

    }

}

1

package src.edu.htc.java1.library;
import java.util.ArrayList;

/**
 * 
 */


/**
 * This class is used to test our library and media classes.
 *
 */
public class MediaTester {

    /**
     * This is the main test method
     * @param args - values passed in by the JVM when running
     * the application
     */
    public static void main(String[] args) {



        Book myBook = new Book();
        myBook.setLibraryId(123456L);
        myBook.setLocation("Eden Prairie");
        myBook.setTitle("My Book Title");
        ArrayList<String> authorList = new ArrayList<String>();
        authorList.add("Joe Author");
        authorList.add("Jane Author");
        myBook.setAuthors(authorList);
        myBook.setCopyright("1984");
        myBook.setFormat("paperback");
        myBook.setNumberOfPages(385);
        myBook.setPublishers("Some Publisher");


        System.out.println(myBook);

        Movies myMovie = new Movies();
        myMovie.setTitle("Fargo");
        myMovie.setReleaseDate(2123);
        myMovie.setDirector("Matt Johnson");
        myMovie.setActors("Matt");
        myMovie.setMPAA_rating("R");
        ArrayList<String> actors = new ArrayList<String>();
        actors.add("Tom Hanks");


        System.out.println(myMovie);


        Games myGames = new Games();
        myGames.setTitle("Starcraft");
        myGames.setConsoleType("wii");
        myGames.setEsbnRatings("E");
        myGames.setReleaseDate("2012");
        myGames.setPublishers("Blizzard");

        System.out.println(myGames);


        Newspaper myPaper = new Newspaper();
         myPaper.setLibraryId(11122233L);
         myPaper.setLocation("St. Paul");
         myPaper.setTitle("Pioneer Press");



    ArrayList<Media> myMediaList = new ArrayList<Media>();
    myMediaList.add(myBook);
    myMediaList.add(myMovie);
    myMediaList.add(myGames);
    myMediaList.add(myPaper);
    Library myLibrary = new Library();

    for (Media item : myMediaList) {
    myLibrary.addToCollection(item);
    if (item instanceof Loanable) {
        myLibrary.loan((Loanable) item, "Mary");
        System.out.println(String.format("Item loaned out: %s",item));
        } else {
        System.out.println(
        String.format("Sorry you can't loan out this item: %s", item));
        }
        }

    }
    }

2

package src.edu.htc.java1.library;


import java.text.SimpleDateFormat;

    import java.util.Date;

/**
 * 
 */

/**
 * This class represents an item in our library's collection.
 *
 */
public abstract class Media {

    public static final int STANDARD_LOAN_DAYS = 14;  

    private long libraryId;
    private String title;
    private String location;
    private String loanedTo;
    private Date dueOn;


    @Override
       public String toString() {

           StringBuilder builder = new StringBuilder();

           builder.append("{");

           builder.append(this.getClass().getSimpleName());

           builder.append(": ");

           builder.append("[id=").append(libraryId);

           builder.append("], [title=").append(title);

           builder.append("], [loanedTo=").append(loanedTo);




           if (getDueOn() != null) {

            builder.append("], [dueOn=");

               SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

               builder.append(formatter.format(getDueOn()));

           }

           builder.append("]");

           builder.append(toStringHelper());

           builder.append("}");

           return builder.toString();

       }

    public long getLibraryId() {
        return libraryId;
    }
    public void setLibraryId(long libraryId) {
        this.libraryId = libraryId;
    }
    protected String toStringHelper() {
        return " ";
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public String getLoanedTo() {
        return loanedTo;
    }
    public Date getDueOn() {
        return dueOn;
    } 
    public void setDueOn(Date dueOn) {
        this.dueOn = dueOn;
    }
    public void setLoanedTo(String loanedTo) {
        this.loanedTo = loanedTo;
    }



}

3

1 个答案:

答案 0 :(得分:0)

如果继承类没有,那么您的Media类可能需要像注释中指出的那样实现Loanable接口,然后是这个方法:

public void loan(Loanable item,String name) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, item.getDaysToLoan()); // Now this is asking the helper how many days to loan for
    setDueOn(calendar.getTime()); 
    setLoanedTo(name);
    return(setdueOn(Loanable));     
}

有一些需要修复的错误:

  1. 如果声明方法void,则意味着它不能(也不应该)返回任何内容,因此不要在其中使用任何return语句。
  2. 需要在特定对象上调用setDueOnsetLoanedTo方法,在本例中为item。目前,您正试图通过Library类本地名称调用方法,但这些名称不存在。
  3. 如果您查看方法setDueOn(Date dueOn),您会注意到它需要一个日期,而不是类或接口名称......
  4. 更正后,方法可能如下所示:

    public void loan(Loanable item,String name) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, item.getDaysToLoan());
        item.setDueOn(calendar.getTime()); 
        item.setLoanedTo(name);
    }
    

    可能还有其他问题,但代码实际上有点太长了,无法浏览...