1. WHEN I RUN THE CODE AT FIRST AND I SELECT THE RETURN
OPTION, IT BREAKS. BUT WHEN I SELCT THE RETURN OPTION AFTER I
BORROW A BOOK, IT WORKS
2. WHY IS MY ARRAYLIST NO SAVING? WHAT I MEAN IS THAT AFTER I
BORROW A BOOK AND I END THE SYSTEM, WHEN I RESTART IT, THE BOOK I
BORROWED IS STILL AVAILABLE
3.ALSO ANY IDEAS ON WHAT MY TEST CASES COULD BE?
/*
* To change this license header, choose License Headers in
Project 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<String> availBooks = new
ArrayList<>();
ArrayList<String> borrowedBooks = new
ArrayList<>();
ArrayList<String> allBooks = new
ArrayList<>();
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 looking
for?");
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 this
Library");
System.out.println("returning to main session ...");
begin();
}
}
}
/*
* To change this license header, choose License Headers in
Project 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<String> rUsers = new
ArrayList<>();
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 Full
name");
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 not
registered");
createAccount();
}
}
}
/*
* To change this license header, choose License Headers in
Project 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 or
No");
//asking user if he or she is registered in order to get
access 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();
}
}
}