学生管理系统1.0版本
学生管理系统1.0版本有5个功能,即添加学生、删除学生、修改学生、查看全部学生、退出系统。
里面对添加重复学号、删除和修改不存在的学号等问题都有相应的解决办法。
代码区:
Student.java
package student;
//快捷键Alt+insert
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;
}
}
StudentManager.java
package student;
import java.util.Scanner;
import java.util.ArrayList;
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":
//System.out.println("添加学生");
addStudent(array);
break;
case "2":
//System.out.println("删除学生");
deleteStudent(array);
break;
case "3":
//System.out.println("修改学生");
updateStudent(array);
break;
case "4":
findAllStudent(array);
//System.out.println("查看所有学生");
break;
case "5":
System.out.println("退出系统");
System.exit(0); //直接退出java虚拟机
default:
System.out.println("操作错误,请重新输入:");
}
}
}
//添加功能
public static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);//刘正风,嵩山
String sid;
System.out.println("请输入学生学号:");//风清扬,华山,岳不群,华山
while(true) {
int index=-1;
sid = sc.nextLine();
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(sid)) {
index=i;
break;
}
}
if(index==-1)
break;
else
System.out.println("你输入的学号已被使用,请重新输入学号:");
}
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\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) {
if(array.size()==0){
System.out.println("无信息!请先添加信息再删除!");
return ;
}
Scanner sc = new Scanner(System.in);
String sid;
int index=-1;
System.out.println("请输入你要删除的学生的学号:");
while(true) {
sid = sc.nextLine();
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(sid)) {
index=i;
break;
}
}
if(index==-1)
System.out.println("该学生不存在!\n请重新输入你要删除的学生的学号:");
else {
array.remove(index);
System.out.println("删除成功!");
break;
}
}
}
//修改功能
public static void updateStudent(ArrayList<Student> array) {
if(array.size()==0){
System.out.println("无信息!请先添加信息!");
return ;
}
Scanner sc = new Scanner(System.in);
String sid;
int index=-1;
System.out.println("请输入你要修改的学生的学号:");
while(true) {
sid = sc.nextLine();
for (int i = 0; i < array.size(); i++) {
Student s1 = array.get(i);
if (s1.getSid().equals(sid)) {
index = i;
break;
}
}
if (index==-1) {
System.out.println("该学生不存在!\n请重新输入你要修改的学生的学号:");
}
else
break;
}
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.setAge(age);
s.setName(name);
s.setAddress(address);
array.set(index,s);
System.out.println("修改成功!");
}
}
部分运行结果: