方法不会在对象ArrayList中增加一个int变量?

时间:2015-12-11 11:15:42

标签: java arraylist

我在“贷款手册”中遇到过这个问题。方法,它的功能是增加“数字”和“数字”。存储在对象ArrayList中的变量为1,但是当我运行该方法时,它似乎不会更改该值。

//Allows loaning of books
public static void Loanbook(Scanner sc, ArrayList<Book> books){
System.out.println("Please enter a book title");
    if(sc.hasNext()){
        String criteria = sc.nextLine();
        for (int i = 0; i < books.size(); i++){
            if(criteria.equals(books.get(i).getBookTitle())){
            System.out.println("The book " + books.get(i).getBookTitle() + " there are " + books.get(i).getNumInStock() + " in stock");
                    books.get(i).setNumOnLoan(books.get(i).GetNumOnLoan()+1);
                    System.out.println("number on loan: " + books.get(i).GetNumOnLoan()); break;    
                        }
                    } System.out.println("Book Loaned");
                        LibraryTester.MenuReturn(sc, books);

            }   
                LibraryTester.MenuReturn(sc, books);
        }

我怀疑这是代码逻辑的问题但是我编辑它,代码似乎没有做我想要的。

编辑: book.java

public class Book {
private int id;
private String bookTitle;
private String authorName;
private int bookReleaseYear;
private int numOnLoan;
private int numInStock;

//constructor
public Book(int id, String bookTitle, String authorName, int bookReleaseYear, int numOnLoan, int numInStock) {
    this.id = id;
    this.bookTitle = bookTitle;
    this.authorName = authorName;
    this.bookReleaseYear = bookReleaseYear;
    this.numOnLoan = numOnLoan;
    this.numInStock = numInStock;
}
//Getters/Setters
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getBookTitle() {
    return bookTitle;
}

public void setBookTitle(String bookTitle) {
    this.bookTitle= bookTitle;
}

public String getAuthorName() {
    return authorName;
}

public void setAuthorName(String authorName) {
    this.authorName = authorName;
}

public int GetNumOnLoan() {
    return numOnLoan;
}

public void setNumOnLoan(int numOnLoan) {
    this.numOnLoan = numOnLoan;
}

public int getNumInStock() {
    return numInStock;
}

public void setNumInStock(int numInStock) {
    this.numInStock = numInStock;
}



public int getBookReleaseYear() {
    return bookReleaseYear;
}

public void setBookReleaseYear(int bookReleaseYear) {
    this.bookReleaseYear = bookReleaseYear;
}

Library.java

import java.util.ArrayList;

import java.util.Scanner;

public class Library {

public ArrayList<Book> books = new ArrayList<Book>();

public Library(){
      super();
    }

//Getters/Setters
public Library(ArrayList<Book> books) {
    this.books = books;
}

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

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

//Methods

//Allows loaning of books
public static void Loanbook(Scanner sc, ArrayList<Book> books){
System.out.println("Please enter a book title");
    if(sc.hasNext()){
        String criteria = sc.nextLine();
        for (int i = 0; i < books.size(); i++){
            if(criteria.equals(books.get(i).getBookTitle())){
            System.out.println("The book " + books.get(i).getBookTitle() + "    there are " + books.get(i).getNumInStock() + " in stock");
                    books.get(i).setNumOnLoan(+1);
                    System.out.println("number on loan: " +    books.get(i).GetNumOnLoan()); break; 
                        }
                    } System.out.println("Book Loaned");
                        LibraryTester.MenuReturn(sc, books);

            }   
                LibraryTester.MenuReturn(sc, books);
        }


}

public static void Returnbook(Scanner sc, ArrayList<Book> books){
System.out.println("Please enter a book title");
    if(sc.hasNext()){
        String criteria = sc.nextLine();
    for (int i = 0; i < books.size(); i++){
        if(criteria.equals(books.get(i).getBookTitle())){
        System.out.println("The book " + books.get(i).getBookTitle() +
        " is in stock," + " there are " + books.get(i).getNumInStock() + " in stock and " + books.get(i).GetNumOnLoan() + " out on loan");
        books.get(i).setNumOnLoan(-1);; break;

                        } 


                    } System.out.println("Book returned");
                        LibraryTester.MenuReturn(sc, books);

            }
        }

LibraryTester.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class LibraryTester {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Library lib = new Library();
    ArrayList<Book> books = lib.getBooks();
    books = Library.CreateBooksArrayList();
    MenuInput(sc, books);
    sc.close();
    lib.setBooks(books);

}



//prints menu
public static void PrintMenu(){
    System.out.println("Public Library Menu System");
    System.out.println("---------------------------");
    System.out.println("1: Add Book");
    System.out.println("2: Search for book");
    System.out.println("3: Loan Book");
    System.out.println("4: Return Book");
    System.out.println("5: Amend Book Details");
    System.out.println("6: Display all Books");
    System.out.println("7: Delete a Book");
    System.out.println("8: Other Options");
    System.out.println("9: Exit system");
    System.out.println("----------------------------");
}


// Allows menu input
public static void MenuInput(Scanner sc, ArrayList<Book> books){
int input = 0;
PrintMenu();
if(sc.hasNextInt()){
    input = sc.nextInt();

    switch(input){
    case 1:Library.AddBook(sc, books);
    case 2:Library.SearchBooks(sc, books);;
    case 3:Library.Loanbook(sc,books);
    case 4:Library.Returnbook(sc, books);
    case 5:Library.AmendDetails(sc,books);
    case 6:Library.DisplayAllBooks(sc,books);
    case 7:Library.Removebook(sc, books);;
    case 8:OtherMenu(sc,books);
    case 9:System.exit(0);
    }
} else {
    System.out.println("Please enter a number");
}



}



//Returns user to main menu
public static void MenuReturn(Scanner sc, ArrayList<Book> books){
System.out.println("Press any key to return to the main menu");
if(sc.hasNextLine()){
    sc.nextLine();
    MenuInput(sc, books);
    }
}





}

