-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
155 lines (152 loc) · 7.41 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.EventQueue;
public class Main{
static HashMap<String, User> users = new HashMap<String, User>();
static TreeMap<String, Book> books = new TreeMap<String, Book>();
static String loginemail;
public static void init()throws IOException{
//iden name ps email
File doc1 = new File("users.txt");
Scanner scanner = new Scanner(doc1);
while (scanner.hasNextLine()){
String tmp = scanner.nextLine();
String tmps[] = tmp.split(" ");
if(tmps[0].equals("Admin")){
users.put(tmps[3], new Admin(tmps[1], tmps[2], tmps[3]));
}
else if(tmps[0].equals("Student")){
users.put(tmps[3], new Student(tmps[1], tmps[2], tmps[3]));
}
else if(tmps[0].equals("Teacher")){
users.put(tmps[3], new Teacher(tmps[1], tmps[2], tmps[3]));
}
else if(tmps[0].equals("Staff")){
users.put(tmps[3], new Staff(tmps[1], tmps[2], tmps[3]));
}
}
//isbn bookname author publish page status year day email
File doc2 = new File("books.txt");
scanner = new Scanner(doc2);
while (scanner.hasNextLine()){
String tmp = scanner.nextLine();
String tmps[] = tmp.split(" ");
if(tmps[5].equals("false"))
books.put(tmps[0], new Book(tmps[0], tmps[1], tmps[2], tmps[3], tmps[4], false, Integer.parseInt(tmps[6]), Integer.parseInt(tmps[7]),tmps[8]));
else{
books.put(tmps[0], new Book(tmps[0], tmps[1], tmps[2], tmps[3], tmps[4], true, Integer.parseInt(tmps[6]), Integer.parseInt(tmps[7]),tmps[8]));
if(users.get(tmps[8]) instanceof Student){
Student s = (Student)users.get(tmps[8]);
s.books.add(books.get(tmps[0]));
s.count++;
}
else if(users.get(tmps[8]) instanceof Teacher){
Teacher s = (Teacher)users.get(tmps[8]);
s.books.add(books.get(tmps[0]));
s.count++;
}
else if(users.get(tmps[8]) instanceof Staff){
Staff s = (Staff)users.get(tmps[8]);
s.books.add(books.get(tmps[0]));
s.count++;
}
}
}
//isbn bookname author publish page status year day email
File doc3 = new File("record.txt");
scanner = new Scanner(doc3);
while (scanner.hasNextLine()){
String tmp = scanner.nextLine();;
String tmps[] = tmp.split(" ");
if(users.get(tmps[8]) instanceof Student){
Student s = (Student)users.get(tmps[8]);
s.record.add(new Book(tmps[0], tmps[1], tmps[2], tmps[3], tmps[4], false, Integer.parseInt(tmps[6]), Integer.parseInt(tmps[7]),tmps[8]));
}
else if(users.get(tmps[8]) instanceof Teacher){
Teacher s = (Teacher)users.get(tmps[8]);
s.record.add(new Book(tmps[0], tmps[1], tmps[2], tmps[3], tmps[4], false, Integer.parseInt(tmps[6]), Integer.parseInt(tmps[7]),tmps[8]));
}
else if(users.get(tmps[8]) instanceof Staff){
Staff s = (Staff)users.get(tmps[8]);
s.record.add(new Book(tmps[0], tmps[1], tmps[2], tmps[3], tmps[4], false, Integer.parseInt(tmps[6]), Integer.parseInt(tmps[7]),tmps[8]));
}
}
}
public static Book search(){
String isbn = JOptionPane.showInputDialog(null, "請輸入要搜尋的書籍的isbn:");
Book b = books.get(isbn);
if(b != null){
JOptionPane.showMessageDialog(null, b);
return b;
}
else{
JOptionPane.showMessageDialog(null, "查無此書!");
return b;
}
}
public static void add_user()throws IOException{
String identification[] = {"管理員", "學生", "老師", "職員"};
int identification_option = JOptionPane.showOptionDialog(null, "請選擇身分", null, 1, 1, null, identification, null);
if(identification_option == 0){//admin
String new_name = JOptionPane.showInputDialog(null, "請輸入您的名字:");
String new_email = JOptionPane.showInputDialog(null, "請輸入您的E-mail:");
String new_password = JOptionPane.showInputDialog(null, "請輸入您的密碼:");
users.put(new_email, new Admin(new_name, new_password, new_email));
FileWriter filewriter = new FileWriter("users.txt", true);
filewriter.write("Admin "+new_name+" "+new_password+" "+new_email+ "\r\n");
filewriter.close();
}
else if(identification_option == 1){//student
String new_name = JOptionPane.showInputDialog(null, "請輸入您的名字:");
String new_email = JOptionPane.showInputDialog(null, "請輸入您的E-mail:");
String new_password = JOptionPane.showInputDialog(null, "請輸入您的密碼:");
users.put(new_email, new Student(new_name, new_password, new_email));
FileWriter filewriter = new FileWriter("users.txt", true);
filewriter.write("Student "+new_name+" "+new_password+" "+new_email+ "\r\n");
filewriter.close();
}
else if(identification_option == 2){//teacher
String new_name = JOptionPane.showInputDialog(null, "請輸入您的名字:");
String new_email = JOptionPane.showInputDialog(null, "請輸入您的E-mail:");
String new_password = JOptionPane.showInputDialog(null, "請輸入您的密碼:");
users.put(new_email, new Teacher(new_name, new_password, new_email));
FileWriter filewriter = new FileWriter("users.txt", true);
filewriter.write("Teacher "+new_name+" "+new_password+" "+new_email+ "\r\n");
filewriter.close();
}
else {//staff
String new_name = JOptionPane.showInputDialog(null, "請輸入您的名字:");
String new_email = JOptionPane.showInputDialog(null, "請輸入您的E-mail:");
String new_password = JOptionPane.showInputDialog(null, "請輸入您的密碼:");
users.put(new_email, new Staff(new_name, new_password, new_email));
FileWriter filewriter = new FileWriter("users.txt", true);
filewriter.write("Staff "+new_name+" "+new_password+" "+new_email+ "\r\n");
filewriter.close();
}
}
public static void save()throws IOException{
FileWriter filewriter = new FileWriter("books.txt");
for(Map.Entry<String, Book> it:books.entrySet()){
filewriter.write(it.getValue().getIsbn()+" "+it.getValue().getName()+" "+it.getValue().getAuthor()+" "+it.getValue().getPublisher()+" "+it.getValue().getPage()+" "+it.getValue().getLend()+" "+it.getValue().getReturn_year()+" "+it.getValue().getReturn_day()+" "+it.getValue().getWho()+"\r\n");
}
filewriter.close();
}
public static void main(String[] args)throws IOException{
init();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login mainframe = new Login();
mainframe.setVisible(true);
save();
/*while(mainframe.control){
mainframe.setVisible(true);
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}