Question

In: Computer Science

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

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();
}
}
}

Solutions

Expert Solution

Hi,

1) There is only one change in your returnBook() function.you should use nextLine() instead of next() to read string line.

public void returnBook() {
        
System.out.println("What is the name of the book "
+ "you would like to return?");

 String returnName = input.nextLine(); // you were using next() instead nextLine() for string line
 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();
}
}

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.

When you end the system means exit the system after pressing option 4 then you will be exit from the program and everything will be lost from memory..you are not saving your data in the database but in arraylist which is temprary data structure to hold the data.

3) ALSO ANY IDEAS ON WHAT MY TEST CASES COULD BE?

A TEST CASE is a set of actions executed to verify a particular feature or functionality of your software application.

For test cases: suppose you are writing test case for registration part then

First, check result, registraion details if user is entering correct details

Second, check result if user is entering any invalid entry

Third, check result if user is inputing empty string

This is nothing but a test case for the user registration.you can write then same test cases for Login, Search book, borrow book and return books.

Feel free to ask if any question you have by comment box?

Thanks


Related Solutions

Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
i need a javafx code 1. first create menu bar with option open and save 2....
i need a javafx code 1. first create menu bar with option open and save 2. when user click on open it opens the file only image file 3. when user click on save it saves as new file.
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
This code needs to run a working Reverse Polish Calculator, but when I input my commands...
This code needs to run a working Reverse Polish Calculator, but when I input my commands it only pops my inputs never push them. So my answer is always zero. Here is my code. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> struct Node { int element; struct Node *next; }; //Global variable struct Node *top = NULL; void push(int ele) { struct Node *newNode; newNode = (struct Node *) malloc(sizeof(struct Node)); newNode->element = ele; newNode->next = top; top = newNode; }...
You must select a high-speed transportation option between Charlotte and Raleigh. The first option is a...
You must select a high-speed transportation option between Charlotte and Raleigh. The first option is a revolutionary Tube Train with an initial investment of $350 million with yearly maintenance costs of $25 million. You will have to overhaul the Tube Train every 50 years at a cost of $350 million. The second option is the reliable Point-toPoint Transporter with an initial investment of X dollars and a yearly maintenance cost of 2% of the initial investment (0.02*X). You will overhaul...
Below is my code in C#, When I run it, the output shows System.32[], Can you...
Below is my code in C#, When I run it, the output shows System.32[], Can you please check and let me know what is the problem in the code. class Program { static void Main(string[] args) { int number=12; Console.WriteLine(FizzArray(number)); } public static int[] FizzArray(int number) { int[] array = new int[number]; for (int i = 1; i < number; i++) array[i] = i; return array; }
(Choose Best Option) 1) The probability of a type I error increases when: a) Significance is...
(Choose Best Option) 1) The probability of a type I error increases when: a) Significance is lowered b) The number of samples is lowered c) Using two-tailed t-test rather than a one-tailed t-test d) Multiple pair-wise comparisons are performed 2) A population has a mean weight of 70 kg with a standard deviation of 5 kg. The weight of a sample of N = 100 subjects were taken. The following statement is false: a) 95% of the people in the...
(Choose Best Option) 1) The probability of a type I error increases when: a) Significance is...
(Choose Best Option) 1) The probability of a type I error increases when: a) Significance is lowered b)   The number of samples is lowered c)   Using two-tailed t-test rather than a one-tailed t-test d)   Multiple pair-wise comparisons are performed   2) A population has a mean weight of 70 kg with a standard deviation of 5 kg. The weight of a sample of N = 100 subjects were taken. The following statement is false: a)   95% of the people in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT