package com.heima.myArrayList;
public class Student {
private String sid;
private String name;
private String age;
private String address;
public Student() {
}
public Student(String sid, String name, String age, String address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
package com.heima.myArrayList;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<Student>();
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 sc = new Scanner(System.in);
String line = sc.nextLine();
switch (line) {
case "1":
addStudent(array);
// System.out.println("添加学生");
break;
case "2":
deleteStudent(array);
// System.out.println("删除学生");
break;
case "3":
updateStudent(array);
// System.out.println("修改学生");
break;
case "4":
findAllStudent(array);
// System.out.println("查看所有学生");
break;
case "5":
System.out.println("谢谢使用");
System.exit(0);//JVM退出
return;
}
}
}
//定义一个方法,用于添加学生信息
public static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String sid = sc.nextLine();
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
System.out.println("请输入学生居住地:");
String address = sc.nextLine();
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
//定义一个方法,用于查看学生信息
public static void findAllStudent(ArrayList<Student> array) {
if (array.size() == 0) {
System.out.println("暂无信息,请添加后再查询");
return;
}
System.out.println("学号\t姓名\t年龄\t居住地");
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "岁\t" + s.getAddress());
}
}
//定义一个方法,用于删除学生信息
public static void deleteStudent(ArrayList<Student> array) {
//键盘录入要删除的学生学号,显示提示信息
Scanner sc = new Scanner(System.in);
System.out.println("请删除你要删除学生的学号");
String sid = sc.nextLine();
//遍历集合将对应学生对象从集合中删除
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(sid)) {
array.remove(i);
break;
}
}
//给出删除成功提示
System.out.println("删除学生信息成功");
}
//定义一个方法,用于修改学生信息
public static void updateStudent(ArrayList<Student> array) {
//键盘录入要修改的学生学号,显示提示信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生的学号");
String sid = sc.nextLine();
//键盘录入要修改的学生信息
System.out.println("请输入学生的新的姓名");
String name = sc.nextLine();
System.out.println("请输入学生的新的年龄");
String age = sc.nextLine();
System.out.println("请输入学生的新的居住地");
String address = sc.nextLine();
//创建学生对象
Student s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//遍历集合,修改对应的学生信息
for (int i = 0; i < array.size(); i++) {
Student student = array.get(i);
if (student.getSid().equals(sid)) {
array.set(i, s);
break;
}
}
//给出修改成功的提示
System.out.println("已成功修改学生信息");
}
} |
|