1 import java.io.*;
2 import java.util.ArrayList;
3 import java.util.List;
4 import java.util.Scanner;
5
6 /**
7 * @ClassName: demo30
8 * @Description: javase学习-图书管理系统I/O https://www.bilibili.com/video/BV1Gv411T7pi?p=85
9 * @author: 罗一帆
10 * @date: 2022/10/1 14:26
11 */
12 public class demo30 {
13
14 private static List<Book> LIST = new ArrayList<>();
15
16
17 public static void main(String[] args) {
18 readData();
19 Scanner scanner = new Scanner(System.in);
20 while (true) {
21 System.out.println("======= 图书管理系统 =======");
22 System.out.println("1.插入信息");
23 System.out.println("2.修改信息");
24 System.out.println("3.查询图书列表");
25 System.out.println("4.删除图书");
26 System.out.println("(按其他任意键退出系统)");
27 String str = scanner.nextLine();
28 switch (str){
29 case "1":
30 System.out.println("请依次输入书籍名称-作者-价格");
31 insertBook(scanner);
32 break;
33 case "2":
34 System.out.println("请输入要修改的编号");
35 modifyBook(scanner);
36 break;
37 case "3":
38 showBooks();
39 break;
40 case "4":
41 System.out.println("请输入要删除数除书的序号");
42 deleteBook(scanner);
43 break;
44 default:
45 saveData();
46 scanner.close();
47 return;
48 }
49 }
50 }
51
52 private static void readData(){
53 File file = new File("D:\\IDEA_workspace\\absolute-path");
54 if (file.exists()) {
55 try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("D:\\IDEA_workspace\\absolute-path\\IOlibrarManageSystem.txt"))){
56 LIST = (List<Book>) inputStream.readObject();
57 }catch (IOException | ClassNotFoundException e){
58 e.printStackTrace();
59 }
60 }else {
61 LIST = new ArrayList<>();
62 }
63 }
64
65 private static void saveData(){
66 try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("D:\\IDEA_workspace\\absolute-path\\IOlibrarManageSystem.txt"))){
67 outputStream.writeObject(LIST);
68 outputStream.flush();
69 }catch (IOException e){
70 e.printStackTrace();
71 }
72 }
73
74 private static void modifyBook(Scanner scanner) {
75 int i = 0;
76 for(Book book : LIST){
77 System.out.println(i+"."+book);
78 i++;
79 }
80 int index = scanner.nextInt();
81 scanner.nextLine();
82 if(index>=LIST.size()) System.out.println("错误的序号");
83 else {
84 System.out.println("请依次输入书籍名称-作者-价格");
85 LIST
86 .get(index)
87 .name(scanner.nextLine())
88 .author(scanner.nextLine())
89 .price(scanner.nextDouble());
90 }
91 scanner.nextLine();
92 }
93
94 private static void deleteBook(Scanner scanner){
95 int index = scanner.nextInt();
96 int i = 0;
97 for(Book book : LIST){
98 System.out.println(i+"."+book);
99 i++;
100 }
101 if(index>=LIST.size()) System.out.println("错误的序号");
102 else LIST.remove(index);
103 scanner.nextLine();
104 }
105
106 private static void showBooks(){
107 LIST.forEach(System.out::println);
108 }
109
110 private static void insertBook(Scanner scanner){
111 Book book = new Book()
112 .name(scanner.nextLine())
113 .author(scanner.nextLine())
114 .price(scanner.nextDouble());
115 LIST.add(book);
116 scanner.nextLine();
117 }
118
119 private static class Book implements Serializable{
120 String name;
121 String author;
122 double price;
123
124 public Book name(String name){
125 this.name = name;
126 return this;
127 }
128
129 public Book author(String author){
130 this.author = author;
131 return this;
132 }
133
134 public Book price(double price){
135 this.price = price;
136 return this;
137 }
138
139 @Override
140 public String toString() {
141 return "书籍{" +
142 "名称='" + name + '\'' +
143 ", 作者='" + author + '\'' +
144 ", 价格=" + price +
145 '}';
146 }
147 }
148
149 }