发布于2021-06-12 14:37 阅读(890) 评论(0) 点赞(4) 收藏(5)
- package com.userlogin;
-
- import java.util.Scanner;
-
- public class LoginSystem {
- static int num = 0; //创建用户的个数
-
- public static void main(String[] args){ //主函数
- User[] users = new User[10];
- Scanner input = new Scanner(System.in);
- boolean flag = true;
-
- while(flag){ //功能实现
- System.out.print("0.退出程序\t");
- System.out.print("1.创建用户\t");
- System.out.print("2.用户登录\t");
- System.out.println("3.查看所有账户");
- System.out.println("请输入您要使用的功能:");
- int p = input.nextInt();
- switch(p){
- case(1):{
- function1(users,input,true); //功能1
- break;
- }
- case(2):{
- function2(users,input,true); //功能2
- break;
- }
- case(3):{
- usersShow(users); //查看所有用户名
- break;
- }
- case(0):
- default:flag = false;
- }
- }
- }
-
- public static void function1(User[] users,Scanner input,boolean flag_1){ //实现功能1
- while(flag_1){
- boolean flag_11,flag_12;
- flag_11 = flag_12 = true;
- String psd = "0";
-
- System.out.println("请输入你要创建的用户名:");
- String uname = input.next();
- try{
- cheekUsername(uname);
- }
- catch(Exception e){
- System.out.println(e.getMessage());
- continue;
- }
- while(flag_11){
- System.out.println("请输入密码:");
- psd = input.next();
- try{
- cheekUserSetPassword(psd);
- }
- catch(Exception e){
- System.out.println(e.getMessage());
- continue;
- }
- flag_11 = false;
- }
- while(flag_12){
- System.out.println("确认密码:");
- try{
- cheekUserSetPassword(psd,input.next());
- }
- catch(Exception e){
- System.out.println(e.getMessage());
- continue;
- }
- flag_12 = false;
- }
- users[num++] = new User(uname,psd);
- System.out.println("创建用户"+users[num-1].getUsername()+"成功!");
- System.out.println("是否继续创建用户?(1/是 0/否)");
- if(input.nextInt() == 0){
- flag_1 = false;
- }
- }
- }
-
- public static void function2(User[] users,Scanner input,boolean flag_2){ //实现功能2
- while(flag_2)
- {
- System.out.println("请输入用户名:");
- String uname = input.next();
- try{
- cheekUsername(uname);
- }
- catch(Exception e){
- System.out.println(e.getMessage());
- continue;
- }
- System.out.println("请输入密码:");
- String psd = input.next();
- try{
- cheekUserPassword(users,uname,psd);
- }
- catch(Exception e){
- System.out.println(e.getMessage());
- continue;
- }
- System.out.println("登录成功,欢迎"+uname+"回到LoginSystem!");
- flag_2 = false;
- }
- }
-
- public static void cheekUsername(String uname) throws UserLoginException{ //用户名是否符合要求,不符合抛出异常
- if(uname.length() < 6){
- throw new UserLoginException("用户名过短");
- }
- else if(uname.length() > 14){
- throw new UserLoginException("用户名过长");
- }
- }
-
- public static void cheekUserPassword(User[] users,String uname,String psd) throws UserLoginException{ //登录时密码是否符合要求,不符合抛出异常
- if(psd.length() > 16){
- throw new UserLoginException("密码输入过长");
- }
- if(psd.length() < 3){
- throw new UserLoginException("密码输入过短");
- }
- for(int i=0;i<num;i++){
- if(users[i].getUsername().equals(uname)){
- if(!users[i].getPassword().equals(psd)){
- throw new UserLoginException("密码错误");
- }
- else{
- return;
- }
- }
- }
- throw new UserLoginException("该用户不存在");
- }
-
- public static void cheekUserSetPassword(String psd) throws UserLoginException{ //创建用户时密码是否符合要求,不符合抛出异常
- if(psd.length() < 3){
- throw new UserLoginException("密码过短");
- }
- else if(psd.length() > 16){
- throw new UserLoginException("密码过长");
- }
- }
-
- public static void cheekUserSetPassword(String psd,String enpsd) throws UserLoginException{ //创建用户时确认密码是否符合要求,不符合抛出异常
- if(enpsd.length() < 3){
- throw new UserLoginException("密码过短");
- }
- else if(enpsd.length() > 16){
- throw new UserLoginException("密码过长");
- }
- else if(!psd.equals(enpsd)){
- throw new UserLoginException("两次输入密码不一致");
- }
- }
-
- public static void usersShow(User[] users){ //查看所有用户名
- for(int i=0;i<num;i++){
- System.out.println("用户"+(i+1)+":"+users[i].getUsername());
- }
- }
- }
- package com.userlogin;
-
- public class User {
- public String username;
- public String password;
-
- public User(String username,String password){
- this.username = username;
- this.password = password;
- }
-
- public String getUsername(){
- return username;
- }
-
- public String getPassword(){
- return password;
- }
- }
- package com.userlogin;
-
- public class UserLoginException extends Exception{
- public UserLoginException(){
-
- }
-
- public UserLoginException(String s){
- super(s);
- }
- }
运行结果:
总结:
这个只是一个用户登录系统的雏形,还有很多的地方等待完善
比如用户创建后的数据应该保留在一个文件内,还有可以查看所有用户的用户名应该只有超级管理员才能看,剩下的就是一些代码优化和系统功能的扩充
作者:哦哦好吧
链接:http://www.javaheidong.com/blog/article/222404/0035e40c9bb407543b87/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!