- 目录
2.1StudentManagementSystemTest
1.设计过程
1.1类的划分
类名 | 属性 | 方法 | 说明 |
Student.java | 学生姓名 private String name | public String getName() | 获取Student类的name属性 |
public void setName(String name) | 设置Student类的name属性 | ||
StudentManagementSystem.java | 学生列表 private List<Student> students | public void addStudent(Student student) | 增添学生信息 |
public List<Student> getStudents | 获取学生信息 | ||
public List<Student> searchByName(String name) | 通过姓名搜索学生 |
Student类
package StudentManagementChange; public class Student { private String name; //初始化 public Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "name=" + name + '}'; } }
- StudentManagementSystem类
package StudentManagementChange; import java.util.ArrayList; import java.util.List; public class StudentManagementSystem { private List<Student> students; public StudentManagementSystem() { students = new ArrayList<>(); } public void addStudent(Student student) { students.add(student); } public List<Student> getStudents() { return students; } //通过姓名查询学生 public List<Student> searchByName(String name) { List<Student> result = new ArrayList<>(); for (Student student : students) { if (student.getName().equals(name)) { result.add(student); } } return result; } }
1.2功能的实现
- 添加学生(提示输入姓名后创建)
- 显示所有学生(使用良好的格式显示)
- 查询学生(输入姓名后查询学生)
- 退出
(1)添加学生
- 利用scanner读取学生的姓名
- 新建学生对象
- 利用addStudent方法将学生添加到学生列表中
private static void addStudents(Scanner scanner, StudentManagementSystem sms) { System.out.println("Enter student name:"); String name = scanner.next(); Student student = new Student(name); sms.addStudent(student); System.out.println("Student added successfully!\n"); }
(2)显示所有学生
- 利用getStudent方法获取学生信息
- 利用if-else判断语句,如果列表不为空则使用foreach循环输出学生信息
private static void showAllStudents(StudentManagementSystem sms) { List<Student> studentList = sms.getStudents(); if(studentList.size()==0){ System.out.println("The System Date is empty!"); } else{ for(Student studentItem:studentList){ System.out.println(studentItem.toString()); } } }
(3)查询学生
- 利用Scanner读取要查找的学生姓名
- 利用searchByName方法查找学生
- 利用if-else判断语句,如果searchResults列表不为空则遍历搜索结果,并打印每个符合条件学生的信息
private static void searchStudentsByName(Scanner scanner, StudentManagementSystem sms) { System.out.println("Enter student name to search:"); String searchName = scanner.next(); List<Student> searchResults = sms.searchByName(searchName); if(searchResults.isEmpty()){ System.out.println("No students found!"); } else{ System.out.println("Search results:"); for(Student s : searchResults){ System.out.println(s); } } }
(4)利用方法简化main
package StudentManagementChange; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); StudentManagementSystem sms = new StudentManagementSystem(); boolean running = true; while(running){ //菜单 System.out.println("菜单"); System.out.println("Enter 1 to add a student"); System.out.println("Enter 2 to show all students"); System.out.println("Enter 3 to search for a student by name"); System.out.println("Enter 4 to exit"); //输出选项 int choice = scanner.nextInt(); switch(choice){ //添加学生 case 1: addStudents(scanner, sms); break; //展示所有学生 case 2: showAllStudents(sms); break; //通过姓名查询学生 case 3: searchStudentsByName(scanner, sms); break; case 4 : running = false; System.out.println("Exit Successfully!"); break; default: System.out.println("Invalid choice!"); break; } } } private static void searchStudentsByName(Scanner scanner, StudentManagementSystem sms) { System.out.println("Enter student name to search:"); String searchName = scanner.next(); List<Student> searchResults = sms.searchByName(searchName); if(searchResults.isEmpty()){ System.out.println("No students found!"); } else{ System.out.println("Search results:"); for(Student s : searchResults){ System.out.println(s); } } } private static void showAllStudents(StudentManagementSystem sms) { List<Student> studentList = sms.getStudents(); if(studentList.size()==0){ System.out.println("The System Date is empty!"); } else{ for(Student studentItem:studentList){ System.out.println(studentItem.toString()); } } } private static void addStudents(Scanner scanner, StudentManagementSystem sms) { System.out.println("Enter student name:"); String name = scanner.next(); Student student = new Student(name); sms.addStudent(student); System.out.println("Student added successfully!\n"); } }
1.3基本语法
- public和private
- 利用方法初始化
(1)
public
和private
是 Java 中的访问修饰符,它们用于控制类、方法、变量等成员的可见性和可访问性。了解这些修饰符对于编写清晰、安全和易于维护的代码非常重要。public:用public修饰的方法和对象可以被任何地方访问,通常用于类的公共方法和属性,特别是那些需要被外部类访问的方法。
public class Student{ @Override public String toString() { return "Student{" + "name=" + name + '}'; } }
private:表示该成员只能被其所在的类访问。其他类无法直接访问这些成员,除非通过该类提供的公共方法(如 getter 和 setter)。主要用于封装类的内部状态,防止外部类直接修改或查看类的敏感数据。
public class Student{ private String name;//name属性 }
(2)利用方法初始化
Student类
private String name; //初始化 public Student(String name) { this.name = name; }
StudentManegementSystem类
private List<Student> students; public StudentManagementSystem() { students = new ArrayList<>(); }
2.代码功能展示
2.1StudentManagementSystemTest
package StudentManagementChange;
public class StudentManagementSystemTest {
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
Student s1 = new Student("John");
Student s2 = new Student("Mary");
Student s3 = new Student("Tom");
sms.addStudent(s1);
sms.addStudent(s2);
sms.addStudent(s3);
System.out.println("All Students:"+sms.getStudents());
System.out.println("Search for student by name:"+sms.searchByName("Mary"));
}
}
2.2main运行结果
标签:01,name,System,面向对象,Student,println,信息管理系统,public,out From: https://blog.csdn.net/2301_80122123/article/details/142291166(1)菜单
(2)添加学生
(3)显示所有学生
(4)查询学生
(5)退出