You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
5.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Login {
private static final String USER_FILE = "users.txt";
private static List<User> users = new ArrayList<>();
public void Main() {
load();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n---------登录系统---------");
System.out.println("请输入用户名和密码(用空格隔开): ");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("q")) {
System.out.println("\n已退出");
break;
}
String[] NandP = input.split(" ");
if (NandP.length != 2) {
System.out.println("请输入正确的用户名和密码!");
continue;
}
String username = NandP[0];
String password = NandP[1];
User user = Prove(username, password);
if (user != null) {
Success(user, scanner);
} else {
System.out.println("请输入正确的用户名和密码!");
}
}
scanner.close();
}
private static void load() {
users.clear();
try (BufferedReader reader = new BufferedReader(new FileReader(USER_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty()) {
String[] parts = line.split(" ");
if (parts.length == 3) {
users.add(new User(parts[0], parts[1], parts[2]));
}
}
}
reader.close();
System.out.println("Loading...");
} catch (IOException e) {
System.out.println("出错!" + e.getMessage());
}
}
private static User Prove(String username, String password) {
for (User user : users) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
return user;
}
}
return null;
}
private static void Success(User user, Scanner scanner) {
String role = getRole(user.getRole());
System.out.println("\n登录成功\n" + user.getUsername() + " [" + role + "]");
QuestionManager questionManager = new QuestionManager(user.getUsername(), user.getRole());
while (true) {
System.out.println("\n输入 -1 退出登录,输入 q 退出程序,输入\"切换为XX\"切换难度XX为小学、初中、高中");
System.out.println("准备生成[" + questionManager.getCurrentRoleChinese() + "]数学题目请输入生成题目数量10~30");
String input = scanner.nextLine().trim();
if (input.equals("-1")) {
System.out.println("返回登录界面...");
break;
} else if (input.equalsIgnoreCase("q")) {
System.out.println("\n已退出");
System.exit(0);
} else if (input.startsWith("切换为")) {
Switch(questionManager, input);
} else {
try {
int count = Integer.parseInt(input);
if (count >= 10 && count <= 30) {
questionManager.generateQuestions(count);
} else if (count == -1) {
System.out.println("返回登录界面...");
break;
} else {
System.out.println("题目数量应在10~30之间!");
}
} catch (NumberFormatException e) {
System.out.println("请输入有效的数字10~30或指定命令!");
}
}
}
}
private static void Switch(QuestionManager questionManager, String input) {
String newRole = input.substring(3).trim();
switch (newRole) {
case "小学":
questionManager.setRole("primary");
System.out.println("已切换为小学难度");
break;
case "初中":
questionManager.setRole("junior");
System.out.println("已切换为初中难度");
break;
case "高中":
questionManager.setRole("senior");
System.out.println("已切换为高中难度");
break;
default:
System.out.println("请输入小学、初中和高中三个选项中的一个");
}
}
private static String getRole(String role) {
switch (role) {
case "primary":
return "小学";
case "junior":
return "初中";
case "senior":
return "高中";
default:
return "未知";
}
}
}