编辑:我似乎得到了输出:

Please enter a book title
Input:   book1
Please enter a book title
The book book1 is in stock, there are 5 in stock and 1 out on loan
Book returned
Press any key to return to the main menu

最终编辑:修复了问题,由于if语句的问题,代码没有运行,当我删除它时,所有文本一次出现时出现问题,这是由额外的&#39; sc.nextLine&#39 ;. ReturnBook在没有通话的情况下运行的问题是由于缺少休息而导致的。&#39;在菜单系统中。

感谢大家的帮助,特别是Shreyans Sheth

1 个答案:

答案 0 :(得分:2)

我只是深入研究你的代码。

你似乎有一个讨厌的错误(如果它是一个)。

我会解决这个问题您问题中的特定行:“它的功能是将存储在对象ArrayList中的'numOnLoan'变量增加1,但是当我运行该方法时,它似乎不会更改该值。

原始方法:

public void setNumOnLoan(int numOnLoan)  
{
    this.numOnLoan= numOnLoan;
}

Loanbook中的方法调用:

    books.get(i).setNumOnLoan(1);


LoanBook方法中,您只是将'+1'作为参数传递,每次只分配1。这是你需要做的。

public void setNumOnLoan(int numOnLoan)  
{
    //When numOnLoan is 1, as you have passed everytime
    //the current value gets incremented by one. I think that is what you wanted.
    this.numOnLoan += numOnLoan; //You add it to the existing variable
}



你真的应该将该功能命名为其他功能。我很确定那里有更多的错误,但这可以实现你想要的吗?


输出:
(第一次致电Loanbook
请输入书名
输入:bbbbb
书中的bbbbb有5个库存
贷款人数: 2
预订借书

(第二次致电Loanbook
请输入书名
输入:bbbbb
书中的bbbbb有5个库存
贷款数量: 3
预订借书