package xuesheng;
public class Student {
//学号
private String id;
//姓名
private String name;
//年龄
private int age;
//班级
private int clas;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 int getClas() {
return clas;
}
public void setClas(int clas) {
this.clas = clas;
}
public Student(String id, String name, int age, int clas) {
this.id = id;
this.name = name;
this.age = age;
this.clas = clas;
}
public Student() {
}
}
//
package xuesheng;
import com.sun.deploy.net.MessageHeader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentMannger {
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 sc = new Scanner(System.in);
String x = sc.nextLine();
switch (x) {
case "1":
addstudent(list);
break;
case "2":
deletestudent(list);
break;
case "3":
Revisestudent(list);
break;
case "4":
checkall(list);
break;
case "5":
System.exit(0);
}
}
}
public static void addstudent(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
String s ;
while(true) {
System.out.println("请输入学号");
s= sc.nextLine();
boolean flag = isused(list, s);
if (flag == true) {
System.out.println("该学号已经添加,请重新输入");
}
else {
break;
}
}
System.out.println("请输入名字");
String s1 = sc.nextLine();
System.out.println("请输入年龄");
int a = sc.nextInt();
System.out.println("请输入班级");
int b = sc.nextInt();
Student st = new Student();
st.setId(s);
st.setName(s1);
st.setAge(a);
st.setClas(b);
list.add(st);
System.out.println("添加学生成功");
}
public static boolean isused(ArrayList<Student> list,String id){
boolean flag=false;
for (int i=0;i< list.size();i++){
Student s=list.get( i);
if (s.getId().equals(id)){
flag=true;
break;
}
}
return flag;
}
public static void deletestudent(ArrayList<Student> list) {
if (list.isEmpty()) {
System.out.println("没有内容不能删除");
} else {
System.out.println("请输入要删除学生的学号");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int index=-1;
for (int i=0;i< list.size();i++){
Student st=list.get(i);
if (st.getId().equals(s)){
index=i;
break;
}
}
if (index==-1){
System.out.println("没有该学生");
}
else{
list.remove(index) ;
System.out.println("删除成功");
}
}
}
public static void Revisestudent(ArrayList<Student>list){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要修改学生的学号");
String id = scanner.nextLine();
System.out.println("请输入新名字");
String name = scanner.nextLine();
System.out.println("请输入新年龄");
int age=scanner.nextInt();
System.out.println("请输入新班级");
int cla = scanner.nextInt();
Student student=new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setClas(cla);
for (int i = 0; i < list.size(); i++) {
Student s=list.get(i);
if (s.getId().equals(id)){
list.set(i,student);
}
}
System.out.println("修改成功");
}
public static void checkall(ArrayList<Student> list){
if(list.size()==0){
System.out.println("请先添加信息再查询");
return;
}
System.out.println("学号: 姓名: 年龄: 班级");
for (int i=0;i< list.size();i++){
Student s=list.get(i);
System.out.println(s.getId()+" "+s.getName()+" "+s.getAge()+" "+s.getClas());
}
}
}
用ArrayList实现简单的学生管理系统
最新推荐文章于 2022-07-26 20:34:42 发布