当前位置:   article > 正文

JAVA入门——简单的学生管理系统_java基础学生管理系统

java基础学生管理系统

一、管理员登录界面:

1.登录:

(1)用户名:

                若未注册,提示先注册,返回主界面

(2)密码:

                输入密码

                生成验证码并验证验证码

                最后判断密码是否正确,密码错误3次返回主界面

2.注册:

(1)用户名:

                用户名唯一,长度3~15

                用户名只能由大小写字母或数字组成,不能是纯数字

(2)身份证:

                身份证必须为18位,首位不为0

                前17位必须是数字,末尾为数字或‘x’或‘X’

(3)手机号:

                手机号必须为11位,首位不为0

(4)设置密码:

                需二次输入验证

3.忘记密码:

(1)验证信息:

                输入身份证及手机号,任何一个错误提示信息不匹配,返回主界面

(2)重新设置密码:

                需二次验证

4.退出:

(1)退出程序

二、学生管理系统(登录成功方可进入)

1.添加学生:

(1)学生id唯一
(2)学生属性:id,姓名,年龄,地址

2.根据id删除学生,若没有学生提示无学生信息,请先添加

3.根据id修改学生,若没有学生提示无学生信息,请先添加

4.根据id查询学生,若没有学生提示无学生信息,请先添加

5.遍历所有学生,若没有学生提示无学生信息,请先添加

6.退出

        (1)返回登录界面

话不多说,直接放代码

 管理员类:

  1. //管理员类:
  2. package project;
  3. public class StudentAdmin {
  4. private String username;
  5. private String password;
  6. private String idNum;
  7. private String phone;
  8. public StudentAdmin(){
  9. }
  10. public StudentAdmin(String username,String password,String idNum,String phone){
  11. this.username=username;
  12. this.password=password;
  13. this.idNum=idNum;
  14. this.phone=phone;
  15. }
  16. public String getUsername() {
  17. return username;
  18. }
  19. public void setUsername(String username) {
  20. this.username = username;
  21. }
  22. public String getPassword() {
  23. return password;
  24. }
  25. public void setPassword(String password) {
  26. this.password = password;
  27. }
  28. public String getIdNum() {
  29. return idNum;
  30. }
  31. public void setIdNum(String idNum) {
  32. this.idNum = idNum;
  33. }
  34. public String getPhone() {
  35. return phone;
  36. }
  37. public void setPhone(String phone) {
  38. this.phone = phone;
  39. }
  40. }

