代码不执行用户输入并从我的库类调用函数

时间:2015-12-02 02:34:42

标签: java java.util.scanner

我希望设置一个Library类,它将设置Book对象的数组或数组列表,并执行适当的功能,例如添加书籍,编辑书籍的详细信息,删除书籍,借书和返回书。此外,我希望实现一个测试人员类,它将完全测试解决方案。我希望将测试菜单的每个选项编程为包含验证(如果适用),并调用库类中创建的函数。到目前为止,我有以下几个类:

图书课程:

public class Book {

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

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

    public Book(int iD, String title2) {
        // TODO Auto-generated constructor stub
    }

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

图书馆班:

import java.util.ArrayList;

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 (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

    }
}

图书馆测试员班:

import java.util.Scanner;

public class Library_Tester {

    public static void main(String args[]) {

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

        displayMenu(lib, sc);
    }

    static void displayMenu(Library i, Scanner s) {
        System.out.println("--- Library Menu ---");
        System.out.println("--- Display Books ---");
        System.out.println("--- Add Book ---");
        System.out.println("--- Remove Book ---");
        System.out.println("--- Edit Book ---");
        System.out.println("--- EXIT ---");

        int option = s.nextInt();

        switch (option) {
        case 1:
            displayBooks();
            break;

        case 2:
            System.out.println("Enter an ID");
            int id = s.nextInt();
            System.out.println("Enter a title");
            String title = s.nextLine();
            i.addBook(id, title);
            break;

        case 3:
            System.out.println("Enter an ID to remove");
            int idToRemove = s.nextInt();
            i.removeBook(idToRemove);
            break;

        case 4:
            System.out.println("Enter an ID to edit");
            int idToEdit = s.nextInt();
            i.editBook(idToEdit, s);
            break;

        case 5:
            System.out.println("EXITING...");
            System.exit(1);
            break;

        default:
            System.out.println("ERROR: Invalid input");
            break;

        }

        displayMenu(i, s);

    }

    private static void displayBooks() {
        // TODO Auto-generated method stub

    }
}

HelperUtilities类:

import java.util.ArrayList;

public class HelperUtilities {

    private static String[] names = { "Harry Potter and the Philosopher's Stone",
            "Harry Potter and the Chamber of Secrets", "Harry Potter and the Prisoner of Azkaban",
            "Harry Potter and the Goblet of Fire", "Harry Potter and the Order of the Phoenix",
            "Harry Potter and the Half-Blood Prince", "Harry Potter and the Deathly Hallows" };

    private static int[] ids = { 1000, 1001, 1002, 1003, 1004, 1005, 1006 };

    public static ArrayList<Book> generateBooks() {
        ArrayList<Book> books = new ArrayList<Book>();

        for (int i = 0; i < names.length; i++) {
            books.add(new Book(ids[i], names[i]));
        }

        return books;
    }

}

我的问题是,最终,为什么我的代码不执行用户输入并从我的库类调用函数?任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

就像MadProgrammer提到的那样,你需要在s.nextLine()之后添加s.nextInt(),这会给你带来麻烦。

如果您无法决定在哪里插入s.nextLine(),请将您的所有s.nextInt()更改为:

int someVariable = Integer.parseInt(s.nextLine());

以String形式接收输入,然后解析为适当的类型。这样,您的输入始终使用换行符。