Java练手小项目——学生管理系统

简介: Java练手小项目——学生管理系统

学生类

1. package timberman666;
2. 
3. public class Student {
4. private String id;
5. private String name;
6. private int age;
7. private String address;
8. 
9. public Student() {
10.     }
11. 
12. public Student(String id, String name, int age, String address) {
13. this.id = id;
14. this.name = name;
15. this.age = age;
16. this.address = address;
17.     }
18. 
19. /**
20.      * 获取
21.      * @return id
22.      */
23. public String getId() {
24. return id;
25.     }
26. 
27. /**
28.      * 设置
29.      * @param id
30.      */
31. public void setId(String id) {
32. this.id = id;
33.     }
34. 
35. /**
36.      * 获取
37.      * @return name
38.      */
39. public String getName() {
40. return name;
41.     }
42. 
43. /**
44.      * 设置
45.      * @param name
46.      */
47. public void setName(String name) {
48. this.name = name;
49.     }
50. 
51. /**
52.      * 获取
53.      * @return age
54.      */
55. public int getAge() {
56. return age;
57.     }
58. 
59. /**
60.      * 设置
61.      * @param age
62.      */
63. public void setAge(int age) {
64. this.age = age;
65.     }
66. 
67. /**
68.      * 获取
69.      * @return address
70.      */
71. public String getAddress() {
72. return address;
73.     }
74. 
75. /**
76.      * 设置
77.      * @param address
78.      */
79. public void setAddress(String address) {
80. this.address = address;
81.     }
82. 
83. public String toString() {
84. return "Student{id = " + id + ", name = " + name + ", age = " + age + ", address = " + address + "}";
85.     }
86. }

菜单

1.  loop:while (true) {
2.             System.out.println("Welcome to the timberman's Student Management System");
3.             System.out.println("1:Add");
4.             System.out.println("2:Delete");
5.             System.out.println("3:Modify");
6.             System.out.println("4:Query");
7.             System.out.println("5:Quit");
8.             System.out.println("Please enter your choice");
9.             Scanner sc=new Scanner(System.in);
10.             String choose= sc.next();
11. switch (choose)
12.             {
13. case"1"-> addStudent(list);
14. case"2"-> deleteStudent(list);
15. case"3"-> modifyStudent(list);
16. case"4"-> queryStudent(list);
17. case"5"-> {
18.                     System.out.println("Quit");
19. break loop;
20.                 }
21. default -> System.out.println("There is no such option");
22.             }
23.         }

添加学生信息功能的实现

1. //add student
2. public static void addStudent(ArrayList<Student> list) {
3.         System.out.println("Add a student");
4.         Student s= new Student();
5.         Scanner sc=new Scanner(System.in);
6. 
7.         String id=null;
8. while (true) {
9.             System.out.println("Please enter the student's id");
10.             id=sc.next();
11. boolean flag=contains(list,id);
12. if(flag){
13.                 System.out.println("The ID already exists, please re-enter it");
14.             }
15. else{
16.                 s.setId(id);
17. break;
18.             }
19. 
20.         }
21. 
22.         System.out.println("Please enter the student's name");
23.         String name=sc.next();
24.         s.setName(name);
25. 
26.         System.out.println("Please enter the student's age");
27. int age= sc.nextInt();
28.         s.setAge(age);
29. 
30.         System.out.println("Please enter the student's address");
31. String address = sc.next();
32.         s.setAddress(address);
33. 
34.         list.add(s);
35. 
36.         System.out.println("Student information added successfully");
37.     }

删除学生信息功能的实现

1. //delete student
2. public static void deleteStudent(ArrayList<Student> list) {
3.         System.out.println("Delete a student");
4.         Scanner sc=new Scanner(System.in);
5.         String id=sc.next();
6. int index=getIndex(list,id);
7. if(index>=0){
8.             list.remove(index);
9.         }
10. else{
11.             System.out.println("id does not exist, deletion failed");
12.         }
13. 
14.     }

修改学生信息功能的实现

1. //modify student
2. public static void modifyStudent(ArrayList<Student> list) {
3.         System.out.println("Modify a student");
4.         Scanner sc=new Scanner(System.in);
5.         String id=sc.next();
6. int index=getIndex(list,id);
7. if(index==-1){
8.             System.out.println("The ID to be modified does not exist " +id+ " please re-enter it");
9. return;
10.         }
11.         Student stu=list.get(index);
12.         System.out.println("Please enter the name of the student you want to modify");
13.         String newName= sc.next();
14.         stu.setName(newName);
15. 
16.         System.out.println("Please enter the age of the student you want to modify");
17. int newAge= sc.nextInt();
18.         stu.setAge(newAge);
19. 
20.         System.out.println("Please enter the address of the student you want to modify");
21.         String newAddress= sc.next();
22.         stu.setAddress(newAddress);
23. 
24.         System.out.println("The student's information was modified successfully");
25.     }

查询学生信息的实现

