0% found this document useful (0 votes)
11 views11 pages

Java Library Management System

Uploaded by

megon19007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Java Library Management System

Uploaded by

megon19007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Section 2 and 3

Main Class:
package [Link];

import [Link];
import [Link];

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Library library = new Library();

// Add some books to the library's inventory


[Link](new Book("ID-001", "Brave New World", "Aldous Huxley", "Dystopian
Fiction"));
[Link](new Book("ID-003", "To Kill a Mockingbird", "Harper Lee", "Classic
Fiction"));
[Link](new Book("ID-004", "1984", "George Orwell", "Dystopian Fiction"));
[Link](new Book("ID-005", "Pride and Prejudice", "Jane Austen", "Classic
Fiction"));
[Link](new Book("ID-006", "The Catcher in the Rye", "J.D. Salinger",
"Fiction"));
[Link](new Book("ID-007", "The Great Gatsby", "F. Scott Fitzgerald",
"Classic Fiction"));
[Link](new Book("ID-008", "Moby Dick", "Herman Melville", "Adventure
Fiction"));
[Link](new Book("ID-009", "The Hobbit", "J.R.R. Tolkien", "Fantasy
Fiction"));
[Link](new Book("ID-010", "The Da Vinci Code", "Dan Brown", "Mystery
Fiction"));

// Add some members to the library


[Link](new Member("M001", "Alice Johnson", "alice@[Link]"));
[Link](new Member("M002", "Bob Smith", "bob@[Link]"));
[Link](new Member("M003", "Charlie Brown", "charlie@[Link]"));
[Link](new Member("M004", "Diana Prince", "diana@[Link]"));
[Link](new Member("M005", "Edward Norton", "edward@[Link]"));
[Link](new Member("M006", "Fiona Green", "fiona@[Link]"));

// Creating an EBook
EBook eBook = new EBook("B004", "Digital Fortress", "Dan Brown", "Thriller", 5.5,
"PDF");
[Link]();
[Link]();
[Link]();

// Creating an AudioBook
AudioBook audioBook = new AudioBook("B005", "Becoming", "Michelle Obama",
"Biography", 19.0, "Michelle Obama");
[Link]();
[Link]();

// Main loop for user interaction


while (true) {
// Show the options to the user
[Link]("\nChoose an option (Provide the option Number):");
[Link]("1. Borrow a Book");
[Link]("2. Return a Book");
[Link]("3. View Transactions");
[Link]("4. Exit");
[Link]("Choose an option: ");
int choice = [Link]();
[Link]();

// Process the user's choice


switch (choice) {
case 1:
// Option to borrow a book
[Link]("\nMembers:");
for (Member member : [Link]()) {
[Link]("Name: " + [Link]());
}
[Link]("\nEnter member name: ");
String memberNameBorrow = [Link]();

[Link]("\nBooks:");
for (Book book : [Link]()) {
[Link]("ID: " + [Link]() + ", Title: " +
[Link]());
}
[Link]("\nEnter book title: ");
String bookTitleBorrow = [Link]();

Member memberBorrow = [Link](memberNameBorrow);


if (memberBorrow != null) {
[Link](memberBorrow, bookTitleBorrow);
} else {
[Link]("\nMember not found.");
}
break;

case 2:
// Option to return a book
[Link]("\nMembers:");
for (Member member : [Link]()) {
[Link]("Name: " + [Link]());
}
[Link]("\nEnter member name: ");
String memberNameReturn = [Link]();

[Link]("\nBooks:");
for (Book book : [Link]()) {
[Link]("ID: " + [Link]() + ", Title: " +
[Link]());
}
[Link]("\nEnter book title: ");
String bookTitleReturn = [Link]();

Member memberReturn = [Link](memberNameReturn);


if (memberReturn != null) {
[Link](memberReturn, bookTitleReturn);
} else {
[Link]("\nMember not found.");
}
break;

case 3:
// Option to view transactions
[Link]("\nTransactions:");
[Link]();
break;

case 4:
// Exit the application
[Link]("\nExiting...");
[Link]();
return;

default:
[Link]("\nInvalid option. Please try again.");
}
}
}
}

Library class:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class Library {


private ArrayList<Book> bookInventory;
private HashMap<String, Member> members;
private List<Transaction> transactions;

public Library() {
[Link] = new ArrayList<>();
[Link] = new HashMap<>();
[Link] = new ArrayList<>();
}

// Add a new book to the library's inventory


public void addBook(Book book) {
[Link](book);
[Link]("New Book added: " + [Link]());
}

// Remove a book from the library's inventory


public void removeBook(Book book) {
if ([Link](book)) {
[Link]("Book removed: " + [Link]());
} else {
[Link]("This Book does not exist.");
}
}
// Add a new member to the library
public void addMember(Member member) {
[Link]([Link](), member);
[Link]("Member added: " + [Link]());
}

// Remove a member from the library


public void removeMember(Member member) {
if ([Link]([Link]()) != null) {
[Link]("Member removed: " + [Link]());
} else {
[Link]("Member not found.");
}
}

// Search for a book by its title


public Book searchBookByTitle(String title) {
for (Book book : bookInventory) {
if ([Link]().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}

// Process borrowing of a book by a member


public void borrowBook(Member member, String bookTitle) {
Book book = searchBookByTitle(bookTitle);
if (book != null && [Link]()) {
[Link](book);
Transaction transaction = new Transaction("T" + ([Link]() + 1),
book, member, new Date());
[Link](transaction);
[Link]([Link]() + " borrowed " + [Link]());
} else {
[Link]("Book is not available.");
}
}

// Process returning of a book by a member


public void returnBook(Member member, String bookTitle) {
Book book = searchBookByTitle(bookTitle);
if (book != null) {
[Link](book);
Transaction transaction = findTransaction(book, member);
if (transaction != null) {
[Link](new Date());
[Link]([Link]() + " returned " + [Link]());
}
} else {
[Link]("Book not found in inventory.");
}
}

// Get a member by their ID


public Member getMemberById(String memberId) {
return [Link](memberId);
}

// Display all transactions


public void displayTransactions() {
for (Transaction transaction : transactions) {
[Link]();
}
}

// Find a transaction based on book and member


private Transaction findTransaction(Book book, Member member) {
for (Transaction transaction : transactions) {
if ([Link]().equals(book) &&
[Link]().equals(member) && [Link]() == null) {
return transaction;
}
}
return null;
}

// Get a list of all members


public List<Member> getMembers() {
return new ArrayList<>([Link]());
}

// Get a list of all books


public List<Book> getBooks() {
return new ArrayList<>(bookInventory);
}

// Get a member by their name


public Member getMemberByName(String name) {
for (Member member : [Link]()) {
if ([Link]().equalsIgnoreCase(name)) {
return member;
}
}
return null;
}
}
Book class:

package [Link];

public class Book {


private String bookID;
private String title;
private String author;
private String category;
private boolean isAvailable;

// Constructor for Book class


public Book(String bookID, String title, String author, String category) {
[Link] = bookID;
[Link] = title;
[Link] = author;
[Link] = category;
[Link] = true;
}

// Getters and Setters


public String getBookID() {
return bookID;
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}

public String getCategory() {


return category;
}

public boolean isAvailable() {


return isAvailable;
}

public void setAvailable(boolean available) {


isAvailable = available;
}

public void displayInfo() {


[Link]("Book ID: " + bookID);
[Link]("Title : " + title);
[Link]("Author: " + author);
[Link]("Category: " + category);
[Link]("Available: " + isAvailable);
}
}
Audio Book class:

package [Link];

public class AudioBook extends Book {


// class variabls
private double duration; // in hours
private String narrator;

// AudioBook constructor
public AudioBook(String bookID, String title, String author, String category, double
duration, String narrator) {
super(bookID, title, author, category); // Call to superclass constructor
[Link] = duration;
[Link] = narrator;
}

// get the file size


public double getDuration() {
return duration;
}

// change the duration


public void setDuration(double duration) {
[Link] = duration;
}

// get the Narrator


public String getNarrator() {
return narrator;
}

// change the Narrator


public void setNarrator(String narrator) {
[Link] = narrator;
}

// Overridden Method
@Override
public void displayInfo() {
[Link](); // Call superclass method
[Link]("Duration : " + duration + " hours");
[Link]("Narrator : " + narrator);
}

// Additional Methods
public void playSample() {
[Link]("Playing a sample of " + getTitle() + " narrated by " + narrator
+ ".");
}
}
Ebook class:

package [Link];

public class EBook extends Book {


// class variabls
private double fileSize; // in MB
private String format; // e.g., PDF, EPUB

// EBook Constructor
public EBook(String bookID, String title, String author, String category, double
fileSize, String format) {
super(bookID, title, author, category); // Call to superclass constructor
[Link] = fileSize;
[Link] = format;
}

// get the file size


public double getFileSize() {
return fileSize;
}

// change the file size


public void setFileSize(double fileSize) {
[Link] = fileSize;
}

// get the format


public String getFormat() {
return format;
}

// change the format


public void setFormat(String format) {
[Link] = format;
}

// Overridden Method
@Override
public void displayInfo() {
[Link](); // Call superclass method
[Link]("File Size: " + fileSize + " MB");
[Link]("Format : " + format);
}

// Additional Methods
public void download() {
[Link]("Downloading " + getTitle() + " in " + format + " format.");
}
}
Member class:

package [Link];

import [Link];
import [Link];

public class Member {


private String memberID;
private String name;
private String email;
private List<Book> borrowedBooks;

// Constructor for Member class


public Member(String memberID, String name, String email) {
[Link] = memberID;
[Link] = name;
[Link] = email;
[Link] = new ArrayList<>();
}

// Getters and Setters


public String getMemberID() {
return memberID;
}

public String getName() {


return name;
}

public String getEmail() {


return email;
}

// Add a borrowed book to the member's list


public void borrowBook(Book book) {
[Link](book);
[Link](false);
}

// Remove a returned book from the member's list


public void returnBook(Book book) {
[Link](book);
[Link](true);
}
}

Transaction class:

package [Link];

import [Link];
import [Link];
public class Transaction {
private String transactionID;
private Book book;
private Member member;
private Date borrowDate;
private Date returnDate;

// Constructor for Transaction class


public Transaction(String transactionID, Book book, Member member, Date borrowDate) {
[Link] = transactionID;
[Link] = book;
[Link] = member;
[Link] = borrowDate;
[Link] = null;
}

// Mark the book as returned and set the return date


public void returnBook(Date returnDate) {
[Link] = returnDate;
}

// Log the transaction details


public void logTransaction() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
[Link]("Transaction ID: " + transactionID);
[Link]("Book: " + [Link]());
[Link]("Member: " + [Link]());
[Link]("Borrow Date: " + [Link](borrowDate));
if (returnDate != null) {
[Link]("Return Date: " + [Link](returnDate));
} else {
[Link]("Book is still borrowed.");
}
}

// Getters
public String getTransactionID() {
return transactionID;
}

public Book getBook() {


return book;
}

public Member getMember() {


return member;
}

public Date getBorrowDate() {


return borrowDate;
}

public Date getReturnDate() {


return returnDate;
}
}
Out Put:

Choose an option (Provide the option Number):


1. Borrow a Book
2. Return a Book
3. View Transactions
4. Exit
Choose an option: 1

Members:
Name: Diana Prince
Name: Charlie Brown
Name: Bob Smith
Name: Alice Johnson
Name: Fiona Green
Name: Edward Norton

Enter member name: Diana Prince

Books:
ID: ID-001, Title: Brave New World
ID: ID-003, Title: To Kill a Mockingbird
ID: ID-004, Title: 1984
ID: ID-005, Title: Pride and Prejudice
ID: ID-006, Title: The Catcher in the Rye
ID: ID-007, Title: The Great Gatsby
ID: ID-008, Title: Moby Dick
ID: ID-009, Title: The Hobbit
ID: ID-010, Title: The Da Vinci Code

Enter book title: To Kill a Mockingbird


Diana Prince borrowed To Kill a Mockingbird

Choose an option (Provide the option Number):


1. Borrow a Book
2. Return a Book
3. View Transactions
4. Exit
Choose an option: 3

Transactions:
Transaction ID: T1
Book: To Kill a Mockingbird
Member: Diana Prince
Borrow Date: 2024-09-20
Book is still borrowed.

Choose an option (Provide the option Number):


1. Borrow a Book
2. Return a Book
3. View Transactions
4. Exit
Choose an option:

You might also like