Java编辑书,借阅书和返还书方法

时间:2015-12-09 14:02:43

标签: java methods

我希望实施编辑书,借书和退货功能,并且不知道从哪里开始。

到目前为止,我有4个类,包括BookLibraryLibraryTesterhelperutilities类。

package Assignment;

import java.util.Scanner;

public class LibraryTester {

public static void main(String args[]){

    Library lib = new Library(HelperUtilities.generateBooks());
    Scanner sc = new Scanner(System.in);

    displayMenu(lib, sc);
}

public static void displayMenu(Library i, Scanner s) {
System.out.println("--- Library Menu ---");
System.out.println("---(1) Display Books ---");
System.out.println("---(2) Add Book ---");
System.out.println("---(3) Remove Book ---");
System.out.println("---(4) Edit Book ---");
System.out.println("---(5) Loan Book ---");
System.out.println("---(6) Return Book ---");
System.out.println("---(7) EXIT ---");

到目前为止,1,2和3个函数根据我在Library类中创建的方法工作。我现在只需要实现编辑书,贷款簿和退货书的方法。任何帮助将不胜感激,谢谢。

库类方法:

package Assignment;

import java.util.ArrayList;
import java.util.Scanner;

public class Library {

private ArrayList<Book> books;

public Library (ArrayList<Book> books){
    super();
    this.books = books;
}

public ArrayList<Book> getBooks () {
    return books;
}

public void setBooks(ArrayList<Book> books){
    this.books = books;
}

public void displayBooks()
{
    for (int i = 0; i < books.size(); i++) {
        System.out.println("ID " +books.get(i).getBookID());
        System.out.println("Title "+books.get(i).getTitle());
    }
    System.out.println("Displayed "+books.size()+" Books");
}

public void addBook(int ID, String title)
{
    books.add(new Book(ID,title));

}

public void loaning_A_Book (int bookIndex) {
    Book book = books.get(bookIndex);
    book.setOn_Loan (true);
}

public void removeBook(int id)
{
    boolean successful = false;
    for (int i=0;i<books.size();i++)
    {
        if((int)books.get(i).getBookID()==id)
        {
            books.remove(i);
            System.out.println("Book removal successful");
            successful=true;
        }
    }

    if (!successful)
    {
        System.out.println("Could not remove book id "+id);
        }
    }

public void editBook(int idToEdit, Scanner s) {
    // TODO Auto-generated method stub

}

}

图书课程:

package Assignment;

public class Book {

    //Instance variables
    private int BookID;
    private String Title;
    private String Author;
    private boolean On_Loan;
    private int Number_of_Loans;

    public Book(int BookID, String Title) {
        this.BookID = BookID;
        this.Title = Title;
    }

    //Constructor
    public Book(int BookID, String Title, String Author, boolean On_Loan, int  Number_of_Loans){
        this.BookID = BookID;
        this.Title = Title;
        this.Author = Author;
        this.On_Loan = On_Loan;
        this.Number_of_Loans = Number_of_Loans;
    }

    //Mutator methods
    public void setBookID(int BookID){
        this.BookID = BookID;
    }
    public void setTitle(String Title){
        this.Title = Title;
    }
    public void setAuthor(String Author){
        this.Author = Author;
    }
    public void setOn_Loan(boolean On_Loan){
        this.On_Loan = On_Loan;
    }
    public void setNumber_of_Loans(int Number_of_Loans){
        this.Number_of_Loans = Number_of_Loans;
    }

    //Accessor methods
    public int getBookID(){
        return BookID;
    }
    public String getTitle(){
        return Title;
    }
    public String getAuthor(){
        return Author;
    }
    public boolean getOn_Loan(){
        return On_Loan;
    }
    public int getNumber_of_Loans(){
        return Number_of_Loans;
    }
    private boolean available;

    public void setAvailable(boolean avail){

    available = avail;
    }

    public boolean isAvailable(){

    return available;
    }
}

1 个答案:

答案 0 :(得分:0)

编辑书籍:

Book book = lib.getBook(bookId);
// edit by making all the text lower-case, for instance
book.setText(book.getText().toLowerCase());

借书和退票:

图书类可以有可用的属性

private boolean available;

public void setAvailable(boolean avail){

available = avail;
}

public boolean isAvailable(){

return available;
}

然后贷款:

lib.getBook(bookId).setAvailable(false);

返回:

lib.getBook(bookId).setAvailable(true);

当然,图书馆不应在展示图书部分列出不可用的图书(致电isAvailable()并检查是否属实)。