-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
138 lines (123 loc) · 4.02 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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 scanner = new Scanner(System.in);
while(true) {
System.out.println("================ 图书管理系统 ================");
System.out.println("1.插入信息");
System.out.println("2.修改信息");
System.out.println("3.查询图书列表");
System.out.println("4.删除图书");
System.out.println("(按任意键退出系统)");
String str = scanner.nextLine();
switch (str){
case "1":
insertBook(scanner);
break;
case "2":
modifyBook(scanner);
break;
case "3":
showBooks();
break;
case "4":
deleteBook(scanner);
break;
default:
saveData();
scanner.close();
return;
}
}
}
@SuppressWarnings("unchecked")
private static void readData(){
File file = new File("data");
if(file.exists()) {
try(ObjectInputStream inputStream = new ObjectInputStream((new FileInputStream("data")))){
LIST = (List<Book>) inputStream.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 modifyBook(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 showBooks(){
LIST.forEach(System.out::println);
}
private static void deleteBook(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(i);
scanner.nextLine();
}
private static void insertBook(Scanner scanner){
Book book = new Book()
.name(scanner.nextLine())
.author(scanner.nextLine())
.price(scanner.nextDouble());
LIST.add(book);
scanner.nextLine();
}
private static class Book implements Serializable{
String name;
String author;
transient double price;
public Book name(String name){
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 +
'}';
}
}
}