Hi everyone
I am writing a simple java code Library class and Book class. I have hard time to write the code to return a book after borrowing it. I need help. This is my code
#########################Book class################################
package Library;
public class Book {
String title;
boolean borrowed = false;
//borrowed = false;
// Creates a new Book
public Book(String bookTitle) {
title = bookTitle;
}
// Marks the book as rented
public void borrowed() {
borrowed = true;
}
// Marks the book as not rented
public void returned() {
borrowed = false;
}
// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
if (borrowed)
return true;
else
return false;
}
// Returns the title of the book
public String getTitle() {
return title;
}
}
##############################Library class###################################
package Library;
public class Library {
String address;
String title;
public Book myBook;
Library(String streetname) {
address = streetname;
}
String[] tmp = new String[4];
int bookcount = 0;
//adding book
public void addBook(Book book) {
tmp[bookcount] = book.getTitle();
bookcount++;
}
//Library hours
public static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm.");
}
//Library address
public void printAddress() {
System.out.println(this.address);
}
//Avaiable book
public void printAvailableBooks() {
for (int j = 0; j < bookcount; j++)
{
System.out.println(tmp[j]);
}
}
//borrowed book
public void borrowBook(String title) {
for (int i = 0; i < bookcount; i++)
{
if (tmp[i].equals(title))
tmp[i] = "";
}
}
//returning book -----> I have trouble here
/* public void returnBook(String title) {
for (int i = 0; i < bookcount; i++)
{
if (tmp[i].equals(title))
System.out.println("Your book is from other library. ");
else
}
}*/
########################test bench#########################################
public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
// Add four books to the first library
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new Book("The Lord of the Rings"));
// Print opening hours and the addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
System.out.println("Borrowing Le Petit Prince:");
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.borrowBook("Le Petit Prince");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();
// Return The Lords of the Rings to the first library
System.out.println("Returning The Lord of the Rings:");
//firstLibrary.returnBook("The Lord of the Rings");
System.out.println();
// Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
}
}