Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library Management System #26

Open
NandiniGanvir opened this issue Mar 3, 2024 · 0 comments
Open

Library Management System #26

NandiniGanvir opened this issue Mar 3, 2024 · 0 comments

Comments

@NandiniGanvir
Copy link

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");
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant