当前位置:   article > 正文

JAVA基础:学生管理系统_java基础学生管理系统

java基础学生管理系统

 

借鉴黑马程序员java经典入门

1.创建学生类:

  1. package project1StudentManager;
  2. public class student {
  3. private String id;
  4. private String name;
  5. private String age;
  6. private String address;
  7. //ALT+INS
  8. public student() {
  9. }
  10. public student(String id, String name, String age, String address) {
  11. this.id = id;
  12. this.name = name;
  13. this.age = age;
  14. this.address = address;
  15. }
  16. public String getId() {
  17. return id;
  18. }
  19. public void setId(String 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 String getAge() {
  29. return age;
  30. }
  31. public void setAge(String 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. }

2.创建学生测试类:

  1. package project1StudentManager;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4. public class studentmanager {
  5. public static void main(String[] args) {
  6. ArrayList<student> array = new ArrayList<student>();
  7. while (true) {
  8. System.out.println("--------欢迎使用学生管理系统--------");
  9. System.out.println("1.添加学生信息");
  10. System.out.println("2.删除学生信息");
  11. System.out.println("3.修改学生信息");
  12. System.out.println("4.查看某个学生信息");
  13. System.out.println("5.查看所有学生信息");
  14. System.out.println("6.退出");
  15. System.out.println("请输入你要选择的功能编号:");
  16. Scanner sc = new Scanner(System.in);
  17. int i = sc.nextInt();
  18. if (i == 1){
  19. addStudent(array);
  20. }else if (i == 2){
  21. deleStudent(array);
  22. }else if (i == 3){
  23. modifyStudent(array);
  24. }else if (i == 4){
  25. seeStudent(array);
  26. }else if (i == 5){
  27. findallStudent(array);
  28. }
  29. else if (i == 6){
  30. System.out.println("谢谢使用");
  31. break;
  32. }else {
  33. System.out.println("你输入的编号有误,请重新输入");
  34. }
  35. }
  36. }
  37. //添加学生信息
  38. public static void addStudent(ArrayList<student> array){
  39. Scanner sc = new Scanner(System.in);
  40. System.out.println("请输入学生学号:");
  41. String id = sc.nextLine();
  42. int index = -1;
  43. for (int i = 0;i < array.size();i++){
  44. student s = array.get(i);
  45. if(s.getId().equals(id));
  46. index = 1;
  47. System.out.println("学号重复");
  48. break;
  49. }
  50. if (index ==-1){
  51. System.out.println("请输入学生姓名:");
  52. String name = sc.nextLine();
  53. System.out.println("请输入学生年龄:");
  54. String age = sc.nextLine();
  55. System.out.println("请输入学生地址:");
  56. String address = sc.nextLine();
  57. student s = new student();
  58. s.setId(id);
  59. s.setName(name);
  60. s.setAge(age);
  61. s.setAddress(address);
  62. array.add(s);
  63. System.out.println("添加学生信息成功");
  64. }
  65. }
  66. //通过学生学号删除学生信息
  67. public static void deleStudent(ArrayList<student> array){
  68. Scanner sc = new Scanner(System.in);
  69. System.out.println("请输入学生学号:");
  70. String id = sc.nextLine();
  71. int index = -1;
  72. for (int i = 0;i < array.size();i++){
  73. student s = array.get(i); //遍历列表,得到所有学生信息
  74. if(s.getId().equals(id)){
  75. index = 1;
  76. array.remove(i);
  77. System.out.println("删除学生信息成功");
  78. }
  79. }
  80. if (index == -1){
  81. System.out.println("你输入的学号有误");
  82. }
  83. }
  84. //通过学生学号修改学生信息-------
  85. public static void modifyStudent(ArrayList<student> array){
  86. Scanner sc = new Scanner(System.in);
  87. System.out.println("请输入你要修改的学生学号:");
  88. String id = sc.nextLine();
  89. int index = -1;
  90. for (int i = 0;i < array.size();i++){
  91. student s = array.get(i);
  92. if(s.getId().equals(id)){
  93. index = 1;
  94. System.out.println("请输入新的学生姓名:");
  95. String name = sc.nextLine();
  96. System.out.println("请输入新的学生年龄:");
  97. String age = sc.nextLine();
  98. System.out.println("请输入新的学生地址:");
  99. String address = sc.nextLine();
  100. //创建学生对象
  101. student s1 = new student();
  102. s1.setId(id);
  103. s1.setName(name);
  104. s1.setAge(age);
  105. s1.setAddress(address);
  106. array.set(i,s1); //修改array数组第i个位置的数据
  107. System.out.println("修改学生信息成功");
  108. break;
  109. }
  110. }
  111. if(index == -1) {
  112. System.out.println("你输入的学号有误");
  113. }
  114. }
  115. //通过学号查看学生信息
  116. public static void seeStudent(ArrayList<student> array){
  117. Scanner sc = new Scanner(System.in);
  118. System.out.println("请输入你要查看的学生学号");
  119. String id = sc.nextLine();
  120. int index = -1; //判断输入的学号是否有误
  121. for (int i = 0;i < array.size();i++){
  122. student s = array.get(i);
  123. if(s.getId().equals(id)){
  124. index = 1;
  125. System.out.println("学号\t\t\t\t姓名\t\t年龄\t\t地址");
  126. System.out.println(s.getId()+"\t"+s.getName()+"\t\t"+s.getAge()+"\t\t"+s.getAddress());
  127. break;
  128. }
  129. }
  130. if (index ==-1){
  131. System.out.println("你输入的学号有误");
  132. }
  133. }
  134. //查看所有学生信息
  135. public static void findallStudent(ArrayList<student> array){
  136. if(array.size() == 0){
  137. System.out.println("无学生信息,请先添加再查看");
  138. }else {
  139. System.out.println("学号\t\t\t\t姓名\t\t年龄\t\t地址");
  140. for (int i = 0;i < array.size();i++){
  141. student s = array.get(i);
  142. System.out.println(s.getId()+"\t"+s.getName()+"\t\t"+s.getAge()+"\t\t"+s.getAddress());
  143. }
  144. }
  145. }
  146. }

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

闽ICP备14008679号