1. WHEN I RUN THE CODE AT FIRST AND I SELECT THE RETURN OPTION, IT BREAKS....

90.2K

Verified Solution

Question

Programming

1. WHEN I RUN THE CODE AT FIRST AND I SELECT THE RETURNOPTION, IT BREAKS. BUT WHEN I SELCT THE RETURN OPTION AFTER IBORROW A BOOK, IT WORKS

2. WHY IS MY ARRAYLIST NO SAVING? WHAT I MEAN IS THAT AFTER IBORROW A BOOK AND I END THE SYSTEM, WHEN I RESTART IT, THE BOOK IBORROWED IS STILL AVAILABLE

3.ALSO ANY IDEAS ON WHAT MY TEST CASES COULD BE?
/*
* To change this license header, choose License Headers inProject Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package librarysystem;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
*/
public class Books {
ArrayList availBooks = newArrayList<>();
ArrayList borrowedBooks = newArrayList<>();
ArrayList allBooks = newArrayList<>();
Scanner input = new Scanner(System.in);
Books() {
}
public void availableBooks() {
availBooks.add(\"Chronicle of Narnia\");
availBooks.add(\"Harry Potter\");
availBooks.add(\"The fault in our stars\");
availBooks.add(\"Wizard of Oz\");
}
public void allBooks() {
allBooks.add(\"Chronicle of Narnia\");
allBooks.add(\"Harry Potter\");
allBooks.add(\"The fault in our stars\");
allBooks.add(\"Wizard of Oz\");
allBooks.add(\"James Bond\");
allBooks.add(\"The Hobbit\");
allBooks.add(\"Fellowship of the ring\");
}
public void borrowedBooks() {
borrowedBooks.add(\"James Bond\");
borrowedBooks.add(\"The Hobbit\");
borrowedBooks.add(\"Fellowship of the ring\");
}
public void begin() {
System.out.println(\".............................\");
System.out.println(\"What would you like to do today?\");
System.out.println(\"1. Search for book\"
+ \"\n\" + \"2. Borrow book\" + \"\n\" + \"3. Return book\"
+ \"\n\" + \"4. Log out\" + \"\n pick 1,2,3 or 4\");
int ans = input.nextInt();
input.nextLine();
if (ans == 1) {
searchBooks();
} else if (ans == 2) {
borrow();
} else if (ans == 3) {
returnBook();
} else if (ans == 4) {
System.out.println(\"Logging out...\");
System.exit(0);
} else {
System.out.println(\"Pleasepick 1,2,3 or 4\");
}
}
public void searchBooks() {
System.out.println(\"What is the name of the book youre lookingfor?\");
String bookName = input.nextLine();
if (allBooks.contains(bookName) &&borrowedBooks.contains(bookName)) {
System.out.println(\"Book found!: \" + bookName);
System.out.println(\"Status: Unavailble for borrowing\");
System.out.println(\"returning to main session ...\");
begin();
} else if (allBooks.contains(bookName)
&& availBooks.contains(bookName)) {
System.out.println(\"Book found!: \" + bookName);
System.out.println(\"Status: Available for borrowing\");
System.out.println(\"Would you like to borrow\" + bookName
+ \"? yes or no\");
String ans = input.nextLine();
if (ans.equalsIgnoreCase(\"yes\")) {
borrow1(bookName);
} else if (ans.equalsIgnoreCase(\"no\")) {
System.out.println(\"returning to main session ...\");
begin();
} else {
System.out.println(\"returning to main session ...\");
}
} else {
System.out.println(\"Sorry, we could not find \" +bookName);
System.out.println(\"returning to main session ...\");
begin();
}
}
public void borrow() {
System.out.println(\"What is the name of the book you\"
+ \" would like to borrow?\");
String borrowName = input.nextLine();
if (allBooks.contains(borrowName)
&& borrowedBooks.contains(borrowName)) {
System.out.println(\"Book found!: \" + borrowName);
System.out.println(\"Status: Unavailble for borrowing\");
System.out.println(\"returning to main session ...\");
begin();
} else if (allBooks.contains(borrowName)
&& availBooks.contains(borrowName)) {
System.out.println(\"Book found!: \" + borrowName);
System.out.println(\"Status: Available for borrowing\");
System.out.println(\"Would you like to borrow \" +borrowName
+ \"? yes or no\");
String ans = input.nextLine();
if (ans.equalsIgnoreCase(\"Yes\")) {
borrow1(borrowName);
} else if (ans.equalsIgnoreCase(\"No\")) {
System.out.println(\"returning to main session ...\");
begin();
} else {
System.out.println(\"returning to main session ...\");
}
} else {
System.out.println(\"Sorry, we could not find \" +borrowName);
System.out.println(\"returning to main session ...\");
begin();
}
}
public void borrow1(String nameOfBook) {
System.out.println(\"Borrowing ...\");
availBooks.remove(nameOfBook);
borrowedBooks.add(nameOfBook);
System.out.println(\"You have successfully borrowed \" +nameOfBook);
System.out.println(\"returning to main session ...\");
begin();
}
public void returnBook() {
System.out.println(\"What is the name of the book \"
+ \"you would like to return?\");
String returnName = input.next();
if (allBooks.contains(returnName)) {
availBooks.add(returnName);
borrowedBooks.remove(returnName);
System.out.println(\"Returning ...\");
System.out.println(\"Return successful!\");
System.out.println(\"returning to main session ...\");
begin();
} else {
System.out.println(\"This book does not belong to thisLibrary\");
System.out.println(\"returning to main session ...\");
begin();
}
}
}
/*
* To change this license header, choose License Headers inProject Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package librarysystem;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.util.List;
/**
*
*/
public class Users {
Books book = new Books();
ArrayList rUsers = newArrayList<>();
Scanner input = new Scanner(System.in);
Users() {
}
public void arrayList() {
rUsers.add(\"name1\");
rUsers.add(\"name2\");
}
public void createAccount() {
System.out.println(\"To register, please enter your Fullname\");
String fullname = input.nextLine();
rUsers.add(fullname);
System.out.println(\"You have succesfully registered!\");
book.allBooks();
book.availableBooks();
book.borrowedBooks();
//CALL THE METHOD THAT STARTS THE OPTIONS
book.begin();
}
public void logIn() {
System.out.println(\"Please enter your name\");
String name = input.nextLine();
if (rUsers.contains(name)) {
System.out.println(\"You are logged in!\");
book.allBooks();
book.availableBooks();
book.borrowedBooks();
book.begin();
} else {
//run the loop that asks the questions
System.out.println(\"Invalid Login\");
System.out.println(\"Please create account if you are notregistered\");
createAccount();
}
}
}
/*
* To change this license header, choose License Headers inProject Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package librarysystem;
import java.util.Scanner;
/**
*
*/
public class LibrarySystem {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Users users = new Users();
Scanner input = new Scanner(System.in);
System.out.println(\"WELCOME TO LIBRARY SYSTEM\");
System.out.println(\"Are you a registered user? Yes orNo\");
//asking user if he or she is registered in order to getaccess into
//the system
String answer = input.nextLine();
// the answer the user types in
if (answer.equalsIgnoreCase(\"Yes\")) {
//if the user is part of the system log in
users.arrayList();
users.logIn();
} else {
users.createAccount();
users.logIn();
}
}
}

Answer & Explanation Solved by verified expert
3.8 Ratings (514 Votes)
Hi 1 There is only one change in your returnBook functionyou should use nextLine instead of next to read string line public void returnBook SystemoutprintlnWhat is the name of the book you would like to return String returnName inputnextLine you were using next instead nextLine for string line if allBookscontainsreturnName    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students