Untitled

 avatar
unknown
plain_text
a year ago
3.6 kB
4
Indexable
package baidemo8;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class AppDemo8 {

    static ArrayList<Product> ds = new ArrayList<>();

    public static void main(String[] args) {
        loadDuLieu();
        menu();
    }

    private static void loadDuLieu() {
        try (BufferedReader reader = new BufferedReader(new FileReader(".\\src\\baidemo8\\idemo8.txt"))) {
            String s = reader.readLine();
            while (s != null) {
                String[] data = s.split("[|]");
                String code = data[1];
                String name = data[2];
                double price = Double.parseDouble(data[3]);

                if (data[0].equalsIgnoreCase("B")) {
                    String author = data[4];
                    ds.add(new Book(author, name, code, price));
                } else {
                    String flavor = data[4];
                    ds.add(new Food(flavor, code, name, price));
                }
                s = reader.readLine();
            }
        } catch (Exception e) {
            System.out.println("Loi load du lieu");
        }
    }

    private static void menu() {
        try (Scanner sc = new Scanner(System.in)) {
            while (true) {
                System.out.println("1. Hiển thị danh sách các sản phẩm");
                System.out.println("2. Bổ sung thêm sản phẩm");
                System.out.println("3. Tìm kiếm sản phẩm theo code");
                System.out.println("4. Xoá sản phẩm theo code");
                System.out.println("5. Cập nhật thông tin theo code");
                System.out.println("6. Sắp xếp danh sách sản phẩm giảm dần theo code");
                System.out.println("0. Thoát");
                System.out.println("Bạn chọn mục nào (0-6)?");

                String chon = sc.nextLine();

                switch (chon) {
                    case "1":
                        hienThiDanhSach();
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    case "5":
                        break;
                    case "6":
                        break;
                    case "0":
                        System.out.print("Bạn có lưu dữ liệu không (C/K)? ");
                        String chonLuu = sc.nextLine();
                        if (chonLuu.equalsIgnoreCase("C")) {
                            luuData();
                        }
                        System.exit(0);
                        break;
                    default:
                        System.err.println("Bạn đã chọn sai");
                        break;
                }
            }
        }
    }

    private static void hienThiDanhSach() {
        for (Product p : ds) {
            System.out.println(p);
        }
    }

    private static void luuData() {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(".\\src\\baidemo8\\idemo8.txt"))) {
            for (Product p : ds) {
                writer.write(p.toString() + "\n");
            }
        } catch (Exception e) {
            System.out.println("Lỗi ghi file");
        }
    }
}
Editor is loading...
Leave a Comment