简体   繁体   中英

incompatible types: missing return value

I'm new to java and I'm currently creating a library program that has 3 classes: TextBook, LibraryCard and Library; I created a method called public LibraryCard issueCard() this is meant to create a new LibraryCard Object while also incrementing the borrowers variable.

Here is the code for The LibraryCard Class:

public class LibraryCard
{

private int booklimit;
private int books_read;
private String CardID;

public LibraryCard(int Limit, String ID)
{
    booklimit=Limit;
    CardID=ID;
}

public void swipe(){
    books_read++;
}

public boolean expired(){
    if(booklimit<=books_read){
        System.out.println("You have reached your book borrowing limit."); 
        return true;
    }
    else{
        return false;
    }

}
public String getCardRef(){
    return CardID;
}

public void describe() {
    System.out.println("Library Card ID:" + CardID + " with " + books_read + " books read!");
}
}

And here's the code for the Library Class:

public class Library
{

int nextBook;
int borrowers = 0;

public void Library(int Max_Books)
{
    TextBook[] bookshelf = new TextBook[5];
    for(int ptr=0;ptr<bookshelf.length;ptr++)
        bookshelf[ptr] = new TextBook("book title goes here", 25);  
}

public LibraryCard issueCard()
{
    borrowers++;
    new LibraryCard(2,"CardID"+ borrowers); 
    return;
}
}

Thank You in advance.

Your issueCard method is not actually returning the LibraryCard that it creates. Replace it with the following and it should work:

public LibraryCard issueCard()
{
    borrowers++;
    return new LibraryCard(2, "CardID" + borrowers); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM