Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Computer Science question Don't answer using chatgpt it's a totally wrong I will dislike you with my multiple uk friends Account guys so provide me

Computer Science question
Don't answer using chatgpt it's a totally wrong I will dislike you with my multiple uk friends Account guys so provide me correct solution for this question.no copy paste pls
Computer Science
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
class Book {
private String title;
private String author;
private String isbn;
private boolean isIssued;
public Book(String title, String author, String isbn){
this.title = title;
this.author = author;
this.isbn = isbn;
this.isIssued = false;
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public String getIsbn(){
return isbn;
}
public boolean isIssued(){
return isIssued;
}
public void issueBook(){
isIssued = true;
}
public void returnBook(){
isIssued = false;
}
}
class Member {
private String name;
private String memberId;
private ArrayList issuedBooks;
public Member(String name, String memberId){
this.name = name;
this.memberId = memberId;
this.issuedBooks = new ArrayList<>();
}
public String getName(){
return name;
}
public String getMemberId(){
return memberId;
}
public ArrayList getIssuedBooks(){
return issuedBooks;
}
public void issueBook(Book book){
if (!book.isIssued()){
book.issueBook();
issuedBooks.add(book);
System.out.println("Book issued successfully to "+ name);
} else {
System.out.println("Book is already issued.");
}
}
public void returnBook(Book book){
if (issuedBooks.contains(book)){
book.returnBook();
issuedBooks.remove(book);
System.out.println("Book returned successfully by "+ name);
} else {
System.out.println("This book is not issued to this member.");
}
}
}
class Transaction {
private Member member;
private Book book;
private Date issueDate;
private Date returnDate;
public Transaction(Member member, Book book, Date issueDate, Date returnDate){
this.member = member;
this.book = book;
this.issueDate = issueDate;
this.returnDate = returnDate;
}
public Member getMember(){
return member;
}
public Book getBook(){
return book;
}
public Date getIssueDate(){
return issueDate;
}
public Date getReturnDate(){
return returnDate;
}
}
public class LibraryManagementSystem {
private ArrayList books;
private ArrayList members;
private ArrayList transactions;
public LibraryManagementSystem(){
books = new ArrayList<>();
members = new ArrayList<>();
transactions = new ArrayList<>();
}
public void addBook(Book book){
books.add(book);
System.out.println("Book added successfully.");
}
public void addMember(Member member){
members.add(member);
System.out.println("Member added successfully.");
}
public Book findBookByIsbn(String isbn){
for (Book book : books){
if (book.getIsbn().equals(isbn)){
return book;
}
}
return null;
}
public Member findMemberById(String memberId){
for (Member member : members){
if (member.getMemberId().equals(memberId)){
return member;
}
}
return null;
}
public void issueBook(String memberId, String isbn){
Member member = findMemberById(memberId);
Book book = findBookByIsbn(isbn);
if (member != null && book != null){
member.issueBook(book);
transactions.add(new Transaction(member, book, new Date(), null));
} else {
System.out.println("Invalid member ID or book ISBN.");
}
}
public void returnBook(String memberId, String isbn){
Member member = findMemberById(memberId);
Book book = findBookByIsbn(isbn);
if (member != null && book != null){
member.returnBook(book);
for (Transaction transaction : transactions){
if (transaction.getMember().equals(member) && transaction.getBook().equals(book) && transaction.getReturnDate()== null){
transaction = new Transaction(member, book, transaction.getIssueDate(), new Date());
break;
}
}
} else {
System.out.println("Invalid member ID or book ISBN.");
}
}
public void showAllBooks(){
for (Book book : books){
System.out.println("Title: "+ book.getTitle()+", Author: "+ book.getAuthor()+", ISBN: "+ book.getIsbn()+", Issued: "+ book.isIssued());
}
}
public void showAllMembers(){
for (Member member : members){
System.out.println("Name: "+ member.getName()+", Member ID: "+ member.getMemberId());
}
}
public void showTransactions(){
for (Transaction transaction : transactions){
System.out.println("Member: "+ transaction.getMember().getName()+", Book: "+ transaction.getBook().getTitle()+", Issue Date: "+ transaction.getIssueDate()+", Return Date: "+(transaction.getReturnDate()!= null ? transaction.getReturnDate() : "Not Returned"));
}
}
public static void main(String[] args){
LibraryManagementSystem library = new LibraryManagementSystem();
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("
Library Management System");
System.out.println("1. Add Book");
System.out.println("2. Add Member");
System.out.println("3. Issue Book");
System.out.println("4. Return Book");
System.out.println("5. Show All Books");
System.out.println("6. Show All Members");
System.out.println("7. Show Transactions");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice){
case 1:
System.out.print("Enter book title: ");
scanner.nextLine();
String title = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
System.out.print("Enter book ISBN: ");
String isbn = scanner.nextLine();
library.addBook(new Book(title, author, isbn));
break;
case 2:
System.out.print("Enter member name: ");
scanner.nextLine();
String name = scanner.nextLine();
System.out.print("Enter member ID: ");
String memberId = scanner.nextLine();
library.addMember(new Member(name, memberId));
break;
case 3:
System.out.print("Enter member ID: ");
scanner.nextLine();
memberId = scanner.nextLine();
System.out.print("Enter book ISBN: ");
isbn = scanner.nextLine();
library.issueBook(memberId, isbn);
break;
case 4:
System.out.print("Enter member ID: ");
scanner.nextLine();
memberId = scanner.nextLine();
System.out.print("Enter book ISBN: ");
isbn = scanner.nextLine();
library.returnBook(memberId, isbn);
break;
case 5:
library.showAllBooks();
break;
case 6:
library.showAllMembers();
break;
case 7:
library.showTransactions();
break;
case 8:
System.out.println("Exiting the system.");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice !=8);
scanner.close();
}
}
Classes
Book Class
Attributes:
title: The title of the book.
author: The author of the book.
isbn: The International Standard Book Number of the book.
isIssued: A boolean indicating whether the book is issued.
Methods:
getTitle(), getAuthor(), getIsbn(), isIssued(): Getters for the attributes.
issueBook(): Sets isIssued to true.
returnBook(): Sets isIssued to false.
Member Class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions