import java.io.
*;
import java.util.ArrayList;
import java.util.Scanner;
// Define the Person class
class Person implements Serializable {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}
// Define the Book class
class Book implements Serializable {
private String name;
private String publisher;
private Person author;
public Book(String name, String publisher, Person author) {
this.name = name;
this.publisher = publisher;
this.author = author;
}
public String getName() {
return name;
}
public String getPublisher() {
return publisher;
}
public Person getAuthor() {
return author;
}
@Override
public String toString() {
return "Book{name='" + name + "', publisher='" + publisher + "', author=" +
author + "}";
}
}
public class BookStore {
private static final String FILE_NAME = "BookStore";
public static void main(String[] args) {
// Create Person objects for authors
Person author1 = new Person("J.K. Rowling");
Person author2 = new Person("George R.R. Martin");
Person author3 = new Person("J.R.R. Tolkien");
Person author4 = new Person("Agatha Christie");
Person author5 = new Person("Dan Brown");
// Create Book objects
Book book1 = new Book("Harry Potter", "Bloomsbury", author1);
Book book2 = new Book("Game of Thrones", "Bantam Books", author2);
Book book3 = new Book("The Hobbit", "George Allen & Unwin", author3);
Book book4 = new Book("Murder on the Orient Express", "Collins Crime Club",
author4);
Book book5 = new Book("The Da Vinci Code", "Doubleday", author5);
// Store books in a list
ArrayList<Book> books = new ArrayList<>();
books.add(book1);
books.add(book2);
books.add(book3);
books.add(book4);
books.add(book5);
// Write books to a file
saveBooksToFile(books);
// Read books back from the file and provide functionalities
ArrayList<Book> readBooks = readBooksFromFile();
if (readBooks != null) {
displayBooks(readBooks);
searchBookByName(readBooks);
}
}
// Write books to a file
private static void saveBooksToFile(ArrayList<Book> books) {
try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream(FILE_NAME))) {
oos.writeObject(books);
System.out.println("Books written to file successfully.");
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
// Read books from a file
private static ArrayList<Book> readBooksFromFile() {
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream(FILE_NAME))) {
return (ArrayList<Book>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
System.err.println("Error reading from file: " + e.getMessage());
}
return null;
}
// Display books
private static void displayBooks(ArrayList<Book> books) {
System.out.println("Books available in the store:");
for (Book book : books) {
System.out.println(book);
}
}
// Method to search for a book by name
private static void searchBookByName(ArrayList<Book> books) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the book to search: ");
String searchName = scanner.nextLine();
boolean found = false;
for (Book book : books) {
if (book.getName().equalsIgnoreCase(searchName)) {
System.out.println("Book found: " + book);
found = true;
break;
}
}
if (!found) {
System.out.println("Book not found in the records.");
}
}
}