0% found this document useful (0 votes)
5 views

java_print

Uploaded by

megon19007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

java_print

Uploaded by

megon19007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Section 2 and 3

Main Class:
package com.library;

import java.util.Date;
import java.util.Scanner;

public class Main {


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

// Add some books to the library's inventory


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

// Add some members to the library


library.addMember(new Member("M001", "Alice Johnson", "[email protected]"));
library.addMember(new Member("M002", "Bob Smith", "[email protected]"));
library.addMember(new Member("M003", "Charlie Brown", "[email protected]"));
library.addMember(new Member("M004", "Diana Prince", "[email protected]"));
library.addMember(new Member("M005", "Edward Norton", "[email protected]"));
library.addMember(new Member("M006", "Fiona Green", "[email protected]"));

// Creating an EBook
EBook eBook = new EBook("B004", "Digital Fortress", "Dan Brown", "Thriller", 5.5,
"PDF");
eBook.displayInfo();
eBook.download();
System.out.println();

// Creating an AudioBook
AudioBook audioBook = new AudioBook("B005", "Becoming", "Michelle Obama",
"Biography", 19.0, "Michelle Obama");
audioBook.displayInfo();
audioBook.playSample();

// Main loop for user interaction


while (true) {
// Show the options to the user
System.out.println("\nChoose an option (Provide the option Number):");
System.out.println("1. Borrow a Book");
System.out.println("2. Return a Book");
System.out.println("3. View Transactions");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();

// Process the user's choice


switch (choice) {
case 1:
// Option to borrow a book
System.out.println("\nMembers:");
for (Member member : library.getMembers()) {
System.out.println("Name: " + member.getName());
}
System.out.print("\nEnter member name: ");
String memberNameBorrow = scanner.nextLine();

System.out.println("\nBooks:");
for (Book book : library.getBooks()) {
System.out.println("ID: " + book.getBookID() + ", Title: " +
book.getTitle());
}
System.out.print("\nEnter book title: ");
String bookTitleBorrow = scanner.nextLine();

Member memberBorrow = library.getMemberByName(memberNameBorrow);


if (memberBorrow != null) {
library.borrowBook(memberBorrow, bookTitleBorrow);
} else {
System.out.println("\nMember not found.");
}
break;

case 2:
// Option to return a book
System.out.println("\nMembers:");
for (Member member : library.getMembers()) {
System.out.println("Name: " + member.getName());
}
System.out.print("\nEnter member name: ");
String memberNameReturn = scanner.nextLine();

System.out.println("\nBooks:");
for (Book book : library.getBooks()) {
System.out.println("ID: " + book.getBookID() + ", Title: " +
book.getTitle());
}
System.out.print("\nEnter book title: ");
String bookTitleReturn = scanner.nextLine();

Member memberReturn = library.getMemberByName(memberNameReturn);


if (memberReturn != null) {
library.returnBook(memberReturn, bookTitleReturn);
} else {
System.out.println("\nMember not found.");
}
break;

case 3:
// Option to view transactions
System.out.println("\nTransactions:");
library.displayTransactions();
break;

case 4:
// Exit the application
System.out.println("\nExiting...");
scanner.close();
return;

default:
System.out.println("\nInvalid option. Please try again.");
}
}
}
}

Library class:
package com.library;

