Open NandiniGanvir opened 8 months ago
import java.util.ArrayList; import java.util.HashMap; import java.util.Map;
class Book { private String title; private String author; private String genre; private boolean available;
public Book(String title, String author, String genre) { this.title = title; this.author = author; this.genre = genre; this.available = true; } public String getTitle() { return title; } public String getAuthor() { return author; } public String getGenre() { return genre; } public boolean isAvailable() { return available; } public void setAvailable(boolean available) { this.available = available; }
}
class Patron { private String name; private String contactInfo; private ArrayList borrowedBooks;
public Patron(String name, String contactInfo) { this.name = name; this.contactInfo = contactInfo; this.borrowedBooks = new ArrayList<>(); } public String getName() { return name; } public String getContactInfo() { return contactInfo; } public ArrayList<Book> getBorrowedBooks() { return borrowedBooks; } public void borrowBook(Book book) { borrowedBooks.add(book); book.setAvailable(false); } public void returnBook(Book book) { borrowedBooks.remove(book); book.setAvailable(true); }
public class LibraryManagementSystem { private HashMap<String, Book> books; private HashMap<String, Patron> patrons; private HashMap<Book, Patron> borrowedBooks;
public LibraryManagementSystem() { this.books = new HashMap<>(); this.patrons = new HashMap<>(); this.borrowedBooks = new HashMap<>(); } public void addBook(String title, String author, String genre) { Book book = new Book(title, author, genre); books.put(title, book); } public void addPatron(String name, String contactInfo) { Patron patron = new Patron(name, contactInfo); patrons.put(name, patron); } public void borrowBook(String patronName, String bookTitle) { Patron patron = patrons.get(patronName); Book book = books.get(bookTitle); if (patron != null && book != null && book.isAvailable()) { patron.borrowBook(book); borrowedBooks.put(book, patron); System.out.println("Book \"" + bookTitle + "\" borrowed by " + patronName); } else { System.out.println("Book \"" + bookTitle + "\" is not available for borrowing or patron \"" + patronName + "\" does not exist."); } } public void returnBook(String patronName, String bookTitle) { Patron patron = patrons.get(patronName); Book book = books.get(bookTitle); if (patron != null && book != null && !book.isAvailable() && patron.getBorrowedBooks().contains(book)) { patron.returnBook(book); borrowedBooks.remove(book); System.out.println("Book \"" + bookTitle + "\" returned by " + patronName); } else { System.out.println("Book \"" + bookTitle + "\" was not borrowed by patron \"" + patronName + "\"."); } } public static void main(String[] args) { LibraryManagementSystem library = new LibraryManagementSystem(); // Add books library.addBook("Book1", "Author1", "Genre1"); library.addBook("Book2", "Author2", "Genre2"); // Add patrons library.addPatron("Patron1", "ContactInfo1"); library.addPatron("Patron2", "ContactInfo2"); // Patron1 borrows Book1 library.borrowBook("Patron1", "Book1"); // Patron2 tries to borrow Book1 (should fail) library.borrowBook("Patron2", "Book1"); // Patron1 returns Book1 library.returnBook("Patron1", "Book1"); // Patron2 borrows Book1 library.borrowBook("Patron2", "Book1"); }
import java.util.ArrayList; import java.util.HashMap; import java.util.Map;
class Book { private String title; private String author; private String genre; private boolean available;
}
class Patron { private String name; private String contactInfo; private ArrayList borrowedBooks;
}
public class LibraryManagementSystem { private HashMap<String, Book> books; private HashMap<String, Patron> patrons; private HashMap<Book, Patron> borrowedBooks;
}