Reputation: 429
Hi I am encountering a problem with a uni project I am working on. I am trying to validate the input so that the BookID
that is entered when attempting to loan the book, is only valid if it exists in the array named 'BookList
'. At the minute I have it working so that it validates it to make sure that an integer is entered, and not letters or negative numbers.
I have tried endlessly but I am stuck completely?? any tips or help, I would much appreciate it. thanks
//loan a book method
public void loanBook() {
int loanID;
do {
System.out.println("Please enter the Book ID of the book that you wish to borrow");
while (!input.hasNextInt()) { // checking that the ID entered is an integer - validation
System.out.println("That is not an integer");
input.nextLine(); //pushing the scanner on
}
loanID = input.nextInt(); //setting the loanID variable equal to the input from the scanner.
}
while (loanID < 0 || loanID > 100000000); //VALIDATION - NEED TO CHANGE SO THAT WHILE LOAN ID EXISTS IN ARRAY LIST ????
for (int i = 0; i < BookList.size(); i++) { //for loop to go through and check for the ID entered to remove the book that it corresponds to
if (BookList.get(i).getBookID() == loanID ) {
System.out.println("The book named : " + BookList.get(i).getTitle() + " has now been taken out on loan. Please return within 2 weeks!");
BookList.get(i).setStatus("On Loan");;
}//end of if statement
}//end of for loop
} //end of return book method
Upvotes: 0
Views: 420
Reputation: 3408
You can use the .contains() method for Arraylists. You just need to make sure you are removing items depending on their status.
if(bookList.contains(loanID)){
//logic for book exists
}else{
//book is on loan.
}
Now as I said you need to make sure you are doing proper verification for removing of the books on loan etc for this to work. The way you have your logic right now is doing a LOT of unnecessary work with your loops. This way you can easily scan the list and find the item needed. Of course there are better ways to set up your lists etc but this should work keeping your code very similar.
EDIT
You requested information on how you would find the index of the item after you have verified it exists. This is still very simple. Once you have verified that the item exists you would use the line:
int index = bookList.indexOf(loanID);
This will return the index in your ArrayList for the location of the book. Once you have the index you can begin doing everything you were doing before with:
bookList.get(index).getBookId();
or
bookList.get(bookList.indexOf(itemId)).getBookId();
This is almost exactly what you were doing previously but cut down to 3 lines and can be made even shorter.
if (BookList.contains(loanID)) {
int index = BookList.indexOf(loanId);
if (!BookList.get(index).getStatus().equals("On Loan")) {
System.out.println("The book named: " + BookList.get(index).getTitle() + " has now been taken on loan.");
BookList.get(index).setStatus("On Loan.");
}else{
System.out.println("Book is on loan already.");
}
}else{
//logic for not existing.
}
Upvotes: 2
Reputation: 65
Create a variable int isExist = 0; After getting the input from user...go through the array and see if that book exists. Then make isExist=1; And then of the loop just make if statement
if( isExist == 0) {
System.out.println("Book is not found");
}
By the way once you have found the book in the array you want to break out of the loop using break;
Upvotes: 0