package com;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) {
ArrayList<Student> array = 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 sc = new Scanner(System.in);
String line = sc.nextLine();
if(line.equals("5"))
{
System.out.println("谢谢使用");
break;
}
switch (line) {
case "1":
addStudent(array);
break;
case "2":
System.out.println("删除学生");
deleteStudent(array);
break;
case "3":
changeStudent( array);
break;
case "4":
findStudent( array);
break;
case "5":
System.out.println("谢谢使用");
break;
}
}
}
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 changeStudent(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(sid.equals(s.getSid()))
{
System.out.println("请输入学生新姓名:");
String name = sc.nextLine();
s.setName(name);
System.out.println("请输入学生新年龄:");
String age = sc.nextLine();
s.setAge(age);
System.out.println("请输入学生新居住地:");
String address = sc.nextLine();
s.setAddress(address);
}
}
}
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))//比较两个字符串用equals(),否则是在比较地址
{
array.remove(i);
System.out.println("删除成功");
break;
}
}
}
public static void findStudent(ArrayList<Student> array) {
if(array.size()==0)
{
System.out.println("无数据,请先添加信息再查询");
return;
}
System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getSid() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "岁" + "\t" + s.getAddress());
}
}
Student类
package com;
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 String getName() { return name; }
public String getAge() { return age; }
public String getAddress() { return address; }
public void setSid(String sid) { this.sid = sid; }
public void setName(String name) { this.name = name; }
public void setAge(String age) { this.age = age; }
public void setAddress(String address) { this.address = address; }
}