BÀI TOÁN QUẢN LÝ THƯ VIỆN
BÀI TOÁN QUẢN LÝ THƯ VIỆN
BÀI TOÁN QUẢN LÝ THƯ VIỆN
import java.util.Objects;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author ducbao
*/
public Account() {
}
public Account(String accountID, String accountType, String fullName, String date, String phoneNumber, String
email, String address, String status) {
this.accountID = accountID;
this.accountType = accountType;
this.fullName = fullName;
this.date = date;
this.phoneNumber = phoneNumber;
this.email = email;
this.address = address;
this.status = status;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Account other = (Account) obj;
return Objects.equals(this.accountID, other.accountID);
}
}
2. Đối tượng Book (Book.java)
import java.util.Objects;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author ducbao
*/
public class Book {
String ISBN, title, authors, year, publisher, status;
public Book() {
}
public Book(String ISBN, String title, String authors, String year, String publisher, String status) {
this.ISBN = ISBN;
this.title = title;
this.authors = authors;
this.year = year;
this.publisher = publisher;
this.status = status;
}
@Override
public String toString() { //Format chỉnh theo cá nhân
return ISBN + " " + title + " " + authors + " " + year + " " + publisher + " " + status;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Book other = (Book) obj;
return Objects.equals(this.ISBN, other.ISBN);
}
}
3. Đối tượng BookRequest (BookRequest.java)
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author ducbao
*/
public class BookRequest{
String blCode, memberID, ISBN, dueDate, returnDate;
public BookRequest() {
}
public BookRequest(String blCode, String memberID, String ISBN, String dueDate, String returnDate) {
this.blCode = blCode;
this.memberID = memberID;
this.ISBN = ISBN;
this.dueDate = dueDate;
this.returnDate = returnDate;
}
@Override
public String toString() { //Format chỉnh theo cá nhân
return blCode + " " + memberID + " " + ISBN + " " + dueDate + " " + returnDate;
}
}
4. Đối tượng Librarian (Librarian.java)
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author ducbao
*/
public class Librarian extends Account {
public Librarian() {
}
public Librarian(String accountID, String accountType, String fullName, String date, String phoneNumber, String
email, String address, String status, String startingDate, String martitalStatus) {
super(accountID, accountType, fullName, date, phoneNumber, email, address, status);
this.startingDate = startingDate;
this.martitalStatus = martitalStatus;
}
@Override
public String toString() { //Format chỉnh theo cá nhân
return accountID + " " + accountType + " " + fullName + " " + date + " " + phoneNumber + " " + email + " " +
address + " " + status + " " + startingDate + " " + martitalStatus;
}
}
5. Đối tượng Member (Member.java)
import java.util.Objects;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author ducbao
*/
public class Member extends Account{
String memberShipDate;
int nBook;
public Member() {
}
public Member(String memberShipDate, int nBook, String accountID, String accountType, String fullName,
String date, String phoneNumber, String email, String address, String status) {
super(accountID, accountType, fullName, date, phoneNumber, email, address, status);
this.memberShipDate = memberShipDate;
this.nBook = nBook;
}
@Override
public String toString() { //Format chỉnh theo cá nhân
return accountID + " " + accountType + " " + fullName + " " + date + " " + phoneNumber + " " + email + " " +
address + " " + status + " " + memberShipDate + " " + nBook;
}
}
6. Lớp Node (Node.java)
import java.util.Objects;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author ducbao
*/
public class Node<T> {
private T info;
private Node<T> next;
public Node() {
}
public T getInfo() {
return info;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Node<T> other = (Node<T>) obj;
return true;
}
}
7. Lớp MyList (MyList.java)
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author ducbao
* @param <T> the type of the value
*/
public class MyList<T> {
}
}
list.insertTail((T) newMember);
}
flr.close();
blr.close();
} catch (Exception e) {
}
return list;
}
/*Đọc file chứa dữ liệu của Librarian*/
public MyList<T> readFileLibrarian(String fileName, MyList<T> list) {
try {
FileReader flr = new FileReader(fileName);
BufferedReader blr = new BufferedReader(flr);
String line = "";
while (true) {
line = blr.readLine(); //Đọc dữ liệu file theo từng hàng
if (line == null) { //Hết dữ liệu DỪNG VÒNG LẶP (không phải dừng phương thức)
break;
}
String txt[] = line.split(" "); //Tách dòng đã đọc theo kí tự khoảng trắng
String id = txt[0]; //Mỗi phần tử được tách = một dữ liệu của từng phần tử trong danh sách
String type = txt[1];
String name = txt[2];
String date = txt[3];
String phone = txt[4];
String mail = txt[5];
String address = txt[6];
String status = txt[7];
String startingDate = txt[8];
String martitalStatus = txt[9];
/*
* Tạo 1 đối tượng mới cho đối tượng Librarian
* Thêm đối tượng mới vào danh sách CỦA ĐỐI TƯỢNG LIBRARIAN
*/
Librarian newLibrarian = new Librarian(id, type, name, date, phone, mail, address, status, startingDate,
martitalStatus);
list.insertTail((T) newLibrarian);
}
flr.close();
blr.close();
} catch (Exception e) {
}
return list;
}
}
flr.close();
blr.close();
} catch (Exception e) {
}
return list;
}
return false;
}
}
}
8. Lớp Main (Main.java)
import java.util.Scanner;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
/**
*
* @author ducbao
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
/*Đăng nhập*/
System.out.println("Log in!");
if (!check) {
librarianMenu();
System.out.print("Choose: ");
int choice = sc.nextInt();
switch (choice) {
//Librarian search book for title or id
case 1 -> {
System.out.println("Input book's title or id ");
String data = sc.next();
var result = bookList.search(data).getInfo();
if (result != null) {
System.out.println(result);
} else {
System.out.println("Couldn't find or be borrowed");
}
}
case 2 -> {
System.out.println("Input member's name or id ");
String data = sc.next();
var result = memberList.search(data).getInfo();
if (result != null) {
System.out.println(result);
} else {
System.out.println("Couldn't find");
}
}
case 3 -> {
System.out.println("Input member's id");
String data = sc.next();
bookRequestList.searchMember(data);
}
default ->
throw new AssertionError();
}
} else if (check) {
boolean checkAns = true;
while (checkAns) {
memberMenu();
System.out.print("Choose: ");
int choice = sc.nextInt();
switch (choice) {
case 1 -> {
System.out.println("Input book's title or id ");
String data = sc.next();
var result = bookList.search(data).getInfo();
if (result != null) {
System.out.println(result);
} else {
System.out.println("Couldn't find or be borrowed");
}
System.out.println("Want to borrow book or find more book ?");
System.out.println("1/ Yes");
System.out.println("2/ No");
int ans = sc.nextInt();
switch (ans) {
case 1 ->
checkAns = true;
case 2 ->
checkAns = false;
default ->
throw new AssertionError();
}
}
case 2 -> {
boolean checkBook = false;
String ISBNchecked = "";
while (!checkBook) {
System.out.print("Input the ISBN: ");
String ISBN = sc.next();
if (bookRequestList.compare(ISBN)) {
Node<Book> node = bookList.search(ISBN);
node.getInfo().setStatus("Loaned");
checkBook = false;
System.out.println("Book loaned");
} else {
checkBook = true;
ISBNchecked = ISBN;
}
}
bookRequestList.borrowBook(bookRequestList, bookList, ISBNchecked);
bookList.showList();
System.out.println("Want to borrow book or find more book ?");
System.out.println("1/ Yes");
System.out.println("2/ No");
int ans = sc.nextInt();
switch (ans) {
case 1 ->
checkAns = true;
case 2 ->
checkAns = false;
}
}
default ->
throw new AssertionError();
}
}
}
memberList.writeFile(fileMember, memberList);
librarianList.writeFile(fileLibrarian, librarianList);
bookList.writeFile(fileBook, bookList);
bookRequestList.writeFile(fileBookRequest, bookRequestList);
}
}