赞
踩
- /**
- * @author xyl
- * @date 2023/2/21 10:31
- */
- public class User {
- private String username;//账号
- private String password;//密码
- private String role;//角色 U:普通用户 Y:管理员
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public String getRole() {
- return role;
- }
-
- public void setRole(String role) {
- this.role = role;
- }
-
- public User() {
- }
-
- public User(String username, String password, String role) {
- this.username = username;
- this.password = password;
- //注册的用户应该要默认是普通用户
- //管理员账号应该不支持注册的
- if (role == null){
- this.role = "U";
- }else {
- this.role = role;
- }
- }
-
- @Override
- public String toString() {
- return "User{" +
- "username='" + username + '\'' +
- ", password='" + password + '\'' +
- ", role='" + role + '\'' +
- '}';
- }
- }
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
-
- /**
- * 公共方法提取
- *
- * @author xyl
- * @date 2023/2/21 10:36
- */
- public class Common {
- private static final Scanner sc = new Scanner(System.in);
- static Map<String, User> userMap = new HashMap<>();
-
- //菜单
- public static void menu(String... menus) {
- userMap.put("root", new User("root", "root", "Y"));
- for (String menu : menus) {
- System.out.println(menu);
- }
- }
-
- //注册
- public static void register() {
- System.out.println("=========欢迎注册本系统=========");
- while (true) {
- System.out.println("请输入要注册的账号:");
- String username = sc.next();
- boolean contains = userMap.containsKey(username);
- if (contains) {
- System.out.println("用户已存在,请重新输入!");
- continue;
- }
- System.out.println("请输入密码:");
- String password = sc.next();
- userMap.put(username, new User(username, password, null));
- System.out.println("注册成功!");
- return;
- }
- }
-
- public static void login() {
- System.out.println("=========欢迎登录本系统=========");
- while (true) {
- System.out.println("请输入要登录的账号:");
- String username = sc.next();
- System.out.println("请输入密码:");
- String password = sc.next();
- if (userMap.containsKey(username) && userMap.get(username).getPassword().equals(password)) {
- if (userMap.get(username).getRole().equals("Y")) {
- System.out.println("登录成功,欢迎" + username + "[管理员]使用本系统");
- }else {
- System.out.println("登录成功,欢迎" + username + "使用本系统");
- }
- return;
- } else {
- System.out.println("用户名或密码错误!");
- }
- }
- }
- }
-
- import java.util.Scanner;
-
- /**
- * @author xyl
- * @date 2023/2/21 11:04
- */
- public class Test {
- private static final Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- while (true){
- Common.menu("=========欢迎使用好再来系统=========","1.注册","2.登录","3.退出","请选择您的操作:");
- String next = sc.next();
- switch (next){
- case "1":
- Common.register();
- break;
- case "2":
- Common.login();
- //这里可以在加上你登录之后需要展示的信息或者是操作
- return;
- case "3":
- System.out.println("正在退出系统,请稍后......");
- System.out.println("成功退出");
- System.exit(0);
- break;
- default:
- System.out.println("暂时没有您选择的功能呢,请重新输入:");
- break;
- }
- }
- }
- }
注册:
登录:
ps:
如果有不到之处,希望各位大佬批评指出。如果有小白看不懂或者不理解的,可以在评论区留言!
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。