程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

JAVA实验2:用户登录系统-UserLoginSystem

发布于2021-06-12 14:37     阅读(890)     评论(0)     点赞(4)     收藏(5)


登录系统 

  1. package com.userlogin;
  2. import java.util.Scanner;
  3. public class LoginSystem {
  4. static int num = 0; //创建用户的个数
  5. public static void main(String[] args){ //主函数
  6. User[] users = new User[10];
  7. Scanner input = new Scanner(System.in);
  8. boolean flag = true;
  9. while(flag){ //功能实现
  10. System.out.print("0.退出程序\t");
  11. System.out.print("1.创建用户\t");
  12. System.out.print("2.用户登录\t");
  13. System.out.println("3.查看所有账户");
  14. System.out.println("请输入您要使用的功能:");
  15. int p = input.nextInt();
  16. switch(p){
  17. case(1):{
  18. function1(users,input,true); //功能1
  19. break;
  20. }
  21. case(2):{
  22. function2(users,input,true); //功能2
  23. break;
  24. }
  25. case(3):{
  26. usersShow(users); //查看所有用户名
  27. break;
  28. }
  29. case(0):
  30. default:flag = false;
  31. }
  32. }
  33. }
  34. public static void function1(User[] users,Scanner input,boolean flag_1){ //实现功能1
  35. while(flag_1){
  36. boolean flag_11,flag_12;
  37. flag_11 = flag_12 = true;
  38. String psd = "0";
  39. System.out.println("请输入你要创建的用户名:");
  40. String uname = input.next();
  41. try{
  42. cheekUsername(uname);
  43. }
  44. catch(Exception e){
  45. System.out.println(e.getMessage());
  46. continue;
  47. }
  48. while(flag_11){
  49. System.out.println("请输入密码:");
  50. psd = input.next();
  51. try{
  52. cheekUserSetPassword(psd);
  53. }
  54. catch(Exception e){
  55. System.out.println(e.getMessage());
  56. continue;
  57. }
  58. flag_11 = false;
  59. }
  60. while(flag_12){
  61. System.out.println("确认密码:");
  62. try{
  63. cheekUserSetPassword(psd,input.next());
  64. }
  65. catch(Exception e){
  66. System.out.println(e.getMessage());
  67. continue;
  68. }
  69. flag_12 = false;
  70. }
  71. users[num++] = new User(uname,psd);
  72. System.out.println("创建用户"+users[num-1].getUsername()+"成功!");
  73. System.out.println("是否继续创建用户?(1/是 0/否)");
  74. if(input.nextInt() == 0){
  75. flag_1 = false;
  76. }
  77. }
  78. }
  79. public static void function2(User[] users,Scanner input,boolean flag_2){ //实现功能2
  80. while(flag_2)
  81. {
  82. System.out.println("请输入用户名:");
  83. String uname = input.next();
  84. try{
  85. cheekUsername(uname);
  86. }
  87. catch(Exception e){
  88. System.out.println(e.getMessage());
  89. continue;
  90. }
  91. System.out.println("请输入密码:");
  92. String psd = input.next();
  93. try{
  94. cheekUserPassword(users,uname,psd);
  95. }
  96. catch(Exception e){
  97. System.out.println(e.getMessage());
  98. continue;
  99. }
  100. System.out.println("登录成功,欢迎"+uname+"回到LoginSystem!");
  101. flag_2 = false;
  102. }
  103. }
  104. public static void cheekUsername(String uname) throws UserLoginException{ //用户名是否符合要求,不符合抛出异常
  105. if(uname.length() < 6){
  106. throw new UserLoginException("用户名过短");
  107. }
  108. else if(uname.length() > 14){
  109. throw new UserLoginException("用户名过长");
  110. }
  111. }
  112. public static void cheekUserPassword(User[] users,String uname,String psd) throws UserLoginException{ //登录时密码是否符合要求,不符合抛出异常
  113. if(psd.length() > 16){
  114. throw new UserLoginException("密码输入过长");
  115. }
  116. if(psd.length() < 3){
  117. throw new UserLoginException("密码输入过短");
  118. }
  119. for(int i=0;i<num;i++){
  120. if(users[i].getUsername().equals(uname)){
  121. if(!users[i].getPassword().equals(psd)){
  122. throw new UserLoginException("密码错误");
  123. }
  124. else{
  125. return;
  126. }
  127. }
  128. }
  129. throw new UserLoginException("该用户不存在");
  130. }
  131. public static void cheekUserSetPassword(String psd) throws UserLoginException{ //创建用户时密码是否符合要求,不符合抛出异常
  132. if(psd.length() < 3){
  133. throw new UserLoginException("密码过短");
  134. }
  135. else if(psd.length() > 16){
  136. throw new UserLoginException("密码过长");
  137. }
  138. }
  139. public static void cheekUserSetPassword(String psd,String enpsd) throws UserLoginException{ //创建用户时确认密码是否符合要求,不符合抛出异常
  140. if(enpsd.length() < 3){
  141. throw new UserLoginException("密码过短");
  142. }
  143. else if(enpsd.length() > 16){
  144. throw new UserLoginException("密码过长");
  145. }
  146. else if(!psd.equals(enpsd)){
  147. throw new UserLoginException("两次输入密码不一致");
  148. }
  149. }
  150. public static void usersShow(User[] users){ //查看所有用户名
  151. for(int i=0;i<num;i++){
  152. System.out.println("用户"+(i+1)+":"+users[i].getUsername());
  153. }
  154. }
  155. }

用户类 

  1. package com.userlogin;
  2. public class User {
  3. public String username;
  4. public String password;
  5. public User(String username,String password){
  6. this.username = username;
  7. this.password = password;
  8. }
  9. public String getUsername(){
  10. return username;
  11. }
  12. public String getPassword(){
  13. return password;
  14. }
  15. }

用户登录异常类 

  1. package com.userlogin;
  2. public class UserLoginException extends Exception{
  3. public UserLoginException(){
  4. }
  5. public UserLoginException(String s){
  6. super(s);
  7. }
  8. }

运行结果: 

总结:

这个只是一个用户登录系统的雏形,还有很多的地方等待完善

比如用户创建后的数据应该保留在一个文件内,还有可以查看所有用户的用户名应该只有超级管理员才能看,剩下的就是一些代码优化和系统功能的扩充



所属网站分类: 技术文章 > 博客

作者:哦哦好吧

链接:http://www.javaheidong.com/blog/article/222404/0035e40c9bb407543b87/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)