lab5
unknown
java
a year ago
12 kB
19
Indexable
// BOOK
public class Book {
// Các thuộc tính private của lớp Book
private String ID; // Mã định danh của sách
private String name; // Tên sách
private int year; // Năm xuất bản
private String author; // Tác giả
private int size; // Kích thước sách (đơn vị: KB)
public Book() {
}
public Book(String ID, String name, int year, String author, int size) {
this.ID = ID;
this.name = name;
this.year = year;
this.author = author;
this.size = size;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void showInfo() {
System.out.println("ID" + ID + ",Name: "
+ name + ",Year" + year + ",Author"
+ author + ",Size" + size);
}
}
// BOOKMAGEMENT
import java.util.ArrayList;
import java.util.Comparator;
public class BookManagement {
// Danh sách chứa các đối tượng Book
private ArrayList<Book> listEBook = new ArrayList<>();
// Thêm một cuốn sách mới vào danh sách
public void entryData(Book book) {
listEBook.add(book);
}
// In danh sách các cuốn sách ra màn hình dưới dạng bảng
public void printData() {
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+---------+");
System.out.println("| No. | ID | Name | Year |"
+ " Author | Size |");
// In thông tin của từng cuốn sách
int index = 1;
for (Book book : listEBook) {
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+---------+");
System.out.printf("| %-3d | %-5s | %-20s | %-4d | %-11s | %-3dKB |\n",
index++, book.getID(), book.getName(), book.getYear(), book.getAuthor(), book.getSize());
}
// In đường kẻ cuối cùng của bảng
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+---------+");
}
// Sắp xếp danh sách sách theo kích thước giảm dần
public void sort() {
listEBook.sort(Comparator.comparingInt(Book::getSize).reversed());
}
// Tìm kiếm sách theo ID
public int searchBookByID(String ID) {
for (int i = 0; i < listEBook.size(); i++) {
if (listEBook.get(i).getID().equals(ID)) {
return i;
}
}
return -1; // Không tìm thấy
}
// // Search book by ID (return book object)
// public Book searchBookObjectByID(String ID) {
// for (Book book : listEBook) {
// if (book.getID().equals(ID)) {
// return book;
// }
// }
// return null; // Not found
// }
// Tìm các cuốn sách có kích thước lớn nhất
public ArrayList<Book> biggestSize() {
ArrayList<Book> result = new ArrayList<>();
if (listEBook.isEmpty()) {
return result;
}
// Tìm kích thước lớn nhất
int maxSize = listEBook.stream().mapToInt(Book::getSize).max().orElse(0);
// Thêm tất cả sách có kích thước lớn nhất vào danh sách kết quả
for (Book book : listEBook) {
if (book.getSize() == maxSize) {
result.add(book);
}
}
return result;
}
// In ra danh sách các cuốn sách có kích thước lớn nhất
public void printBiggestSizeBooks() {
ArrayList<Book> biggestBooks = biggestSize();
if (biggestBooks.isEmpty()) {
System.out.println("No books found.");
return;
}
// In tiêu đề của bảng
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
System.out.println("| No. | ID | Name | Year |"
+ " Author | Size |");
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
// In thông tin của từng cuốn sách có kích thước lớn nhất
int index = 1;
for (Book book : biggestBooks) {
System.out.printf("| %-3d | %-5s | %-20s | %-4d | %-10s | %-2dKB |\n",
index++, book.getID(), book.getName(), book.getYear(), book.getAuthor(), book.getSize());
}
// In đường kẻ cuối cùng của bảng
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
}
// Tìm kiếm và in thông tin sách theo ID
public void searchBookByIDAndPrint(String id) {
// Tìm kiếm sách theo ID và lấy chỉ số của sách trong danh sách
int index = -1;
for (int i = 0; i < listEBook.size(); i++) {
if (listEBook.get(i).getID().equals(id)) {
index = i;
break;
}
}
// Hiển thị kết quả tìm kiếm
System.out.println(">>> Please enter book's ID to search: " + id);
System.out.println("Search Results:");
if (index == -1) {
System.out.println("Not Found");
} else {
// In tiêu đề của bảng
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
System.out.println("| No. | ID | Name | Year |"
+ " Author | Size |");
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
// In thông tin của sách tìm thấy
System.out.printf("| %-3d | %-5s | %-20s | %-4d | %-10s | %-3dKB |\n",
index + 1, listEBook.get(index).getID(), listEBook.get(index).getName(),
listEBook.get(index).getYear(), listEBook.get(index).getAuthor(),
listEBook.get(index).getSize());
// In đường kẻ cuối cùng của bảng
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
}
}
// Sắp xếp và in danh sách sách theo kích thước tăng dần
public void printSortedBooksBySize() {
// Sắp xếp danh sách sách theo kích thước
listEBook.sort(Comparator.comparingInt(Book::getSize));// Gọi phương thức sort để sắp xếp danh sách theo kích thước tăng dần
// In danh sách đã sắp xếp theo định dạng bảng
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
System.out.println("| No. | ID | Name | Year |"
+ " Author | Size |");
// In thông tin của từng cuốn sách đã sắp xếp
int index = 1;
for (Book book : listEBook) {
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
System.out.printf("| %-3d | %-5s | %-20s | %-4d | %-11s | %-3dKB |\n",
index++, book.getID(), book.getName(), book.getYear(), book.getAuthor(), book.getSize());
}
// In đường kẻ cuối cùng của bảng
System.out.println("+-----+-------+----------------------+------+"
+ "-------------+-------+");
}
}
//java....
public class JavaApplication {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Khởi tạo đối tượng BookManagement để quản lý sách
BookManagement bm = new BookManagement();
// Khởi tạo đối tượng Scanner để đọc input từ người dùng
Scanner sc = new Scanner(System.in);
int choice;
do {
// Hiển thị menu cho người dùng
System.out.println("\n--- Book Management Menu ---");
System.out.println("1. Adds new book");
System.out.println("2. Shows all books");
System.out.println("3. The biggest size book");
System.out.println("4. Search a book by ID");
System.out.println("5. Sort the list of books ascending by size");
System.out.println("6. Quit");
System.out.print("Please select a function: ");
// Đọc lựa chọn của người dùng
choice = sc.nextInt();
sc.nextLine(); // Loại bỏ ký tự xuống dòng còn lại
switch (choice) {
case 1:
// Thêm sách mới
System.out.print("Input ID: ");
String id = sc.nextLine();
System.out.print("Input name: ");
String name = sc.nextLine();
System.out.print("Input year: ");
int year = sc.nextInt();
sc.nextLine(); // Loại bỏ ký tự xuống dòng
System.out.print("Input authors: ");
String author = sc.nextLine();
System.out.print("Input size (kilobyte): ");
int size = sc.nextInt();
sc.nextLine(); // Loại bỏ ký tự xuống dòng
// Tạo đối tượng Book mới và thêm vào danh sách
bm.entryData(new Book(id, name, year, author, size));
System.out.println("Ebook created and added to list of ebooks successful!"); // Thêm dòng này
break;
case 2:
// Hiển thị tất cả sách
bm.printData();
break;
case 3:
// Hiển thị sách có kích thước lớn nhất
if (bm.biggestSize().isEmpty()) {
System.out.println("No books available.");
} else {
bm.printBiggestSizeBooks();
}
break;
case 4:
// Tìm kiếm sách theo ID
System.out.print("Enter book ID to search: ");
String searchID = sc.nextLine();
bm.searchBookByIDAndPrint(searchID);
break;
case 5:
// Sắp xếp và hiển thị sách theo kích thước tăng dần
bm.printSortedBooksBySize();
break;
case 6:
// Thoát chương trình
System.out.println("THANK FOR USING OUR APPLICATION!\n"
+ "SEE YOU AGAIN!");
break;
default:
// Xử lý lựa chọn không hợp lệ
System.out.println("Invalid choice, please try again.");
break;
}
} while (choice != 0); // Tiếp tục cho đến khi người dùng chọn thoát
sc.close();
}
}
Editor is loading...
Leave a Comment