I/O實(shí)戰(zhàn) 圖書管理系統(tǒng)

視頻鏈接
https://www.bilibili.com/video/BV1Gv411T7pi?p=85&vd_source=74dbfbbeb7bc0d33681acbc0237176f1

創(chuàng)建書類

 private static class Book implements Serializable  //序列化
    {
        String name;
        String author;
        double price;

        public Book name(String name)  //鏈?zhǔn)秸{(diào)用
        {
            this.name = name;
            return this;
        }
        public Book author(String author)
        {
            this.author=author;
            return this;
        }
        public Book price(double price)
        {
            this.price=price;
            return this;
        }

        @Override
        public String toString() {
            return "書籍{" +
                    "名稱='" + name + '\'' +
                    ", 作者='" + author + '\'' +
                    ", 價格=" + price +
                    '}';
        }
    }

主界面

System.out.println("=================圖書管理系統(tǒng)=====================");
            System.out.println("請通過輸入對應(yīng)序號實(shí)現(xiàn)功能");
            System.out.println("1.插入信息");
            System.out.println("2.修改信息");
            System.out.println("3.查詢圖書列表");
            System.out.println("4.刪除圖書");
            System.out.println("按其他任意鍵退出");

            String option=sc.nextLine();
switch (option)
            {

                case "1":insertBook(sc);
                    break;
                case "2":modifyBooks(sc);
                    break;
                case "3":showBooks();
                    break;
                case "4":deleteBooks(sc);
                    break;
                default:
                    saveData();
                    sc.close();
                    return;
            }

先創(chuàng)建一個LIST來存放書籍列表

各函數(shù)的實(shí)現(xiàn)

insertBook(Scanner scanner)插入信息

向LIST中傳入new的新書類并傳入?yún)?shù)(鏈?zhǔn)教砑?,因?yàn)閯?chuàng)建書類的屬性時是鏈?zhǔn)降臉?gòu)造函數(shù))

private static void insertBook(Scanner scanner)
    {
        //鏈?zhǔn)降暮锰?精簡添加
        LIST.add(new Book()
          .name(scanner.nextLine())
          .author(scanner.nextLine())
          .price(scanner.nextDouble()));  //可以添加報錯的實(shí)現(xiàn)
        scanner.nextLine();

    }

修改書籍信息
首先給書籍加序號,然后通過序號來選中要修改的書籍,并修改數(shù)據(jù)

private static void modifyBooks(Scanner scanner)
    {
        int i=0;
        for(Book book:LIST)
        {
            System.out.println(i+++","+book);
        }
        int index=scanner.nextInt();
        scanner.nextLine();
        if(index>=LIST.size()) System.out.println("錯誤的序號");
        else
        {
            LIST.get(index)
                    .name(scanner.nextLine())
                    .author(scanner.nextLine())
                    .price(scanner.nextDouble());
        }

        scanner.nextLine();
    }

刪除書籍
原理同上,通過序號來刪除

private static void deleteBooks(Scanner scanner)
    {

        int i=0;
        for(Book book:LIST)
        {
            System.out.println(i+++","+book);
        }
        int index=scanner.nextInt();
        if(index>=LIST.size()) System.out.println("錯誤的序號");
        else
        {
            LIST.remove(index);
        }

        scanner.nextLine();//吸收空白輸入行
    }

顯示書籍信息
通過LIST自帶的forEach函數(shù)即可實(shí)現(xiàn)

private static void showBooks()
    {
        LIST.forEach(System.out::println);  //forEach要調(diào)用toString方法 所以要改寫toString方法  在Book類里改寫
    }

保障書籍的長期存儲
因?yàn)楸4娴臅鳥ook類 所以可以用類輸入輸出流來保存 這里的data是二進(jìn)制文件

private static void saveData()
    {
        try(ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream("data")))
        {
            outputStream.writeObject(LIST);
            outputStream.flush();
        }catch (IOException e)
        {
            e.printStackTrace();
        }
    }

同樣的,讀取書籍信息 要先判斷是否存在數(shù)據(jù)(data二進(jìn)制文件) 否則新建一個列表

private static void readData()
    {
        File file=new File("data");
        if(file.exists())
        {
            try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("data"))) {
                LIST = (List<Book>) objectInputStream.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        else
        {
            LIST=new ArrayList<>();
        }
    }

全代碼

package 圖書管理系統(tǒng);

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    private static  List<Book> LIST=new ArrayList<>();


    public static void main(String[] args) {

        readData();


        Scanner sc=new Scanner(System.in);
        while(true)
        {
            System.out.println("=================圖書管理系統(tǒng)=====================");
            System.out.println("請通過輸入對應(yīng)序號實(shí)現(xiàn)功能");
            System.out.println("1.插入信息");
            System.out.println("2.修改信息");
            System.out.println("3.查詢圖書列表");
            System.out.println("4.刪除圖書");
            System.out.println("按其他任意鍵退出");

            String option=sc.nextLine();
            switch (option)
            {

                case "1":insertBook(sc);
                    break;
                case "2":modifyBooks(sc);
                    break;
                case "3":showBooks();
                    break;
                case "4":deleteBooks(sc);
                    break;
                default:
                    saveData();
                    sc.close();
                    return;
            }
        }

    }
    @SuppressWarnings("unchecked")
    private static void readData()
    {
        File file=new File("data");
        if(file.exists())
        {
            try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("data"))) {
                LIST = (List<Book>) objectInputStream.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        else
        {
            LIST=new ArrayList<>();
        }
    }

    private static void saveData()
    {
        try(ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream("data")))
        {
            outputStream.writeObject(LIST);
            outputStream.flush();
        }catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void modifyBooks(Scanner scanner)
    {
        int i=0;
        for(Book book:LIST)
        {
            System.out.println(i+++","+book);
        }
        int index=scanner.nextInt();
        scanner.nextLine();
        if(index>=LIST.size()) System.out.println("錯誤的序號");
        else
        {
            LIST.get(index)
                    .name(scanner.nextLine())
                    .author(scanner.nextLine())
                    .price(scanner.nextDouble());
        }

        scanner.nextLine();
    }

    private static void deleteBooks(Scanner scanner)
    {

        int i=0;
        for(Book book:LIST)
        {
            System.out.println(i+++","+book);
        }
        int index=scanner.nextInt();
        if(index>=LIST.size()) System.out.println("錯誤的序號");
        else
        {
            LIST.remove(index);
        }

        scanner.nextLine();
    }

    private static void showBooks()
    {
        LIST.forEach(System.out::println);  //forEach要調(diào)用toString方法 所以要改寫toString方法
    }


    private static void insertBook(Scanner scanner)
    {
        //鏈?zhǔn)降暮锰?精簡添加
        LIST.add(new Book()
          .name(scanner.nextLine())
          .author(scanner.nextLine())
          .price(scanner.nextDouble()));  //可以添加報錯的實(shí)現(xiàn)
        scanner.nextLine();

    }

    private static class Book implements Serializable  //序列化
    {
        String name;
        String author;
        double price;

        public Book name(String name)  //鏈?zhǔn)秸{(diào)用
        {
            this.name = name;
            return this;
        }
        public Book author(String author)
        {
            this.author=author;
            return this;
        }
        public Book price(double price)
        {
            this.price=price;
            return this;
        }

        @Override
        public String toString() {
            return "書籍{" +
                    "名稱='" + name + '\'' +
                    ", 作者='" + author + '\'' +
                    ", 價格=" + price +
                    '}';
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容