diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7baa262 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/Y0.0.iml b/Y0.0.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Y0.0.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/AccountService.java b/src/AccountService.java new file mode 100644 index 0000000..792f41c --- /dev/null +++ b/src/AccountService.java @@ -0,0 +1,5 @@ +import java.util.Map; +public interface AccountService { + void setAccounts(String type); + Map getAccounts(); +} diff --git a/src/AccountServiceImpl.java b/src/AccountServiceImpl.java new file mode 100644 index 0000000..ef8d765 --- /dev/null +++ b/src/AccountServiceImpl.java @@ -0,0 +1,36 @@ +import java.util.HashMap; +import java.util.Map; + +public class AccountServiceImpl implements AccountService { + private Map 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 getAccounts() { + return accounts; // 返回账户数据 + } +} diff --git a/src/LoginService.java b/src/LoginService.java new file mode 100644 index 0000000..a67e1cd --- /dev/null +++ b/src/LoginService.java @@ -0,0 +1,3 @@ +public interface LoginService { + boolean authenticate(String username, String password); +} diff --git a/src/LoginServiceImpl.java b/src/LoginServiceImpl.java new file mode 100644 index 0000000..aa6ef14 --- /dev/null +++ b/src/LoginServiceImpl.java @@ -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 accounts = accountService.getAccounts(); + + + if (accounts.containsKey(username)) { + String storedPassword = accounts.get(username); + return storedPassword.equals(password); // 密码匹配 + } else { + return false; + } + } +} diff --git a/src/MathApp.java b/src/MathApp.java new file mode 100644 index 0000000..fdbc62a --- /dev/null +++ b/src/MathApp.java @@ -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("请输入小学、初中和高中三个选项中的一个"); + } + } +} diff --git a/src/UserService.java b/src/UserService.java new file mode 100644 index 0000000..8dac8d6 --- /dev/null +++ b/src/UserService.java @@ -0,0 +1,5 @@ +public interface UserService { + boolean switchAccount(String type); + String getAccountType(); +} + diff --git a/src/UserServiceImpl.java b/src/UserServiceImpl.java new file mode 100644 index 0000000..134166c --- /dev/null +++ b/src/UserServiceImpl.java @@ -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; + } +}