学生类:

  1. //学生类:
  2. package project;
  3. public class Student {
  4. private int id;
  5. private String name;
  6. private int age;
  7. private String address;
  8. public Student() {
  9. }
  10. public Student(int id, String name, int age, String address) {
  11. this.id = id;
  12. this.name = name;
  13. this.age = age;
  14. this.address = address;
  15. }
  16. public int getId() {
  17. return id;
  18. }
  19. public void setId(int id) {
  20. this.id = id;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public int getAge() {
  29. return age;
  30. }
  31. public void setAge(int age) {
  32. this.age = age;
  33. }
  34. public String getAddress() {
  35. return address;
  36. }
  37. public void setAddress(String address) {
  38. this.address = address;
  39. }
  40. }

  1. package project;
  2. import java.util.ArrayList;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5. public class StudentSystem {
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. ArrayList<StudentAdmin> list = new ArrayList<>();
  9. while (true) {
  10. System.out.println("---------------------欢迎来到学生管理系统---------------------");
  11. System.out.println("---------------------------1.登录--------------------------");
  12. System.out.println("---------------------------2.注册--------------------------");
  13. System.out.println("--------------------------3.忘记密码------------------------");
  14. System.out.println("---------------------------4.退出--------------------------");
  15. System.out.println("请输入选择:");
  16. String choose = sc.next();
  17. switch (choose) {
  18. case "1":
  19. boolean login = login(list);
  20. if (login) {
  21. system();
  22. }
  23. break;
  24. case "2":
  25. register(list);
  26. break;
  27. case "3":
  28. forgetPassword(list);
  29. for (int i = 0; i < list.size(); i++) {
  30. StudentAdmin sa = list.get(i);
  31. System.out.println(sa.getUsername() + " " + sa.getPassword() + " " + sa.getIdNum() + " " + sa.getPhone());
  32. }
  33. break;
  34. case "4":
  35. System.out.println("谢谢使用,再见");
  36. System.exit(0);
  37. default:
  38. System.out.println("没有该选项");
  39. break;
  40. }
  41. }
  42. }
  43. public static boolean login(ArrayList<StudentAdmin> list) {
  44. Scanner sc = new Scanner(System.in);
  45. System.out.println("---------------------------登录中--------------------------");
  46. int index;
  47. System.out.println("请输入用户名:");
  48. String username = sc.next();
  49. if (getAdminIndex(list, username) == -1) {
  50. System.out.println("用户名不存在,请先注册");
  51. return false;
  52. } else {
  53. index = getAdminIndex(list, username);
  54. }
  55. System.out.println("请输入密码:");
  56. for (int i = 0; i < 3; i++) {
  57. String password = sc.next();
  58. while (true) {
  59. String yzm = yzm();
  60. System.out.println("请输入验证码:");
  61. String ipYzm = sc.next();
  62. if (!yzm.equalsIgnoreCase(ipYzm)) {
  63. System.out.println("验证失败,请重新验证");
  64. } else {
  65. break;
  66. }
  67. }
  68. if (!list.get(index).getPassword().equals(password)) {
  69. if (i == 2) {
  70. System.out.println("登录失败");
  71. return false;
  72. } else {
  73. System.out.println("密码错误,剩余" + (2 - i) + "次机会");
  74. }
  75. } else {
  76. System.out.println("登录成功");
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. public static ArrayList<StudentAdmin> register(ArrayList<StudentAdmin> list) {
  83. Scanner sc = new Scanner(System.in);
  84. StudentAdmin sa = new StudentAdmin();
  85. char num[]={'0','1','2','3','4','5','6','7','8','9'};
  86. char abc[] = new char[52];
  87. for (int i = 0; i < abc.length; i++) {
  88. if (i < 26) {
  89. abc[i] = (char) (97 + i);
  90. } else {
  91. abc[i] = (char) (65 + i - 26);
  92. }
  93. }
  94. System.out.println("---------------------------注册中--------------------------");
  95. System.out.println("请输入用户名:");
  96. while (true) {
  97. int numCnt=0;
  98. boolean con=true;
  99. String username = sc.next();
  100. for (int i = 0; i < username.length(); i++) {
  101. boolean abcCon=contain(username.charAt(i),abc);
  102. boolean numCon=contain(username.charAt(i),num);
  103. if (numCon){
  104. numCnt++;
  105. }
  106. if ((!abcCon) && (!numCon)){
  107. con=false;
  108. }
  109. }
  110. if (getAdminIndex(list, username) >= 0) {
  111. System.out.println("该用户名已存在,请重新输入:");
  112. } else if (username.length() < 3 || username.length() > 15) {
  113. System.out.println("用户名长度应为3~15位,请重新输入:");
  114. } else if (!con) {
  115. System.out.println("用户名只能由大小写字母及数字组成,请重新输入:");
  116. }else if (numCnt==username.length()){
  117. System.out.println("用户名不能是纯数字,请重新输入:");
  118. }else {
  119. sa.setUsername(username);
  120. System.out.println("设置成功");
  121. break;
  122. }
  123. }
  124. System.out.println("请设置密码:");
  125. while (true) {
  126. String p1 = sc.next();
  127. System.out.println("请再次确认:");
  128. String p2 = sc.next();
  129. if (!p1.equals(p2)) {
  130. System.out.println("两次输入不一致,请重新设置:");
  131. } else {
  132. sa.setPassword(p1);
  133. System.out.println("设置成功");
  134. break;
  135. }
  136. }
  137. System.out.println("验证身份证:");
  138. while (true) {
  139. boolean numCon=true;
  140. boolean endCon=false;
  141. String idNum = sc.next();
  142. String idSub=idNum.substring(0,17);
  143. char idEnd=idNum.charAt(17);
  144. for (int i = 0; i < idSub.length(); i++) {
  145. if (!contain(idSub.charAt(i),num)){
  146. numCon=false;
  147. }
  148. }
  149. if ((idEnd>='0' && idEnd<='9') || idEnd=='X' || idEnd=='x'){
  150. endCon=true;
  151. }
  152. if (idNum.length() != 18) {
  153. System.out.println("身份证应为18位,请重新输入:");
  154. } else if (idNum.startsWith("0")) {
  155. System.out.println("身份证首位不为0.请重新输入:");
  156. }else if (!numCon || !endCon){
  157. System.out.println("身份证前17位必须为纯数字,最后一位为数字或大写X或小写x,请重新输入:");
  158. }else {
  159. sa.setIdNum(idNum);
  160. System.out.println("验证成功");
  161. break;
  162. }
  163. }
  164. System.out.println("手机号验证:");
  165. while (true) {
  166. boolean numCon=true;
  167. String phone = sc.next();
  168. for (int i = 0; i < phone.length(); i++) {
  169. char c=phone.charAt(i);
  170. if(!(c>='0' && c<='9')){
  171. numCon=false;
  172. break;
  173. }
  174. }
  175. if (phone.length() != 11) {
  176. System.out.println("手机号应为11位,请重新输入:");
  177. } else if (phone.charAt(0) == '0') {
  178. System.out.println("手机号首位不为0.请重新输入:");
  179. }else if (!numCon){
  180. System.out.println("手机号应为纯数字,请重新输入:");
  181. }else {
  182. sa.setPhone(phone);
  183. System.out.println("验证成功");
  184. break;
  185. }
  186. }
  187. list.add(sa);
  188. System.out.println("注册成功");
  189. return list;
  190. }
  191. public static ArrayList<StudentAdmin> forgetPassword(ArrayList<StudentAdmin> list) {
  192. Scanner sc = new Scanner(System.in);
  193. System.out.println("---------------------------忘记密码--------------------------");
  194. int index;
  195. System.out.println("请输入用户名:");
  196. String username = sc.next();
  197. if (getAdminIndex(list, username) == -1) {
  198. System.out.println("用户名不存在,请先注册");
  199. return null;
  200. } else {
  201. index = getAdminIndex(list, username);
  202. }
  203. System.out.println("输入身份证:");
  204. String inIdNum = sc.next();
  205. System.out.println("输入手机号:");
  206. String inPhone = sc.next();
  207. StudentAdmin sa = list.get(index);
  208. boolean idNum = sa.getIdNum().equals(inIdNum);
  209. boolean phone = sa.getPhone().equals(inPhone);
  210. if (idNum && phone) {
  211. System.out.println("请输入密码");
  212. while (true) {
  213. String p1 = sc.next();
  214. System.out.println("请再次确认:");
  215. String p2 = sc.next();
  216. if (!p1.equals(p2)) {
  217. System.out.println("两次输入不一致,请重新设置:");
  218. } else {
  219. sa.setPassword(p1);
  220. list.set(index, sa);
  221. System.out.println("设置成功");
  222. return list;
  223. }
  224. }
  225. } else {
  226. System.out.println("信息不匹配,修改失败");
  227. return null;
  228. }
  229. }
  230. public static int getAdminIndex(ArrayList<StudentAdmin> list, String username) {
  231. for (int i = 0; i < list.size(); i++) {
  232. if (list.get(i).getUsername().equals(username)) {
  233. return i;
  234. }
  235. }
  236. return -1;
  237. }
  238. public static boolean contain(char c, char[] s) {
  239. for (int i = 0; i < s.length; i++) {
  240. if (s[i] == c) {
  241. return true;
  242. }
  243. }
  244. return false;
  245. }
  246. public static String yzm() {
  247. Random r = new Random();
  248. char abc[] = new char[4];
  249. for (int i = 0; i < abc.length; i++) {
  250. int index = r.nextInt(52);
  251. abc[i] = abc(index);
  252. }
  253. String zm = new String(abc);
  254. int sub = r.nextInt(abc.length);
  255. int ram = r.nextInt(10);
  256. String first = zm.substring(0, sub);
  257. String end = zm.substring(sub);
  258. StringBuilder sb = new StringBuilder();
  259. String yzm = sb.append(first).append(ram).append(end).toString();
  260. System.out.println(yzm);
  261. return yzm;
  262. }
  263. public static char abc(int index) {
  264. char abc[] = new char[52];
  265. for (int i = 0; i < abc.length; i++) {
  266. if (i < 26) {
  267. abc[i] = (char) (97 + i);
  268. } else {
  269. abc[i] = (char) (65 + i - 26);
  270. }
  271. }
  272. return abc[index];
  273. }
  274. public static void system() {
  275. Scanner sc = new Scanner(System.in);
  276. ArrayList<Student> list = new ArrayList<>();
  277. loop:
  278. while (true) {
  279. System.out.println("---------------------欢迎来到学生管理系统---------------------");
  280. System.out.println("-------------------------1.添加学生-------------------------");
  281. System.out.println("-------------------------2.删除学生-------------------------");
  282. System.out.println("-------------------------3.修改学生-------------------------");
  283. System.out.println("-------------------------4.查询学生-------------------------");
  284. System.out.println("-------------------------5.遍历学生-------------------------");
  285. System.out.println("-------------------------6.退出----------------------------");
  286. System.out.println("请输入选择:");
  287. String input = sc.next();
  288. switch (input) {
  289. case "1":
  290. addStudent(list);
  291. break;
  292. case "2":
  293. deleteStudent(list);
  294. break;
  295. case "3":
  296. updateStudent(list);
  297. break;
  298. case "4":
  299. searchStudent(list);
  300. break;
  301. case "5":
  302. showStudent(list);
  303. break;
  304. case "6":
  305. break loop;
  306. //System.exit(0); //停止虚拟机运行
  307. default:
  308. System.out.println("没有该选项");
  309. break;
  310. }
  311. }
  312. }
  313. public static ArrayList<Student> addStudent(ArrayList<Student> list) {
  314. Scanner sc = new Scanner(System.in);
  315. Student stu = new Student();
  316. System.out.println("请输入增加学生id:");
  317. while (true) {
  318. int id = sc.nextInt();
  319. int index = getIndex(list, id);
  320. if (index < 0) {
  321. stu.setId(id);
  322. break;
  323. } else {
  324. System.out.println("该id已存在,请重新输入:");
  325. }
  326. }
  327. System.out.println("请输入增加学生姓名:");
  328. stu.setName(sc.next());
  329. System.out.println("请输入增加学生年龄:");
  330. stu.setAge(sc.nextInt());
  331. System.out.println("请输入增加学生地址:");
  332. stu.setAddress(sc.next());
  333. list.add(stu);
  334. System.out.println("添加成功");
  335. return list;
  336. }
  337. public static ArrayList<Student> deleteStudent(ArrayList<Student> list) {
  338. if (list.size() != 0) {
  339. Scanner sc = new Scanner(System.in);
  340. System.out.println("请输入删除学生id:");
  341. while (true) {
  342. int id = sc.nextInt();
  343. int index = getIndex(list, id);
  344. if (index >= 0) {
  345. list.remove(index);
  346. System.out.println("删除成功");
  347. break;
  348. } else {
  349. System.out.println("该id不存在,请重新输入:");
  350. }
  351. }
  352. } else {
  353. System.out.println("当前无学生信息,请添加后删除");
  354. }
  355. return list;
  356. }
  357. public static int getIndex(ArrayList<Student> list, int id) {
  358. for (int i = 0; i < list.size(); i++) {
  359. if (id == list.get(i).getId()) {
  360. return i;
  361. }
  362. }
  363. return -1;
  364. }
  365. public static ArrayList<Student> updateStudent(ArrayList<Student> list) {
  366. if (list.size() != 0) {
  367. Scanner sc = new Scanner(System.in);
  368. Student stu = new Student();
  369. System.out.println("请输入修改学生id:");
  370. while (true) {
  371. int id = sc.nextInt();
  372. int index = getIndex(list, id);
  373. if (index >= 0) {
  374. stu.setId(id);
  375. System.out.println("请输入修改后的姓名:");
  376. stu.setName(sc.next());
  377. System.out.println("请输入修改后的年龄:");
  378. stu.setAge(sc.nextInt());
  379. System.out.println("请输入修改后的地址:");
  380. stu.setAddress(sc.next());
  381. list.set(index, stu);
  382. System.out.println("修改成功");
  383. break;
  384. } else {
  385. System.out.println("该id不存在,请重新输入:");
  386. }
  387. }
  388. } else {
  389. System.out.println("当前无学生信息,请添加后修改");
  390. }
  391. return list;
  392. }
  393. public static void searchStudent(ArrayList<Student> list) {
  394. if (list.size() != 0) {
  395. Scanner sc = new Scanner(System.in);
  396. System.out.println("请输入查询学生id:");
  397. while (true) {
  398. int id = sc.nextInt();
  399. int index = getIndex(list, id);
  400. if (index >= 0) {
  401. Student stu = list.get(index);
  402. System.out.println(" " + stu.getId() + '\t' + '\t' + stu.getName() + '\t' + '\t' + stu.getAge() + '\t' + '\t' + stu.getAddress());
  403. break;
  404. } else {
  405. System.out.println("id不存在,请重新输入:");
  406. }
  407. }
  408. } else {
  409. System.out.println("当前无学生信息,请添加后查询");
  410. }
  411. }
  412. public static void showStudent(ArrayList<Student> list) {
  413. if (list.size() != 0) {
  414. System.out.println(" " + "id" + '\t' + '\t' + "姓名" + '\t' + '\t' + "年龄" + '\t' + '\t' + "地址");
  415. for (int i = 0; i < list.size(); i++) {
  416. Student stu = list.get(i);
  417. System.out.println(" " + stu.getId() + '\t' + '\t' + stu.getName() + '\t' + '\t' + stu.getAge() + '\t' + '\t' + stu.getAddress());
  418. }
  419. } else {
  420. System.out.println("当前无学生信息,请添加后遍历");
  421. }
  422. }
  423. }

OVER~~ 


--------------------------------------------------分----------割----------线--------------------------------------------------

发现一个小bug:

身份证验证中,由于为后续判断前17位及末尾 提前进行了字符串分割,导致当输入小于18时,无法截取到17位而报错,这里的解决方法是将判断位数调整至外层if判断,else执行字符串分割及其他判断。

此处将身份证验证代码进行修改:

  1. //验证身份证终板:
  2. System.out.println("验证身份证:");
  3. while (true) {
  4. boolean numCon=true;
  5. boolean endCon=false;
  6. String idNum = sc.next();
  7. if (idNum.length() != 18) {
  8. System.out.println("身份证应为18位,请重新输入:");
  9. }else {
  10. String idSub=idNum.substring(0,17);
  11. char idEnd=idNum.charAt(17);
  12. for (int i = 0; i < idSub.length(); i++) {
  13. if (!contain(idSub.charAt(i),num)){
  14. numCon=false;
  15. }
  16. }
  17. if ((idEnd>='0' && idEnd<='9') || idEnd=='X' || idEnd=='x'){
  18. endCon=true;
  19. }
  20. if (idNum.startsWith("0")) {
  21. System.out.println("身份证首位不为0.请重新输入:");
  22. }else if (!numCon || !endCon){
  23. System.out.println("身份证前17位必须为纯数字,最后一位为数字或大写X或小写x,请重新输入:");
  24. }else {
  25. sa.setIdNum(idNum);
  26. System.out.println("验证成功");
  27. break;
  28. }
  29. }
  30. }

 OVER~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/776744
推荐阅读
相关标签
  

闽ICP备14008679号