package exercise;
public class Student {
private String name;//姓名
private String id;//学号
private int age;//年龄
private String adress;//住址
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", age=" + age + ", adress=" + adress + "]";
}
}
package exercise;
import java.util.ArrayList;
import java.util.Scanner;
/**
* 学生管理系统
* 1.查看
* 2.删除
* 3.修改
* 4.增加
* 5.退出
利用集合完成对学生的增删改查四个功能
*/
public class Test01 {
public static void main(String[] args) {
ArrayList<Student> stuList=new ArrayList<>();
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("****欢迎来到学生管理系统****");
System.out.println("请选择操作:1.增加学生信息;2.删除学生信息;3.修改学生信息;4.查看学生信息;5.退出");
System.out.print("请输入你想执行的操作编号:");
int op=Integer.parseInt(sc.nextLine());
switch (op) {
case 1:
addStudent(stuList);
break;
case 2:
deleteStudent(stuList);
break;
case 3:
changeStudent(stuList);
break;
case 4:
showStudent(stuList);
break;
case 5:
sc.close();
System.exit(0);
break;
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}
public static void addStudent(ArrayList<Student> stuList){
Scanner sc=new Scanner(System.in);
Student stu=new Student();
System.out.print("请输入姓名:");
String name=sc.nextLine();
stu.setName(name);
System.out.print("请输入学号:");
String id=sc.nextLine();
stu.setId(id);
System.out.print("请输入年龄:");
int age=Integer.parseInt(sc.nextLine());
stu.setAge(age);
System.out.print("请输入住址:");
String address=sc.nextLine();
stu.setAdress(address);
stuList.add(stu);
System.out.println(stuList);
System.out.println("添加成功!");
}
public static void deleteStudent(ArrayList<Student> stuList){
Scanner sc=new Scanner(System.in);
if(stuList.size()==0){
System.out.println("当前没有学生信息,无法删除!");
return;
}
System.out.println(stuList);
System.out.print("请输入您想删除的学生学号:");
String id=sc.nextLine();
for (int i = 0; i < stuList.size(); i++) {
if(stuList.get(i).getId().equals(id)){
stuList.remove(i);
}
}
System.out.println(stuList);
System.out.println("删除成功!");
}
public static void changeStudent(ArrayList<Student> stuList){
Scanner sc=new Scanner(System.in);
if(stuList.size()==0){
System.out.println("当前没有学生信息,无法修改!");
return;
}
System.out.println(stuList);
System.out.print("请输入您想修改的学生学号:");
String id=sc.nextLine();
Student stu=new Student();
System.out.print("请输入新的姓名:");
String nameNew=sc.nextLine();
stu.setName(nameNew);
System.out.print("请输入新的学号:");
String idNew=sc.nextLine();
stu.setId(idNew);
System.out.print("请输入新的年龄:");
int ageNew=Integer.parseInt(sc.nextLine());
stu.setAge(ageNew);
System.out.print("请输入新的住址:");
String addressNew=sc.nextLine();
stu.setAdress(addressNew);
for (int i = 0; i < stuList.size(); i++) {
if(stuList.get(i).getId().equals(id)){
stuList.set(i, stu);
}
}
System.out.println(stuList);
System.out.println("修改成功!");
}
public static void showStudent(ArrayList<Student> stuList){
Scanner sc=new Scanner(System.in);
if(stuList.size()==0){
System.out.println("当前没有学生信息,无法查看!");
return;
}
for (int i = 0; i < stuList.size(); i++) {
System.out.println(stuList.get(i).getId());
}
System.out.print("请输入您想查看的学生的学号:");
String id=sc.nextLine();
int choice=0;
for (int i = 0; i < stuList.size(); i++) {
if(stuList.get(i).getId().equals(id)){
System.out.println(stuList.get(i));
break;
}
}
}
}