Java实现学生管理系统
public class Student {
String sid;
String name;
int age;
String address;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
public Student(String sid, String name, int 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class 学生管理系统 {
public static void main(String[] args) {
ArrayList array = new ArrayList<>();
Scanner sc = new Scanner(System.in);
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(“请输入你的选择”);
String line = sc.nextLine();
switch (line) {
case “1”:
Studentmate(array);
break;
case “2”:
deletestudent(array);
break;
case “3”:
updatastudent(array);
break;
case “4”:
Fianlstudent(array);
break;
case “5”:
System.out.println(“谢谢使用”);
System.exit(0);
}
}
}
public static void Studentmate(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
String sid;
while (true) {
System.out.println("输入学生的学号");
sid = sc.next();
boolean s = isUsed(array, sid);
if (s == true) {
System.out.println("你输入的学号已经使用,请从新输入学号");
} else break;
}
System.out.println("输入学生的姓名");
String name = sc.next();
System.out.println("输入学生的年龄");
int age = sc.nextInt();
System.out.println("输出学生家庭地址");
String address = sc.next();
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 Fianlstudent(ArrayList<Student> array) {
if (array.size() == 0) {
System.out.println("无信息,请先添加信息再查询");
return;
}
System.out.println("学号\t\t\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" + s.getName() + "\t" + s.getAge() + "\t\t" + s.getAddress());
}
}
public static void deletestudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学号");
String Sid = sc.nextLine();
int index = -1;
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(Sid)) {
array.remove(i);
index = i;
break;
}
}
if (index == -1) {
System.out.println("你输入的学号不存在,请重新输入");
} else
System.out.println("删除成功");
}
public static void updatastudent(ArrayList<Student> array) {
if (array.size() > 0) {
Scanner sc = new Scanner(System.in);
System.out.println("输入你要修改的学号");
String Sid = sc.nextLine();
System.out.println("输入新姓名");
String Name = sc.nextLine();
System.out.println("输入新年龄");
int Age = sc.nextInt();
System.out.println("输入新地址");
String address = sc.next();
Student student = new Student();
student.setSid(Sid);
student.setName(Name);
student.setAge(Age);
student.setAddress(address);
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(Sid)) {
array.set(i, student);
break;
}
}
System.out.println("修改学生成功");
}
else System.out.println("没有学生信息,请先添加学生信息");
}
public static boolean isUsed(ArrayList<Student> array, String sid) {
boolean flag = false;
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(sid)) {
flag = true;
break;
}
}
return flag;
}
}