Hello! for reference, when i compile without debugging i get the following error
ID: 3770450 • Letter: H
Question
Hello!
for reference, when i compile without debugging i get the following errors:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor Patron(String) is undefined
at LibraryTest.main(LibraryTest.java:31)
1)The Library Class, in which I am unsuccesfully invoking methods from the other classes
import java.util.*;
import java.lang.System.*;
import java.io.*;
public class Library{
public static void displayMenu(){
System.out.println("******************************************************************");
System.out.println(" Welcome to The Library! ");
System.out.println(" Please choose from one of the following menu options ");
System.out.println("******************************************************************");
System.out.println("1- Add a New Patron ");
System.out.println("2- Add a New Book ");
System.out.println("3- Edit Patron");
System.out.println("4- Edit Book ");
System.out.println("5- Display All Patrons");
System.out.println("6- Display All Books");
System.out.println("7- Borrow a Book");
System.out.println("8- Return a Book");
System.out.println("0- Exit ");
System.out.println("********************************************************************");
System.out.println(" Please enter your selection from the menu above: ");
System.out.println("********************************************************************");
Scanner choiceInput = new Scanner(System.in);
int choice = choiceInput.nextInt(); //User inputs a selection (integer)
/**
* while loop only accepts menu selections 0-8
*/
while((choice!=0)||(choice!=1)||(choice!=2)||(choice!=3)||(choice!=4)||
(choice!=5)||(choice!=6)||(choice!=7)||(choice!=8))
{
System.out.print("Menu selection invalid, please select an option 0-8 ");
choice = choiceInput.nextInt();
}
ArrayList<String> UserList = new ArrayList<String>();
ArrayList<Book> BookList = new ArrayList<Book>();
if(choice==1)
{
Patron.addPatron();
return;
}
if(choice==2)
{
BookInterface.addBook();
return;
}
if(choice==3)
{
Patron.editPatron();
return;
}
if(choice==4)
{
BookInterface.editBook();
}
if(choice==5 && BookList.size()>0)
{
BookInterface.displayBookList();
}
if(choice == 5 && BookList.size() == 0)
{
System.out.println("There are currently no books in the Library, please add a new book.");
Library.displayMenu();
}
if(choice==6)
{
BookInterface.displayBookList();
}
if(choice==8)
{
BookInterface.returnBook();
}
if(choice==0)
{
break exit;
}
}
}
2) BookInterface Class
import java.util.*;
public class BookInterface{
public String title;
public String author;
public String status;
public String borrower;
public String dateBorrowed;
public String dueDate;
public String status1 = "Available";
public String status2 = "Borrowed";
public String bookChoice;
static int selection ;
static Scanner userInput = new Scanner(System.in);
static Scanner selectionInput = new Scanner(System.in);
public String displayBookInfo(){
String BookInfo = "----------------------------"+
" Title:.................." + title +
" Author:................." + author +
" Status:................." + status +
" Borrower:..............." + borrower +
" Date Borrowed:.........." + dateBorrowed +
" Due Date:............" + dueDate +
" ----------------------------";
return BookInfo;
}
public static void addBook(){
Book newBook = new Book(); //create new book object with status "Available."
newBook.createBook();
BookList.add(newBook); //add the book to the BookList ArrayList.
System.out.println("---------------------------------------------------------");
System.out.println(" You have successfully added the book to the Library. ");
System.out.println("---------------------------------------------------------");
}
public static void editBook(){
}
public void borrowBook(){
System.out.println("---------------------------------------------------------");
System.out.println(" The following books are registered to the Library: ");
System.out.println("---------------------------------------------------------");
displayBookList();
//borrow Loop1:
while(choice == 7){
System.out.println("Please choose an available book by entering it's index number. ");
bookChoice = choiceInput.nextInt()-1;//register user's book choice.
if(bookChoice > BookList.size()){
System.out.println("> The number of the book you entered is not in the list!");
}else if(bookChoice <= BookList.size()){
break borrowLoop1;
}
}
//borrow Loop2:
while(choice == 7){
//Check if the book is available.
if (BookList.get(bookChoice).status.equalsIgnoreCase(status1) && BookList.size() >= bookChoice){
//Print the borrowed book information and change the book status to borrowed.
BookList.get(bookChoice).status = "Borrowed";
System.out.printf(" > You have chosen the following book: %s ", BookList.get(bookChoice).displayBook());
//add the user name to the book borrower variable:
BookList.get(bookChoice).borrower = borrower;
BookList.get(bookChoice).borrowDate = "Today's Date.";
BookList.get(bookChoice).dueDate = "Seven days from today's date";
break; //borrowLoop2;
}else if(BookList.get(bookChoice).status.equalsIgnoreCase(status2) && BookList.size() >= bookChoice){
System.out.println("> The Book you are trying to borrow is unavailable!");
break borrowLoop2;
}else if(bookChoice > BookList.size()-1){
System.out.println("> The number you entered in not in the list!");
choice = 7;
break borrowLoop2;
}
}
}
public void returnBook(){
System.out.println("Enter the Title of the book you wish to return: ");
String returnBookTitle = userInput.nextLine();
int x = 0;
while (x < BookList.size()){//Search for the book by title, if it exists change it's status,
//it's borrower and borrowDate.
if (BookList.get(x).title.equalsIgnoreCase(returnBookTitle)){
BookList.get(x).status = "Available";
BookList.get(x).borrower = "none";
BookList.get(x).borrowDate = "none";
System.out.println("You have successfully returned the book to the Library");
Book.displayMenu();//Display main menu.
choice = choiceInput.nextInt();//Register new choice.
break;//if a title is found, break out of the loop and display choice menu.
}
x = x+1;
}//end of while loop.
x = 0;
while (x < BookList.size() && BookList.size() > 0){//Search for the title and if it's not in the library,
//print a warning message to the user, and register a new menu choice.
if (BookList.get(x).title.equalsIgnoreCase(returnBookTitle)){
}else{
System.out.println("The are no books with this title in the Library. " + " " +
"Please double check your entry or add the book to the Library. ");
Book.displayMenu();//Display main menu.
choice = choiceInput.nextInt();//Register new choice.
}//End of else statement.
}//End of while loop.
Book.displayMenu();//Display main menu.
choice = choiceInput.nextInt(); //Register new choice.
}
public void displayBookList(){
if (BookList.size() == 0){ //If the library is empty, it goes back to main menu and choice.
System.out.println(">-------------------------------------------------------------");
System.out.println("> The Library is currently empty, please add a new book. ");
System.out.println(">-------------------------------------------------------------");
Book.displayMenu();//Display to main menu.
choice = choiceInput.nextInt(); //Register new choice.
}
else{
for (int i = 0; i < BookList.size(); i++){
System.out.printf(" -----------Book Index: [%s]--------------------------------- ",i+1);
System.out.println(BookList.get(i).displayBook());
System.out.println("-------------------------------------------------------------");
}//End of For Loop.
}// End of Else Statement.
}//End of if Statement.
}
3) Book class that implements BookInterface
import java.util.*;
public class Book extends BookInterface{
public Book(String isbnIn, String titleIn, String authorIn){
title = titleIn;
author = authorIn;
}
public Book(String author, String title){
}
public void createBook(){
System.out.println("Please enter the title of the book you wish to add to the Library. ");
Scanner titleInput = new Scanner(System.in);
title = titleInput.nextLine();
System.out.println("Please enter the Author of the book you wish to add to the Library. ");
Scanner authorInput = new Scanner(System.in);
author = authorInput.nextLine();
borrower = "Not currently checked out";
dateBorrowed = "N/A";
dueDate = "N/A";
status = "Available for check out";
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public String toString(){
return author + ", " + title + ", " + dueDate;
}
}
4) The Patron Class
import java.util.Scanner;
public class Patron{
private String firstName;
private String lastName;
private String patron = "firstName + lastName";
private Book book1, book2, book3;
public void addPatron(){
System.out.println("Please enter the First Name of the new Patron");
Scanner firstNameInput = new Scanner(System.in);
firstName = firstNameInput.nextLine();
System.out.println("Please enter the Last Name of the new Patron");
Scanner lastNameInput = new Scanner(System.in);
lastName = lastNameInput.nextLine();
patron = "firstName + lastName";
UserList.add(patron); //Add the userName to the UserList arrayList.
}
public void editPatron(){
System.out.println("Please enter the edited first name of the patron");
Scanner firstNameInput = new Scanner(System.in);
firstName = firstNameInput.nextLine();
System.out.println("Please enter the edited last name of the new Patron");
Scanner lastNameInput = new Scanner(System.in);
lastName = lastNameInput.nextLine();
patron = "firstName + lastName";
}
public void patron(String n){
patron = n;
book1 = null;
book2 = null;
book3 = null;
}
public String getPatron(){
return patron;
}
public String borrowBook(Book b){
if (book1 == null)
{
book1 = b;
return "Book is available";
}
else if (book2 == null)
{
book2 = b;
return "Book is available for check out";
}
else if (book3 == null)
{
book3 = b;
return "Book is available for check out";
}
else
return "Book is currently checked out";
}
public String returnBook(String title) {
if (book1 != null)
if (book1.getTitle().equals(title))
{
book1 = null;
return "Book has been returned";
}
if (book2 != null)
if (book1.getTitle().equals(title))
{
book2 = null;
return "Book has been returned";
}
if (book3 != null)
if (book1.getTitle().equals(title))
{
book3 = null;
return "Book has been returned";
}
return "Book is available for check out";
}
public boolean hasBook(String title){
if (book1 != null)
if (book1.getTitle().equals(title))
return true;
if (book2 != null)
if (book2.getTitle().equals(title))
return true;
if (book3 != null)
if (book3.getTitle().equals(title))
return true;
return false;
}
public String toStringName(){
String str = "Patron's name: " + firstName + lastName;
if (book1 != null)
str = str + " " + book1;
if (book2 != null)
str = str + " " + book2;
if (book3 != null)
str = str + " " + book3;
return str;
}
}
I hope this was clear enough! If you made it this far thank you so much!!
Explanation / Answer
package library;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Book
{
public String title;
public String author;
public String publisher;
public String publicationYear;
public String status;
public String borrower;
public String borrowDate;
public String returnDate;
public String status1 = "Available";
public String status2 = "Borrowed";
public int BookChoice;
static ArrayList<String> UserList = new ArrayList<String>();
static ArrayList<Book> BookList = new ArrayList<Book>();
static int choice ;
static Scanner userInput = new Scanner(System.in);
static Scanner choiceInput = new Scanner(System.in);
public static void displayFirstMenu(){
System.out.println("> Choose one of the options below by typing the corresponding number: ");
System.out.println("2- Add a book to the Library.");
System.out.println("6- Blow up library.");
System.out.println("7- Back to main menu.");
System.out.println("0- Exit.");
System.out.println("> Enter your option here: ");
choice = choiceInput.nextInt();
}
public static void displaySecondMenu(){
System.out.println("1- Check library list.");
System.out.println("2- Add a book to the Library.");
System.out.println("3- Borrow a book.");
System.out.println("4- Return a book.");
System.out.println("5- Delete a book.");
System.out.println("6- Blow up library.");
System.out.println("7- Back to main menu.");
System.out.println("0- Exit.");
System.out.println("> Enter your option here: ");
choice = choiceInput.nextInt();
}
public String displayBook()
{
String BookInfo = " "+
" Title:.................."+title+
" Author:................."+author+
" Publisher:.............."+publisher+
" PublicationYear:........"+publicationYear+
" Status:................."+status+
" Borrower:..............."+borrower+
" Date Borrowed:.........."+borrowDate+
" Return date:............"+returnDate+
return BookInfo;
}
public void createBook(){
System.out.println("> Enter the title of the book: ");
title = userInput.nextLine();
System.out.println("> Enter the author of the book: ");
author = userInput.nextLine();
System.out.println("> Enter the publisher of the book: ");
publisher = userInput.nextLine();
System.out.println("> Enter the publication year of the book: ");
publicationYear = userInput.nextLine();
borrower = "nobody";
borrowDate = "none";
returnDate = "none";
status = "Available";
}
public void addBook(){
Book newBook = new Book(); //create new book object with status "Available."
newBook.createBook();
BookList.add(newBook);//add the book to the BookList ArrayList.
System.out.println("> You have successfully added the book to the library! ");
}
public void displayBookList(){
if (BookList.size() == 0){//If the library is empty, it goes back to main menu and choice.
System.out.println("> There Library is Emply! Please add a book first! ");
Book.displayFirstMenu();
choice = choiceInput.nextInt();//Register new choice.
} else {
for (int i = 0; i < BookList.size(); i++){
System.out.printf(" >-----------Book Index: [%s]--------------------------------- ",i+1);
System.out.println(BookList.get(i).displayBook());
}
}
}
public void borrowBook()
{
System.out.println("> Here are all the books registered in the library: ");
displayBookList();
borrowLoop1:
while(choice == 3){
System.out.println(" > Choose an available book from the above list and write down it's index number: ");
BookChoice = choiceInput.nextInt()-1;
if(BookChoice > BookList.size()){
System.out.println("> The number of the book you entered is not in the list!");
choice = 7;
}else if(BookChoice <= BookList.size()){
break borrowLoop1;
}
}
borrowLoop2:
while(choice == 3)
{
if (BookList.get(BookChoice).status.equalsIgnoreCase(status1) && BookList.size() >= BookChoice)
{
BookList.get(BookChoice).status = "Borrowed";
System.out.printf(" > You have chosen the following book: %s ", BookList.get(BookChoice).displayBook());
BookList.get(BookChoice).borrower = borrower;
BookList.get(BookChoice).borrowDate = "Today.";
BookList.get(BookChoice).returnDate = "In two weeks.";
System.out.println("> You have to return the book in two weeks!");
choice = 7;
break borrowLoop2;
}else if(BookList.get(BookChoice).status.equalsIgnoreCase(status2) && BookList.size() >= BookChoice){
System.out.println("> The Book you are trying to borrow is unavailable!");
choice = 7;
break borrowLoop2;
}else if(BookChoice > BookList.size()-1){
System.out.println("> The number you entered in not in the list!");
choice = 7;
break borrowLoop2;
}
}
}
public void returnBook()
{
System.out.println("> Enter the Title of the book you want to return: ");
String returnBookTitle = userInput.nextLine();
int x = 0;
while (x < BookList.size())
{
if (BookList.get(x).title.equalsIgnoreCase(returnBookTitle)){
BookList.get(x).status = "Available";
BookList.get(x).borrower = "none";
BookList.get(x).borrowDate = "none";
System.out.println("> You have successfully returned the book to the library!");
Book.displayFirstMenu();
choice = choiceInput.nextInt();
break;
}
x = x+1;
}
x = 0;
while (x < BookList.size() && BookList.size() > 0)
{
if (BookList.get(x).title.equalsIgnoreCase(returnBookTitle))
{
}
else
{
System.out.println("> The are no books with this title in the library." +
" Please make sure the title is spelt correctly or choose to add the book " +
"to the library from the main menu. ");
Book.displayFirstMenu();//Display main menu.
choice = choiceInput.nextInt();//Register new choice.
}
}
Book.displayFirstMenu();
choice = choiceInput.nextInt();
}
public void removeBook(){
int i = 0;
System.out.println("> Here are all the books registered in the library: ");
while (i < BookList.size() && BookList.size() > 0)
{
System.out.printf(" > Book number %s: %s",i+1,BookList.get(i));
i = i+1;
}
System.out.println(" > Choose an available book from the above list and write down it's number: ");
int BookChoice = choiceInput.nextInt()-1;
while(choice == 5){
try{
if (BookChoice > 0 && BookChoice < BookList.size() && BookList.get(BookChoice).status.equalsIgnoreCase("Available"))
{
BookList.remove(BookChoice);
System.out.printf(" > You have chosen to delete the following book: %s ", BookList.get(BookChoice));
System.out.printf(" > The Book %s is deleted. ", BookList.get(BookChoice));
choice = 7;
}
}
catch(InputMismatchException error)
{
System.out.println("<ERROR> Please enter a number of book from the list: ");
choiceInput.nextInt();
choice = 5;
}
catch(IndexOutOfBoundsException error)
{
System.out.println("<ERROR> Please enter a number of book from the list: ");
choice = 5;
}
}
}
public void emptyLibrary()
{
System.out.println("> WARNING < You have chosen to delete all books in the library! ");
System.out.println("> Are you sure?? Enter yes or no: ");
String confirmation = userInput.nextLine();
try{
if(confirmation.equalsIgnoreCase("yes")){
System.out.println("> Library is being deleted...");
BookList.clear();
System.out.println("> Library is Empty!");
choice = 7;
}
}catch(InputMismatchException error){
System.out.println("<ERROR> Make sure you spell yes or no correctrly: ");
choice = 6;
}
}
public void addUser(){
System.out.println("> Enter your name: ");
borrower = userInput.nextLine();
UserList.add(borrower);
}
public void run(){
System.out.println("< 1 >");
addUser();
System.out.println("< 2 >");
Book.displayFirstMenu();
System.out.println("3- Entering main while loop");
exit:
while(choice != 0){
try{
if(choice == 1 && BookList.size() > 0){
displayBookList();
choice = 7;
}
if(choice == 1 && BookList.size() == 0){
System.out.println("<ERROR> Library is empty! Please add a Book first!");
choice = 7;
}
if(choice == 2){
//createBook();
addBook();
displaySecondMenu();
}
if(choice == 3){
if(BookList.size() > 0){
borrowBook();
}
}
if(choice == 4){
returnBook();
}
if(choice == 5){
removeBook();
try{
if(BookList.size() > 0){
displaySecondMenu();
}
}catch(IndexOutOfBoundsException error){
System.out.println("<ERROR> The array is Empty! Please add a book first!");
choice = 7;
}
}
if(choice == 6){
emptyLibrary();
}
if(choice == 7){
if(BookList.size() > 0){
displaySecondMenu();
}else if(BookList.size() == 0){
displayFirstMenu();
}
}
if(choice == 0){
break exit;
}
}catch(InputMismatchException error){
System.out.println("<5- Breaking from main while loop>");
break exit;
}
}
System.out.println("Library!");
}
public static void main(String[] args)
{
System.out.println("Welcome to the library!");
Book newBook = new Book();
newBook.run();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.