Library Management System
unknown
java
a month ago
3.4 kB
3
Indexable
Never
// Interface for basic library operations interface LibraryOperations { void addBook(Book book); void removeBook(String bookId); void lendBook(String bookId, User user); void returnBook(String bookId, User user); } // Interface for user-related operations interface UserOperations { void registerUser(User user); void unregisterUser(String userId); } // Interface for book-related operations interface BookOperations { String getBookDetails(); } // Book class implementing BookOperations class Book implements BookOperations { private String id; private String title; private String author; private boolean isAvailable; public Book(String id, String title, String author) { this.id = id; this.title = title; this.author = author; this.isAvailable = true; } @Override public String getBookDetails() { return "ID: " + id + ", Title: " + title + ", Author: " + author + ", Available: " + isAvailable; } public String getId() { return id; } public boolean isAvailable() { return isAvailable; } public void setAvailable(boolean available) { isAvailable = available; } } // User class implementing UserOperations class User implements UserOperations { private String id; private String name; public User(String id, String name) { this.id = id; this.name = name; } @Override public void registerUser(User user) { // Implementation for registering user } @Override public void unregisterUser(String userId) { // Implementation for unregistering user } public String getId() { return id; } public String getName() { return name; } } // Library class implementing LibraryOperations class Library implements LibraryOperations { private Map<String, Book> books = new HashMap<>(); private Map<String, User> users = new HashMap<>(); @Override public void addBook(Book book) { books.put(book.getId(), book); } @Override public void removeBook(String bookId) { books.remove(bookId); } @Override public void lendBook(String bookId, User user) { Book book = books.get(bookId); if (book != null && book.isAvailable()) { book.setAvailable(false); System.out.println("Book lent to " + user.getName()); } else { System.out.println("Book is not available."); } } @Override public void returnBook(String bookId, User user) { Book book = books.get(bookId); if (book != null && !book.isAvailable()) { book.setAvailable(true); System.out.println("Book returned by " + user.getName()); } else { System.out.println("Book was not lent out."); } } } // Example usage public class LibraryManagementSystem { public static void main(String[] args) { Library library = new Library(); User user1 = new User("1", "Alice"); User user2 = new User("2", "Bob"); Book book1 = new Book("101", "Java Programming", "John Doe"); Book book2 = new Book("102", "Design Patterns", "Jane Doe"); library.addBook(book1); library.addBook(book2); library.lendBook("101", user1); library.returnBook("101", user1); } }
Leave a Comment