1. //query student
2. public static void queryStudent(ArrayList<Student> list) {
3.         System.out.println("Query a student");
4. if(list.size()==0)
5.         {
6.             System.out.println("There is currently no student information, please add it and inquire");
7. return;
8.         }
9.         System.out.println("id\t\tname\tage\taddress");
10. for (int i = 0; i < list.size() ; i++) {
11.             Student stu=list.get(i);
12.             System.out.println(stu.getId()+"\t"+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
13. 
14.         }
15. 
16.     }

通过ID获得下标功能的实现

1. //Get the index by id
2. public static int getIndex(ArrayList<Student> list,String id) {
3. for (int i = 0; i < list.size(); i++) {
4.             Student stu=list.get(i);
5.             String sid= stu.getId();
6. if(sid.equals(id)) {
7. return i;
8.             }
9.         }
10. return -1;
11.     }

判断ID是否唯一功能的实现

1. //Determine whether the id exists in the collection
2. public static boolean contains(ArrayList<Student> list,String id) {
3. for (int i = 0; i < list.size(); i++) {
4.             Student s=list.get(i);
5.             String sid=s.getId();
6. if(sid.equals(id)){
7. return true;
8.             }
9.         }
10. return false;
11.     }
相关文章
|
7月前
|
设计模式 消息中间件 传感器
Java 设计模式之观察者模式:构建松耦合的事件响应系统
观察者模式是Java中常用的行为型设计模式,用于构建松耦合的事件响应系统。当一个对象状态改变时,所有依赖它的观察者将自动收到通知并更新。该模式通过抽象耦合实现发布-订阅机制,广泛应用于GUI事件处理、消息通知、数据监控等场景,具有良好的可扩展性和维护性。
538 8
|
7月前
|
移动开发 监控 小程序
java家政平台源码,家政上门清洁系统源码,数据多端互通,可直接搭建使用
一款基于Java+SpringBoot+Vue+UniApp开发的家政上门系统,支持小程序、APP、H5、公众号多端互通。涵盖用户端、技工端与管理后台,支持多城市、服务分类、在线预约、微信支付、抢单派单、技能认证、钱包提现等功能,源码开源,可直接部署使用。
506 24
|
7月前
|
安全 前端开发 Java
使用Java编写UDP协议的简易群聊系统
通过这个基础框架,你可以进一步增加更多的功能,例如用户认证、消息格式化、更复杂的客户端界面等,来丰富你的群聊系统。
279 11
|
7月前
|
机器学习/深度学习 人工智能 自然语言处理
Java与生成式AI:构建内容生成与创意辅助系统
生成式AI正在重塑内容创作、软件开发和创意设计的方式。本文深入探讨如何在Java生态中构建支持文本、图像、代码等多种生成任务的创意辅助系统。我们将完整展示集成大型生成模型(如GPT、Stable Diffusion)、处理生成任务队列、优化生成结果以及构建企业级生成式AI应用的全流程,为Java开发者提供构建下一代创意辅助系统的完整技术方案。
366 10
|
7月前
|
人工智能 监控 Java
Java与AI智能体:构建自主决策与工具调用的智能系统
随着AI智能体技术的快速发展,构建能够自主理解任务、制定计划并执行复杂操作的智能系统已成为新的技术前沿。本文深入探讨如何在Java生态中构建具备工具调用、记忆管理和自主决策能力的AI智能体系统。我们将完整展示从智能体架构设计、工具生态系统、记忆机制到多智能体协作的全流程,为Java开发者提供构建下一代自主智能系统的完整技术方案。
897 4
|
7月前
|
机器学习/深度学习 分布式计算 Java
Java与图神经网络:构建企业级知识图谱与智能推理系统
图神经网络(GNN)作为处理非欧几里得数据的前沿技术,正成为企业知识管理和智能推理的核心引擎。本文深入探讨如何在Java生态中构建基于GNN的知识图谱系统,涵盖从图数据建模、GNN模型集成、分布式图计算到实时推理的全流程。通过具体的代码实现和架构设计,展示如何将先进的图神经网络技术融入传统Java企业应用,为构建下一代智能决策系统提供完整解决方案。
610 0
|
8月前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
8月前
|
IDE 安全 Java
Lombok 在企业级 Java 项目中的隐性成本:便利背后的取舍之道
Lombok虽能简化Java代码,但其“魔法”特性易破坏封装、影响可维护性,隐藏调试难题,且与JPA等框架存在兼容风险。企业级项目应优先考虑IDE生成、Java Records或MapStruct等更透明、稳健的替代方案,平衡开发效率与系统长期稳定性。
489 115
|
8月前
|
存储 小程序 Java
热门小程序源码合集:微信抖音小程序源码支持PHP/Java/uni-app完整项目实践指南
小程序已成为企业获客与开发者创业的重要载体。本文详解PHP、Java、uni-app三大技术栈在电商、工具、服务类小程序中的源码应用,提供从开发到部署的全流程指南,并分享选型避坑与商业化落地策略,助力开发者高效构建稳定可扩展项目。
|
8月前
|
安全 Java API
Java Web 在线商城项目最新技术实操指南帮助开发者高效完成商城项目开发
本项目基于Spring Boot 3.2与Vue 3构建现代化在线商城,涵盖技术选型、核心功能实现、安全控制与容器化部署,助开发者掌握最新Java Web全栈开发实践。
727 1