1.代码涉及知识点
-
一个标准JavaBean类,一个测试类
-
增强switch
-
集合Arraylist及其常用方法
-
equal方法
2.项目大纲
需求:
分析:
初始菜单:
学生类:
添加功能:
删除功能:
修改功能:
查询功能:
3.项目需求:
采取控制台的方式
4.标准的JavaBean类——学生类
-
首先要定义一个Student类,在该类中,可以存储学生的属性,也就是基本信息。
-
属性用private修饰符修饰(将数据封装起来,保证数据的安全性和完整性。当一个属性被声明为private时,只有在同一个类中的方法才能直接访问该属性,如果其他类想要访问该属性,只能通过该类提供的公共方法进行访问)
-
构造器用于初始化对象的属性值,Getter方法用于获取对象的属性值,Setter方法用于设置对象的属性值。(Getter和Setter方法是该类提供的公共方法,用于访问私有属性)
public class Student { private String id; private String name; private int age; private String adress; public Student() { } public Student(String id, String name, int age, String adress) { this.id = id; this.name = name; this.age = age; this.adress = adress; } /** * 获取 * @return id */ public String getId() { return id; } /** * 设置 * @param id */ public void setId(String id) { this.id = id; } /** * 获取 * @return name */ public String getName() { return name; } /** * 设置 * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * @return age */ public int getAge() { return age; } /** * 设置 * @param age */ public void setAge(int age) { this.age = age; } /** * 获取 * @return adress */ public String getAdress() { return adress; } /** * 设置 * @param adress */ public void setAdress(String adress) { this.adress = adress; } public String toString() { return "Student{id = " + id + ", name = " + name + ", age = " + age + ", adress = " + adress + "}"; } }
5.初始菜单(主方法)及退出
分析:在主方法中,先打印初始菜单,然后使用switch让用户输入选择。因为用户可能不止使用一次该项目或者不止选择一种功能,故使用while循环。
剖析难点:
-
ArrayList<Student> list = new ArrayList<>();这句代码是用来创建一个名为list的ArrayList对象,可以用来存储多个Student对象。在学生管理系统中,这个ArrayList会被用于存储学生信息,包括学生的ID、姓名、年龄和地址等属性。通过向集合ArrayList中添加、删除、修改和查询学生信息,实现对学生管理系统的各项功能操作。
-
ArrayList<Student>:声明了一个ArrayList对象,其中<Student>表示该ArrayList只能存储Student类型的数据。
-
list:变量名,可以根据需要自行命名。
-
new ArrayList<>():使用new关键字创建一个ArrayList实例,ArrayList<>中的尖括号表示ArrayList中存储的对象类型,这里留空表示不限制类型
-
-
addStudent,deleteStudent,amendStudent,inquireStudent皆为方法,分别负责实现添加,删除,修改,查询功能。(如果将代码都写在主函数里,会比较复杂,采用调用函数的方法会好一点)
-
System.exit(0);
停止虚拟机运行(使用break只能跳出switch语句,并不能跳出while循环,而且当用户输入5退出时,就要结束这个程序,所以可以采取使虚拟机停止运行)
-
“->”增强switch。增强的switch具有返回值,整个语句可以作为一个表达式,在不同的情况下返回不同的值,而不再只能是不同情况下执行不同的语句。
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
while (true) {
System.out.println("----------欢迎来到学生管理系统----------");
System.out.println("1.添加学生");
System.out.println("2.删除学生");
System.out.println("3.修改学生");
System.out.println("4.查询学生");
System.out.println("5.退出");
System.out.println("请输入您的选择");
Scanner input = new Scanner(System.in);
String choice = input.next(); //nextInt只能录入一个数字,
switch (choice) {
case ("1") -> addStudent(list);
case ("2") -> deleteStudent(list);
case ("3") -> amendStudent(list);
case ("4") -> inquireStudent(list);
case ("5") -> {
System.out.println("退出");
System.exit(0); //停止虚拟机运行
}
default -> System.out.println("没有这个选项");
}
}
}
6.方法
1.getIndex方法
分析:用于判断id是否唯一,在添加,删除,修改方法中会调用
剖析难点:
-
在 Java 中,
equals
是用于比较两个对象是否相等的方法。对于字符串(String
类),equals
方法被用于比较字符串的内容是否相同。
public static int getIndex(ArrayList<Student>list,String id){
for (int i = 0; i < list.size(); i++) {
//获取集合里的每一个对象
Student student=list.get(i);
//获取每一个对象的id
String sid=student.getId();
if(sid.equals(id)){
//如果相同,则返回索引
return i;
}
}
//当循环执行到这里,说明不存在相同的id,返回值-1
return -1;
}
}
2.添加功能:addStudent方法
分析:先创建一个Student对象,用于存储新添加的学生信息,由于要求为id唯一,所以调用方法getIndex,判断id是否存在,若存在,进入循环,若不存在,给创建的Student对象设置id属性为用户的id。
剖析难点:
-
给创建的Student对象设置name属性为用户的name
student.setName(name);
-
是一个ArrayList类的方法,将创建的
Student
对象student
(对象名)添加到ArrayList
类型的列表list
中。list.add(student);
public static void addStudent(ArrayList<Student> list){
Scanner in = new Scanner(System.in);
Student student = new Student();
while (true) {
System.out.println("请输入学生的id");
String id= in.next();
int index=getIndex(list,id);
if(index==-1){
student.setId(id);
break;
}else{
System.out.println("id已存在,请重新录入");
}
}
System.out.println("请输入学生的姓名");
String name=in.next();
student.setName(name);
System.out.println("请输入学生的年龄");
int age=in.nextInt();
student.setAge(age);
System.out.println("请输入学生的住址");
String adress=in.next();
student.setAdress(adress);
list.add(student);
System.out.println("学生信息添加成功");
}
3.删除功能:deleteStudent方法
分析:先让用户输入要删除的id,再调用getIndex方法,判断id是否存在。若存在,则利用集合Arraylist中的方法remove删除;若不存在,提示并返回初始菜单。
剖析难点:
-
一个ArrayList类的方法,用于从列表中删除指定索引位置上的元素。在这个方法中,它将会删除名为
list
的ArrayList对象中的第index
个元素。list.remove(index);
public static void deleteStudent(ArrayList<Student>list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入要删除的id");
String id=in.next();
int index=getIndex(list,id);
if(index>0){
list.remove(index);
System.out.println("id为:"+id+"的学生删除成功");
}else{
System.out.println("id不存在,删除失败");
}
}
4.修改功能:amendStudent方法
分析:先让用户输入要修改的id,再调用getIndex函数,判断id是否唯一。若存在相同id,则先获取要修改id的对象,再对其其他的信息进行修改;若不存在,则进行提示,并返回菜单。
剖析难点:
-
因为不存在,要回到初始菜单,即用return(使用
return
语句可以立即结束当前函数的执行,回到调用该函数的地方,从而达到回到初始菜单的效果)
public static void amendStudent(ArrayList<Student>list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入要修改的id");
String id=in.next();
int index=getIndex(list,id);
if(index==-1){
System.out.println("要修改的id"+id+"不存在,请重新输入");
return;
}
//当代码执行到这里时,说明id是存在的
//获取要修改id的对象
Student student=list.get(index);
//输入要修改的信息
System.out.println("请输入要修改的学生姓名");
String newName= in.next();
student.setName(newName);
System.out.println("请输入要修改学生年龄");
int newAge=in.nextInt();
student.setAge(newAge);
System.out.println("请输入要修改学生住址");
String newAdress=in.next();
student.setAdress(newAdress);
System.out.println("学生信息修改成功");
}
5.查询功能:inquireStudent方法
分析:先利用Arraylist中的size方法判断集合中是否有对象,若有,先打印表头信息,再利用循环打印集合中的每一个对象
剖析知识点:
-
一个
ArrayList
类的方法,用于获取该列表中元素的数量(即列表的大小)。它返回的是一个整数值,表示列表中元素的个数
list.size()
public static void inquireStudent(ArrayList<Student>list) {
if(list.size()==0){
System.out.println("当前无学生信息,请添加后在查询");
return;
}
//打印表头
System.out.println(" id \t姓名 \t年龄 家庭住址");
for (int i = 0; i < list.size(); i++) {
Student student=list.get(i);
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAdress());
}
}
7.测试类完整代码
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
while (true) { //Ctrl+Alt+t
System.out.println("----------欢迎来到学生管理系统----------");
System.out.println("1.添加学生");
System.out.println("2.删除学生");
System.out.println("3.修改学生");
System.out.println("4.查询学生");
System.out.println("5.退出");
System.out.println("请输入您的选择");
Scanner input =new Scanner(System.in);
String choice=input.next();//nextInt只能录入一个数字,
switch (choice){
case("1")->addStudent(list);
case("2")->deleteStudent(list);
case("3")->amendStudent(list);
case("4")->inquireStudent(list);
case("5")->{
System.out.println("退出");
System.exit(0);//停止虚拟机运行
}
default->System.out.println("没有这个选项");
}
}
}
public static void addStudent(ArrayList<Student> list){
Scanner in = new Scanner(System.in);
Student student=new Student();
while (true) {
System.out.println("请输入学生id");
String id=in.next();
int index=getIndex(list,id);
if(index==-1){
student.setId(id);
break;
}else{
System.out.println("id已经存在,请重新录入");
}
}
System.out.println("请输入学生姓名");
String name=in.next();
student.setName(name);
System.out.println("请输入学生年龄");
int age=in.nextInt();
student.setAge(age);
System.out.println("请输入学生家庭住址");
String address=in.next();
student.setAdress(address);
list.add(student);
}
public static void deleteStudent(ArrayList<Student>list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入要删除的id");
String id=in.next();
int index=getIndex(list,id);
if(index>0){
list.remove(index);
System.out.println("id为:"+id+"的学生删除成功");
}else{
System.out.println("id不存在,删除失败");
}
}
public static void amendStudent(ArrayList<Student>list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入要修改的id");
String id=in.next();
int index=getIndex(list,id);
if(index==-1){
System.out.println("要修改的id"+id+"不存在,请重新输入");
return;
}
Student student=list.get(index);
System.out.println("请输入要修改的学生姓名");
String newName= in.next();
student.setName(newName);
System.out.println("请输入要修改学生年龄");
int newAge=in.nextInt();
student.setAge(newAge);
System.out.println("请输入要修改学生住址");
String newAdress=in.next();
student.setAdress(newAdress);
System.out.println("学生信息修改成功");
}
public static void inquireStudent(ArrayList<Student>list) {
if(list.size()==0){
System.out.println("当前无学生信息,请添加后在查询");
return;
}
System.out.println(" id \t姓名 \t年龄 家庭住址");
for (int i = 0; i < list.size(); i++) {
Student student=list.get(i);
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAdress());
}
}
public static int getIndex(ArrayList<Student>list,String id){
for (int i = 0; i < list.size(); i++) {
Student student=list.get(i);
String sid=student.getId();
if(sid.equals(id)){
return i;
}
}
return -1;
}
}