用户属性:用户编号,用户名, 密码,姓名,年龄,性别,身份证号码,用户状态(校验用户帐号是否正常,1正常, 0 异常,对于异常用户不能登录)
原有用户有2个:
用户一:用户名:zhang,密码:123; 登录状态:1
用户二:用户名:wang , 密码:111; 登录状态:0
管理员:admin, admin,1
需求:
1:登录:只有当用户名、密码、验证码 都正确的时候,并且用户状态为1的时候才显示登录成功,用户状态为0不能登录。
2:登录成功之后,提示欢迎你: xxx,
可以进行的操作
管理员:
1:查看所有用户:可以选择查看所有用户信息,显示所有的用户名,密码,姓名,用户状态, - 5
2:拉黑:可以根据用户编号拉黑用户,一旦用户被拉黑,则这个用户则不能继续登录了,需要判断输入的用户编号是否合理。 -- 10
3:根据用户编号查看用户的详细信息,需要判断合理性, 如果没有这个用户的话,则显示没有这个人的信息 -- 10
4:根据用户的的某个字进行模糊查询 -- 姓名中包含某个字的都查出来显示。如:三, 则将所有的包含三的用户都显示出来,如:张三,三子,张三疯 -- 20
普通用户:修改密码,在修改之前需要输入原密码,对了之后才能进行修改 - 5
注意:
package Text6; public class User { private int uid; private String username; private String password; private String name; private int age; private String sex; private String cardId; private String stats; public User(String username, String password, String stats) { this.username = username; this.password = password; this.stats = stats; } public User(int uid, String username, String password, String name, int age, String sex, String cardId, String stats) { this.uid = uid; this.username = username; this.password = password; this.name = name; this.age = age; this.sex = sex; this.cardId = cardId; this.stats = stats; } @Override public String toString() { return "User{" + "uid=" + uid + ", username='" + username + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", cardId='" + cardId + '\'' + ", stats='" + stats + '\'' + '}'; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } 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 getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public String getStats() { return stats; } public void setStats(String stats) { this.stats = stats; } }
package Text6; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Demo { static Scanner sc=new Scanner(System.in); static List<User> users=new ArrayList<>(); static User loginUser=null; public static void main(String[] args) { User user0=new User(0,"admin","admin","管理员",20,"男","10086","1"); User user1=new User(1,"zhang","123","张三",18,"男","10001","1"); User user2=new User(2,"wang","111","王五",38,"男","10002","0"); users.add(user0); users.add(user1); users.add(user2); index(); } public static void index(){ System.out.println("请选择需要的操作"); System.out.println("1.登录"); System.out.println("2.输入任意键退出"); Scanner sc = new Scanner(System.in); String num = sc.next(); switch(num){ case "1": login(); break; default: System.out.println("程序退出"); System.exit(0); } } public static void login(){ System.out.println("输入用户名:"); String myName = sc.next(); System.out.println("输入密码"); String myPwd = sc.next(); String username=null; for(User user : users){ if("1".equals(user.getStats())&&(myName.equals(user.getUsername()))&& (myPwd.equals(user.getPassword()))){ username = myName; System.out.println("登录成功,欢迎你:"+username); break; } } if(username == null){ System.out.println("登陆失败,请重新登陆"); index();//登陆失败回到首页 }else{ if ("admin".equals(username)){ loginUser=users.get(0);//改========== adminView();//管理员视图 }else{ //获取当前用户登录对象 for (User u:users) { if(u.getUsername().equals(myName)){ //进来了u就是登录对象 loginUser=u; } } userView(); } } } private static void userView() { System.out.println("1.修改密码"); System.out.println("2.查看我的信息"); System.out.println("3.输入0返回上一级"); int num=sc.nextInt(); if(num==1){ changePwd(); }else if(num==2){ System.out.println(loginUser.getUid()+"\t"+loginUser.getUsername()+"\t"+loginUser.getPassword()+"\t"+loginUser.getAge()+"\t"+loginUser.getSex()+"\t"+loginUser.getCardId()+"\t"+loginUser.getStats()); userView(); }else if(num==0){ index(); } } private static void changePwd() { System.out.println("请输入原密码:"); String oldPwd=sc.next(); if(oldPwd.equals(loginUser.getPassword())){ System.out.println("请输入新密码:"); String newPwd=sc.next(); System.out.println("请再次确认密码:"); String newPwd2=sc.next(); if(newPwd.equals(newPwd2)){ loginUser.setPassword(newPwd); System.out.println("修改密码成功"); login(); }else { System.out.println("两次密码不一致,请重新操作"); changePwd(); } System.out.println("密码错误,请重新操作"); changePwd(); } } private static void adminView() { System.out.println("欢迎你,admin"); System.out.println("1:查看所有用户");//查 System.out.println("2.拉黑 ");//状态改成0需要判断输入的用户编号是否合理 System.out.println("3.查看详细用户信息");//需要判断合理性, 如果没有这个用户的话,则显示没有这个人的信息 System.out.println("4.模糊查询");//根据用户的的某个字进行模糊查询 -- 姓名中包含某个字的都查出来显示。如:三, 则将所有的包含三的用户都显示出来,如:张三,三子,张三疯 -- 20 System.out.println("0.回到首页"); int num=sc.nextInt(); switch (num){ case 1:showUsers(); case 2:blackUsers(); case 3:showDetialUsers(); case 4:blurSelectUsers(); case 0:index(); default: System.out.println("输入有误"); adminView(); } } private static void blurSelectUsers() { System.out.println("请输入模糊查询名字:"); String bname=sc.next(); String bname0=null; for (User user:users) { if(user.getName().contains(bname)){ bname0=bname; System.out.println(user); } } if(bname0==null){ System.out.println("不存在,请重输入"); blurSelectUsers(); }adminView(); } private static void showDetialUsers() { System.out.println("请输入用户id:"); int x=sc.nextInt(); int id=-1; for (User user:users) { if(x==user.getUid()){ id=x; } } if(id!=-1){ for (User user:users) { if(x==user.getUid()){ System.out.println(user); } } }else { System.out.println("id不存在"); showDetialUsers(); } adminView(); } private static void blackUsers() { System.out.println("请输入拉黑用户id"); int id=-1; int x=sc.nextInt(); for (User user:users) { if(x==user.getUid()){ id=x; } } if(id!=-1){ for (User user:users) { if(x==user.getUid()){ user.setStats("0"); } } }else { System.out.println("id不存在"); blackUsers(); } System.out.println("拉黑成功"); adminView(); } private static void showUsers() { for (User user:users) { System.out.println("用户名:"+user.getUsername()+"密码:"+user.getPassword()+"姓名:"+user.getName()+"用户状态:"+user.getStats()); } System.out.println("输入0返回上一级"); int num=sc.nextInt(); if(num==0){ adminView(); }else { System.out.println("输入0返回上一级"); } } }