By GokiSoft.com|
14:45 14/07/2023|
Java Advanced
[Share Code] Bài tập quản lý sách & lưu thông tin trên Files - C2209I
Bài tập quản lý sách & lưu thông tin trên Files
#BT1091.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
*/
package com.gokisoft.java2.lesson03;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class BT1091 {
static class Book {
String bookName;
String authorName;
float price;
String releasedDate;
String manufacturer;
public Book() {
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getReleasedDate() {
return releasedDate;
}
public void setReleasedDate(String releasedDate) {
this.releasedDate = releasedDate;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten sach: ");
bookName = scan.nextLine();
System.out.println("Nhap tac gia: ");
authorName = scan.nextLine();
System.out.println("Nhap ngay xuat ban: ");
releasedDate = scan.nextLine();
System.out.println("Nhap nsx: ");
manufacturer = scan.nextLine();
}
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "bookName=" + bookName + ", authorName=" + authorName + ", price=" + price + ", releasedDate=" + releasedDate + ", manufacturer=" + manufacturer;
}
public String getLine() {
return bookName + "," + authorName + "," + price + "," + releasedDate + "," + manufacturer + "\n";
}
private void parse(String line) {
try {
String[] params = line.split(",");
bookName = params[0];
authorName = params[1];
price = Float.parseFloat(params[2]);
releasedDate = params[3];
manufacturer = params[4];
} catch(Exception e) {}
}
}
static List<Book> bookList = new ArrayList<>();
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int choose;
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1 -> input();
case 2 -> display();
case 3 -> sort();
case 4 -> saveFile();
case 5 -> readFile();
case 6 -> System.out.println("Thoat!!!");
default -> System.out.println("Nhap sai!!!");
}
} while(choose != 6);
}
static void showMenu() {
System.out.println("1. Nhap N sach");
System.out.println("2. Hien thi thong tin");
System.out.println("3. Sap xep");
System.out.println("4. Luu File");
System.out.println("5. Doc File");
System.out.println("6. Thoat");
System.out.println("Chon: ");
}
private static void input() {
System.out.println("Nhap so sach can them: ");
int N = Integer.parseInt(scan.nextLine());
for (int i = 0; i < N; i++) {
System.out.println("======== Nhap thong tin sach ========");
Book book = new Book();
book.input();
bookList.add(book);
}
}
private static void display() {
System.out.println("Hien thi sach: ");
for (Book book : bookList) {
book.display();
}
}
private static void sort() {
Collections.sort(bookList, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
//am hay duong -> swap o1 & o2
//o1 dang dung truoc o2
//o1:authorName -> B, o2:authorName -> A
//return 1 -> swap: o1.getAuthorName().compareToIgnoreCase(o2.getAuthorName()) > 0
//o1: authorName -> A, o2:authroName -> B
//return -1 -> no swap: o1.getAuthorName().compareToIgnoreCase(o2.getAuthorName()) < 0
return o1.getAuthorName().compareToIgnoreCase(o2.getAuthorName());
}
});
display();
}
private static void saveFile() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("BT1091.txt");
for (Book book : bookList) {
String line = book.getLine();
byte[] b = line.getBytes("utf8");
fos.write(b);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(BT1091.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(BT1091.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(BT1091.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(BT1091.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private static void readFile() {
FileReader reader = null;
BufferedReader bufferedReader = null;
try {
reader = new FileReader("BT1091.txt");
bufferedReader = new BufferedReader(reader);
String line;
while((line = bufferedReader.readLine()) != null) {
if(line.trim().isEmpty()) continue;
Book book = new Book();
book.parse(line);
bookList.add(book);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(BT1091.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(BT1091.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(BT1091.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(reader != null) {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(BT1091.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
display();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)