import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Library {


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

public Library() {
this.bookInventory = new ArrayList<>();
this.members = new HashMap<>();
this.transactions = new ArrayList<>();
}

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


public void addBook(Book book) {
bookInventory.add(book);
System.out.println("New Book added: " + book.getTitle());
}

// Remove a book from the library's inventory


public void removeBook(Book book) {
if (bookInventory.remove(book)) {
System.out.println("Book removed: " + book.getTitle());
} else {
System.out.println("This Book does not exist.");
}
}
// Add a new member to the library
public void addMember(Member member) {
members.put(member.getMemberID(), member);
System.out.println("Member added: " + member.getName());
}

// Remove a member from the library


public void removeMember(Member member) {
if (members.remove(member.getMemberID()) != null) {
System.out.println("Member removed: " + member.getName());
} else {
System.out.println("Member not found.");
}
}

// Search for a book by its title


public Book searchBookByTitle(String title) {
for (Book book : bookInventory) {
if (book.getTitle().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 && book.isAvailable()) {
member.borrowBook(book);
Transaction transaction = new Transaction("T" + (transactions.size() + 1),
book, member, new Date());
transactions.add(transaction);
System.out.println(member.getName() + " borrowed " + book.getTitle());
} else {
System.out.println("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) {
member.returnBook(book);
Transaction transaction = findTransaction(book, member);
if (transaction != null) {
transaction.returnBook(new Date());
System.out.println(member.getName() + " returned " + book.getTitle());
}
} else {
System.out.println("Book not found in inventory.");
}
}

// Get a member by their ID


public Member getMemberById(String memberId) {
return members.get(memberId);
}

// Display all transactions


public void displayTransactions() {
for (Transaction transaction : transactions) {
transaction.logTransaction();
}
}

// Find a transaction based on book and member


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

// Get a list of all members


public List<Member> getMembers() {
return new ArrayList<>(members.values());
}

// 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 : members.values()) {
if (member.getName().equalsIgnoreCase(name)) {
return member;
}
}
return null;
}
}
Book class:

package com.library;

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) {
this.bookID = bookID;
this.title = title;
this.author = author;
this.category = category;
this.isAvailable = 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() {


System.out.println("Book ID: " + bookID);
System.out.println("Title : " + title);
System.out.println("Author: " + author);
System.out.println("Category: " + category);
System.out.println("Available: " + isAvailable);
}
}
Audio Book class:

package com.library;

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
this.duration = duration;
this.narrator = narrator;
}

// get the file size


public double getDuration() {
return duration;
}

// change the duration


public void setDuration(double duration) {
this.duration = duration;
}

// get the Narrator


public String getNarrator() {
return narrator;
}

// change the Narrator


public void setNarrator(String narrator) {
this.narrator = narrator;
}

// Overridden Method
@Override
public void displayInfo() {
super.displayInfo(); // Call superclass method
System.out.println("Duration : " + duration + " hours");
System.out.println("Narrator : " + narrator);
}

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

package com.library;

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
this.fileSize = fileSize;
this.format = format;
}

// get the file size


public double getFileSize() {
return fileSize;
}

// change the file size


public void setFileSize(double fileSize) {
this.fileSize = fileSize;
}

// get the format


public String getFormat() {
return format;
}

// change the format


public void setFormat(String format) {
this.format = format;
}

// Overridden Method
@Override
public void displayInfo() {
super.displayInfo(); // Call superclass method
System.out.println("File Size: " + fileSize + " MB");
System.out.println("Format : " + format);
}

// Additional Methods
public void download() {
System.out.println("Downloading " + getTitle() + " in " + format + " format.");
}
}
Member class:

package com.library;

import java.util.ArrayList;
import java.util.List;

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) {
this.memberID = memberID;
this.name = name;
this.email = email;
this.borrowedBooks = 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) {
borrowedBooks.add(book);
book.setAvailable(false);
}

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


public void returnBook(Book book) {
borrowedBooks.remove(book);
book.setAvailable(true);
}
}

Transaction class:

package com.library;

import java.text.SimpleDateFormat;
import java.util.Date;
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) {
this.transactionID = transactionID;
this.book = book;
this.member = member;
this.borrowDate = borrowDate;
this.returnDate = null;
}

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


public void returnBook(Date returnDate) {
this.returnDate = returnDate;
}

// Log the transaction details


public void logTransaction() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Transaction ID: " + transactionID);
System.out.println("Book: " + book.getTitle());
System.out.println("Member: " + member.getName());
System.out.println("Borrow Date: " + dateFormat.format(borrowDate));
if (returnDate != null) {
System.out.println("Return Date: " + dateFormat.format(returnDate));
} else {
System.out.println("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