Programing Assignment Sample
Q1:
Answer :Solution Approach:
To design an object-oriented application for the library system, we will implement the following classes:
Book - Represents a book in the library.
Library - Manages the collection of books and operations.
User - Represents users borrowing books.
LibrarySystem - The main class to interact with the system.
Implementation Details:
1. Book Class
This class represents a book in the library with attributes for title, author, and availability status.
public class Book {
private String title;
private String author;
private boolean isAvailable;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.isAvailable = true;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isAvailable() {
return isAvailable;
}
public void borrowBook() {
isAvailable = false;
}
public void returnBook() {
isAvailable = true;
}
}
2. User Class
This class represents a user who borrows books from the library.
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
3. Library Class
This class maintains a collection of books and implements methods for adding, borrowing, and returning books.
import java.util.ArrayList;
public class Library {
private ArrayList<Book> books;
private ArrayList<String> borrowedBooks;
public Library() {
this.books = new ArrayList<>();
this.borrowedBooks = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void borrowBook(String title, User user) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title) && book.isAvailable()) {
book.borrowBook();
borrowedBooks.add(user.getName() + " borrowed " + book.getTitle());
System.out.println(user.getName() + " has borrowed " + title);
return;
}
}
System.out.println("Book is not available or does not exist.");
}
public void returnBook(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title) && !book.isAvailable()) {
book.returnBook();
borrowedBooks.removeIf(entry -> entry.contains(title));
System.out.println("Book returned: " + title);
return;
}
}
System.out.println("Book is not borrowed.");
}
public void displayAvailableBooks() {
System.out.println("Available Books:");
for (Book book : books) {
if (book.isAvailable()) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
}
}
public void displayBorrowedBooks() {
System.out.println("Borrowed Books:");
for (String entry : borrowedBooks) {
System.out.println(entry);
}
}
}
4. LibrarySystem (Main Class)
This class provides a user interface to interact with the library.
import java.util.Scanner;
public class LibrarySystem {
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
// Adding sample books
library.addBook(new Book("Java Programming", "James Gosling"));
library.addBook(new Book("Clean Code", "Robert C. Martin"));
library.addBook(new Book("Design Patterns", "Erich Gamma"));
while (true) {
System.out.println("\nLibrary System Menu:");
System.out.println("1. Display Available Books");
System.out.println("2. Borrow Book");
System.out.println("3. Return Book");
System.out.println("4. Display Borrowed Books");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
library.displayAvailableBooks();
break;
case 2:
System.out.print("Enter your name: ");
String userName = scanner.nextLine();
User user = new User(userName);
System.out.print("Enter book title to borrow: ");
String borrowTitle = scanner.nextLine();
library.borrowBook(borrowTitle, user);
break;
case 3:
System.out.print("Enter book title to return: ");
String returnTitle = scanner.nextLine();
library.returnBook(returnTitle);
break;
case 4:
library.displayBorrowedBooks();
break;
case 5:
System.out.println("Exiting... Thank you!");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
Explanation:
Encapsulation: The Book
, User
, and Library
classes encapsulate their properties and expose only necessary methods.
Inheritance: Although not needed in this simple example, we could extend Book
for different types of books.
Polymorphism: Methods like borrowBook
and returnBook
can be extended or overridden in future enhancements.
Error Handling: The program prevents borrowing unavailable books and checks for incorrect inputs.
Conclusion:
This object-oriented design ensures scalability, maintainability, and reusability. Future improvements could include database integration and a GUI interface.