111 #1

Merged
hnu202326010332 merged 1 commits from main into develop 4 months ago

30
.gitignore vendored

@ -0,0 +1,30 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.idea/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,5 @@
import java.util.Map;
public interface AccountService {
void setAccounts(String type);
Map<String, String> getAccounts();
}

@ -0,0 +1,36 @@
import java.util.HashMap;
import java.util.Map;
public class AccountServiceImpl implements AccountService {
private Map<String, String> accounts = new HashMap<>();
@Override
public void setAccounts(String type) {
switch (type) {
case "小学":
accounts.put("张三1", "123");
accounts.put("张三2", "123");
accounts.put("张三3", "123");
break;
case "初中":
accounts.put("李四1", "123");
accounts.put("李四2", "123");
accounts.put("李四3", "123");
break;
case "高中":
accounts.put("王五1", "123");
accounts.put("王五2", "123");
accounts.put("王五3", "123");
break;
default:
System.out.println("未知账户类型!");
break;
}
}
@Override
public Map<String, String> getAccounts() {
return accounts; // 返回账户数据
}
}

@ -0,0 +1,3 @@
public interface LoginService {
boolean authenticate(String username, String password);
}

@ -0,0 +1,27 @@
import java.util.Map;
public class LoginServiceImpl implements LoginService {
private AccountService accountService;
public LoginServiceImpl(AccountService accountService) {
this.accountService = accountService;
}
@Override
public boolean authenticate(String username, String password) {
username = username.trim();
password = password.trim();
Map<String, String> accounts = accountService.getAccounts();
if (accounts.containsKey(username)) {
String storedPassword = accounts.get(username);
return storedPassword.equals(password); // 密码匹配
} else {
return false;
}
}
}

@ -0,0 +1,88 @@
import java.util.Scanner;
public class MathApp {
private static LoginService loginService;
private static Scanner scanner = new Scanner(System.in);
private static String currentType = "小学"; // 默认账户类型是 "小学"
public static void main(String[] args) {
AccountService accountService = new AccountServiceImpl();
accountService.setAccounts(currentType);
loginService = new LoginServiceImpl(accountService);
while (true) {
login();
}
}
public static void login() {
System.out.print("请输入用户名和密码(空格隔开):");
String input = scanner.nextLine();
String[] credentials = input.split(" ");
if (credentials.length != 2) {
System.out.println("格式错误,请重新输入!");
return;
}
String username = credentials[0].trim();
String password = credentials[1].trim();
if (loginService.authenticate(username, password)) {
System.out.println("登录成功!");
System.out.println("当前选择为 " + currentType + " 出题");
generateQuestions();
} else {
System.out.println("用户名或密码错误,请重新输入!");
}
}
public static void generateQuestions() {
System.out.print("请输入生成题目数量(输入-1退出登录");
int numQuestions = scanner.nextInt();
scanner.nextLine();
if (numQuestions == -1) {
System.out.println("退出登录!");
return;
}
if (numQuestions < 10 || numQuestions > 30) {
System.out.println("请输入有效的题目数量10-30");
return;
}
System.out.println("生成 " + numQuestions + " 道题目...");
}
public static void switchAccountType() {
System.out.print("请输入要切换的账户类型(小学、初中或高中):");
String type = scanner.nextLine().trim();
if (type.equals("小学") || type.equals("初中") || type.equals("高中")) {
currentType = type;
AccountService accountService = new AccountServiceImpl();
accountService.setAccounts(currentType);
loginService = new LoginServiceImpl(accountService);
System.out.println("系统提示:准备生成 " + currentType + " 数学题目,请输入生成题目数量");
} else {
System.out.println("请输入小学、初中和高中三个选项中的一个");
}
}
}

@ -0,0 +1,5 @@
public interface UserService {
boolean switchAccount(String type);
String getAccountType();
}

@ -0,0 +1,25 @@
public class UserServiceImpl implements UserService {
private AccountService accountService;
private String accountType = "";
public UserServiceImpl(AccountService accountService) {
this.accountService = accountService;
}
@Override
public boolean switchAccount(String type) {
if ("小学".equals(type) || "初中".equals(type) || "高中".equals(type)) {
accountType = type;
accountService.setAccounts(accountType);
return true;
} else {
System.out.println("请选择正确的账户类型!");
return false;
}
}
@Override
public String getAccountType() {
return accountType;
}
}
Loading…
Cancel
Save