学生管理系统
主类
package Com;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManage {
public static void main(String[] args) {
ArrayList<Student> a = new ArrayList<Student>();
boolean flag = true;
while (flag) {
Scanner s = new Scanner(System.in);
System.out.println("请输入您的选项");
print();
if (s.hasNextInt()) {
int num = s.nextInt();
switch (num) {
case 1:
System.out.println("添加学生");
addS(a);
break;
case 2:
System.out.println("删除学生");
deletes(a);
break;
case 3:
System.out.println("修改学生");
adapts(a);
break;
case 4:
System.out.println("查看所有学生");
finds(a);
break;
case 5:
System.out.println("退出成功");
flag = false;
break;
case 6:
System.out.println("请输入你的选项");
break;
default:
System.out.println("你输入的选项超出范围");
break;
}
} else {
System.out.println("请输入数字选项");
}
}
}
public static void print() {
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("请输入您的选择");
}
public static void addS(ArrayList<Student> s) {
Scanner s1 = new Scanner(System.in);
System.out.println("请输入学生学号");
String sid = s1.nextLine();
System.out.println("请输入学生姓名");
String name = s1.nextLine();
System.out.println("请输入学生年龄");
String age = s1.nextLine();
System.out.println("请输入学生居住地");
String address = s1.nextLine();
Student ss = new Student();
ss.setSid(sid);
ss.setName(name);
ss.setAge(age);
ss.setAddress(address);
s.add(ss);
System.out.println("添加成功");
}
public static void finds(ArrayList<Student> o) {
if(o.size()==0){
System.out.println("无学生信息,请您添加");
return;
}
System.out.println("学号\t姓名\t\t年龄\t居住地");
for (int i = 0; i < o.size(); i++) {
Student a = o.get(i);
System.out.println("第"+i+"号"+a.getSid()+"\t"+a.getName()+"\t" + a.getAge()+"\t"+a.getAddress());
}
}
public static void deletes(ArrayList<Student> s){
System.out.println("学号\t姓名\t年龄\t\t居住地");
for (int i = 0; i < s.size(); i++) {
Student a = s.get(i);
System.out.println("第"+i+"号"+a.getSid()+"\t"+a.getName()+"\t" + a.getAge()+"\t"+a.getAddress());
}
System.out.println("请输入你要删除学生的学号");
Scanner ml = new Scanner(System.in);
String ml1 = ml.nextLine();
int index=-1;
for (int i = 0; i < s.size(); i++) {
Student a=s.get(i);
if(a.getSid().equals(ml1)){
index=i;
break;
}
if (index ==-1){
System.out.println("删除失败");
}else{
s.remove(i);
System.out.println("删除成功");
}
}
}
public static void adapts(ArrayList<Student> y){
System.out.println("学号\t姓名\t\t年龄\t居住地");
for (int i = 0; i < y.size(); i++) {
Student a = y.get(i);
System.out.println("第"+i+"个"+a.getSid()+"\t"+a.getName()+"\t" + a.getAge()+"\t"+a.getAddress());
}
System.out.println("请输入你要修改学生的信息,请输入学号");
Scanner s1 = new Scanner(System.in);
int u=s1.nextInt();
System.out.println("请输入学生学号");
String sid = s1.nextLine();
System.out.println("请输入学生姓名");
String name = s1.nextLine();
System.out.println("请输入学生年龄");
String age = s1.nextLine();
System.out.println("请输入学生居住地");
String address = s1.nextLine();
Student ss = new Student();
ss.setSid(sid);
ss.setName(name);
ss.setAge(age);
ss.setAddress(address);
y.add(ss);
for (int i = 0; i < y.size(); i++) {
Student a=y.get(i);
if (a.getSid().equals(sid)){
y.set(i,ss);
break;
}
}
System.out.println("修改学生成功");
}
}
学生类
package Com;
public class Student {
private String name;
private String sid;
private String age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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;
}
}
|