-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStuManageControl.txt
233 lines (219 loc) · 5.74 KB
/
StuManageControl.txt
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
public class StuManageControl {
private List<String> searchedList = new ArrayList<String>();
private Map<String, List<String>> map = new HashMap<String, List<String>>();
private void welcome() {
System.out.println("你好,欢迎登录学生管理系统!");
this.menu();
}
private void menu() {
System.out.println("请选择相应的功能:\n[1]=用户登录 [2]=用户注册 [3]=信息查询 [4]=退出系统");
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int menu = scanner.nextInt();
try {
this.menu(menu);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void menu(int menu) throws Exception{
switch (menu) {
case 1:
this.login();
break;
case 2:
this.registration();
break;
case 3:
this.search();
break;
case 4:
this.exit();
break;
default:
break;
}
}
//登录
private void login() throws Exception {
BufferedReader br = new BufferedReader(new FileReader(new File("./loginData.txt")));
String string = null;
System.out.println("请输入用户名和密码(使用空格分隔):");
Scanner sc = new Scanner(System.in);
String[] loginInfo = sc.nextLine().split(" ");
while ((string = br.readLine())!=null) {
String[] dataInfo = string.split(" ");
if (loginInfo[0].equals(dataInfo[0])){
if (loginInfo[1].equals(dataInfo[1])) {
System.out.println("登录成功!\n用户名:"+dataInfo[0]+" 昵称:"+dataInfo[2]);
this.menu();
}else{
System.out.println("密码错误!返回主界面");
this.menu();
}
}
}
br.close();
System.out.println("找不到用户,返回主界面");
this.menu();
}
//注册
private void registration() throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int i = 0;
String data = null;
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("./loginData.txt"),true));
if (i==0) {
System.out.println("请输入用户名(长度6—8位,主要靠此字段来查询学生信息):");
String isdata = null;
while ((isdata = br.readLine())!=null) {
if (Pattern.matches("^\\w{6,8}$",isdata)) {
data = isdata;
i++;
break;
} else {
System.out.println("用户名不符合要求,请重新输入");
}
}
}
if (i==1) {
System.out.println("请输入密码(长度6—8位):");
String isdata = null;
while ((isdata = br.readLine())!=null) {
if (Pattern.matches("^\\w{6,8}$",isdata)) {
data = data+" "+isdata;
i++;
break;
} else {
System.out.println("密码不符合要求,请重新输入");
}
}
}
if (i==2) {
System.out.println("请输入昵称:");
data = data+" "+br.readLine();
i++;
}
if (i==3) {
System.out.println("请输入学校:");
data = data+" "+br.readLine();
i++;
}
if (i==4) {
System.out.println("请输入专业:");
data = data+" "+br.readLine();
i++;
}
if (i==5) {
System.out.println("请输入住址:");
data = data+" "+br.readLine()+"\n";
i++;
}
if(i==6) {
System.out.println("信息输入完成,是否进行注册?Y/N(选择Y注册,选择N返回主界面):");
String isreg = br.readLine();
while (isreg != null) {
if (isreg.equalsIgnoreCase("Y")) {
bw.write(data);
bw.newLine();
bw.flush();
System.out.println("注册成功");
this.menu();
}else if (isreg.equalsIgnoreCase("N")) {
this.menu();
} else {
System.out.println("格式不正确,请输入Y/N(选择Y注册,选择N返回主界面)");
isreg = br.readLine();
}
}
}
bw.close();
br.close();
isr.close();
}
//查找
private void search() throws Exception {
System.out.println("请输入要查询的昵称:");
Scanner sc = new Scanner(System.in);
String search = sc.next();
BufferedReader br = new BufferedReader(new FileReader(new File("./loginData.txt")));
String dataList = null;
int i = 1;
boolean isSearched = false;
while ((dataList=br.readLine())!=null) {
String[] data = dataList.split(" ");
if (search.equals(data[2])) {
searchedList.add(String.valueOf(i));
i++;
isSearched = true;
for (String str : data) {
searchedList.add(str);
}
map.put(String.valueOf(i-1), searchedList);
searchedList.clear();
Iterator<String> iter = map.get(String.valueOf(i-1)).iterator();
if (i==2) {
System.out.println("序号\t用户名\t\t密码\t昵称\t学校\t专业\t住址");
}
while (iter.hasNext()) {
System.out.print(iter.next()+"\t");
}
System.out.println();
}
}
if (isSearched) {
System.out.println("[1]=信息修改 [其它键]=返回上一级");
Scanner sc1 = new Scanner(System.in);
if (sc1.nextInt()==1) {
this.modify();
} else {
this.menu();
}
} else {
System.out.println("查无此人!");
this.menu();
}
}
//修改
private void modify() throws Exception {
System.out.println("请输入要修改的序号:");
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int serialNum = sc.nextInt();
if (serialNum-1<map.size()) {
System.out.println("请输入新信息完成修改");
String newDataLine = sc.nextLine();
RandomAccessFile raf = new RandomAccessFile(new File("./loginData.txt"), "rw");
raf.close();
String[] newData = newDataLine.split(" ");
} else {
System.out.println("您输入的序号不符合要求,请重新输入:");
}
}
}
//退出
private void exit() {
System.out.println("退出系统,程序停止");
System.exit(0);
}
public static void main(String[] args) {
new Main().welcome();
}
}