public class Book {
private String title;
private boolean borrowed;
// Creates a new Book
public Book(String bookTitle) {
this.title = bookTitle;
this.borrowed = false;
}
// Marks the book as rented
public void borrowed() {
this.borrowed = true;
}
// Marks the book as not rented
public void returned() {
this.borrowed = false;
}
// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
return this.borrowed;
}
// Returns the title of the book
public String getTitle() {
return this.title;
}
/*public static void main(String[] arguments) {
// Small test of the Book class
Book example = new Book("The Da Vinci Code");
System.out.println("Title (should be The Da Vinci Code): "
+ example.getTitle());
System.out.println("Borrowed? (should be false): "
+ example.isBorrowed());
example.borrowed();
System.out.println("Borrowed? (should be true): "
+ example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): "
+ example.isBorrowed());
}*/
}
public class Library {
private String libraryAddress;
private Book[] Books = new Book[0];
public Library(String address) {
libraryAddress = address;
}
public void addBook(Book item) {
int Len = Books.length;
Book[] Temp = new Book[Len + 1];
for (int I = 0; I < Len; I++)
Temp = Books;
Temp[Len] = item;
Books = new Book[Temp.length];
for (int I = 0; I < Books.length; I++)
Books = Temp;
}
private void returnBook(String returningBook) {
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(returningBook) == 0)
if (Books.isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
}
System.out.println("Sorry, this book is not from our catalog.");
}
private void printAvailableBooks() {
for (int I = 0; I < Books.length; I++)
if (!Books.isBorrowed())
System.out.println(Books.getTitle());
if (Books.length == 0)
System.out.println("No Books in catalog");
}
private void borrowBook(String requestedBook) {
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(requestedBook) == 0)
if (Books.isBorrowed())
{
System.out.println("Sorry, this book is already borrowed.");
return;
} else
{
Books.borrowed();
System.out.println("You successfully borrowed " + requestedBook);
return;
}
System.out.println("Sorry, this book is not in our catalog.");
}
private static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm.");
}
private void printAddress() {
System.out.println(this.libraryAddress);
}
/*public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
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();
// 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();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
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();
}*/
}
public class Book {
private String title;
private boolean borrowed;
// Creates a new Book
public Book(String bookTitle) {
this.title = bookTitle;
this.borrowed = false;
}
// Marks the book as rented
public void borrowed() {
this.borrowed = true;
}
// Marks the book as not rented
public void returned() {
this.borrowed = false;
}
// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
return this.borrowed;
}
// Returns the title of the book
public String getTitle() {
return this.title;
}
/*public static void main(String[] arguments) {
// Small test of the Book class
Book example = new Book("The Da Vinci Code");
System.out.println("Title (should be The Da Vinci Code): "
+ example.getTitle());
System.out.println("Borrowed? (should be false): "
+ example.isBorrowed());
example.borrowed();
System.out.println("Borrowed? (should be true): "
+ example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): "
+ example.isBorrowed());
}*/
}
public class Library {
private String libraryAddress;
private Book[] Books = new Book[0];
public Library(String address) {
libraryAddress = address;
}
public void addBook(Book item) {
int Len = Books.length;
Book[] Temp = new Book[Len + 1];
for (int I = 0; I < Len; I++)
Temp = Books;
Temp[Len] = item;
Books = new Book[Temp.length];
for (int I = 0; I < Books.length; I++)
Books = Temp;
}
private void returnBook(String returningBook) {
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(returningBook) == 0)
if (Books.isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
}
System.out.println("Sorry, this book is not from our catalog.");
}
private void printAvailableBooks() {
for (int I = 0; I < Books.length; I++)
if (!Books.isBorrowed())
System.out.println(Books.getTitle());
if (Books.length == 0)
System.out.println("No Books in catalog");
}
private void borrowBook(String requestedBook) {
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(requestedBook) == 0)
if (Books.isBorrowed())
{
System.out.println("Sorry, this book is already borrowed.");
return;
} else
{
Books.borrowed();
System.out.println("You successfully borrowed " + requestedBook);
return;
}
System.out.println("Sorry, this book is not in our catalog.");
}
private static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm.");
}
private void printAddress() {
System.out.println(this.libraryAddress);
}
/*public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
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();
// 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();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
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();
}*/
}
int Len = Books.length;
Book[] Temp = new Book[Len + 1];
for (int I = 0; I < Len; I++)
Temp = Books;
Temp[Len] = item;
Books = new Book[Temp.length];
for (int I = 0; I < Books.length; I++)
Books = Temp;
for (int I = 0; I < Books.length; I++)
if (Books.getTitle().compareTo(returningBook) == 0)
if (Books.isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
import java.util.*;
public class Library {
String libaryAddress;
ArrayList<Book> books = new ArrayList<Book>(); //Does <Book> only need to be declared once?
public void addBook(Book item) {
books.add(item);
}
public void borrowBook(String requestedBook) {
for (int i = 0; i < Books.length; i++)
if (books(i).getTitle().compareTo(requestedBook) == 0)
if (books(i).isBorrowed())
{
System.out.println("Sorry, this book is already borrowed.");
return;
} else
{
books(i).borrowed();
System.out.println("You successfully borrowed " + requestedBook);
return;
}
System.out.println("Sorry, this book is not in our catalog.");
}
private void returnBook(String returningBook) {
for (int i = 0; i < Books.length; i++)
if (books(i).getTitle().compareTo(returningBook) == 0)
if (books(i).isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
}
System.out.println("Sorry, this book is not from our catalog.");
}
public void printBooks() {
if(books.size != 0) {
for(int i=0; i < books.size(); i++) {
Book item = books.get(i)
String title = item.getTitle();
System.out.println(title);
}
}
else System.out.println("No available books");
}
private static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm");
}
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 each 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 addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
//Try to borrow Lord 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");
//Return the Lord Of The Rings to the first library
firstLibrary.returnBook("The Lord Of The Rings");
//Print titles of available books from first library
printOpeningHours();
}
}
import java.util.*;
public class Library {
String libaryAddress;
ArrayList<Book> books = new ArrayList<Book>(); //Does <Book> only need to be declared once?
public void addBook(Book item) {
books.add(item);
}
public void borrowBook(String requestedBook) {
for (int i = 0; i < Books.length; i++)
if (books(i).getTitle().compareTo(requestedBook) == 0)
if (books(i).isBorrowed())
{
System.out.println("Sorry, this book is already borrowed.");
return;
} else
{
books(i).borrowed();
System.out.println("You successfully borrowed " + requestedBook);
return;
}
System.out.println("Sorry, this book is not in our catalog.");
}
private void returnBook(String returningBook) {
for (int i = 0; i < Books.length; i++)
if (books(i).getTitle().compareTo(returningBook) == 0)
if (books(i).isBorrowed())
{
Books.returned();
System.out.println("You successfully returned " + returningBook);
return;
}
System.out.println("Sorry, this book is not from our catalog.");
}
public void printBooks() {
if(books.size != 0) {
for(int i=0; i < books.size(); i++) {
Book item = books.get(i)
String title = item.getTitle();
System.out.println(title);
}
}
else System.out.println("No available books");
}
private static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm");
}
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 each 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 addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
//Try to borrow Lord 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");
//Return the Lord Of The Rings to the first library
firstLibrary.returnBook("The Lord Of The Rings");
//Print titles of available books from first library
printOpeningHours();
}
}
Last reply 1 month ago
Free website hosting providers with no strings attached?Posted 3 months ago
iDEA silver citizen badge three. Milkshake shack coding problem.Last reply 4 months ago
How do you practice programming java for beginners?Last reply 8 months ago
hows the employment market for tech work in the UK?Last reply 1 month ago
Free website hosting providers with no strings attached?Posted 3 months ago
iDEA silver citizen badge three. Milkshake shack coding problem.Last reply 4 months ago
How do you practice programming java for beginners?Last reply 8 months ago
hows the employment market for tech work in the UK?