需求
采用控制台得方式去书写学生管理系统。
分析:
初始菜单
"------------欢迎来到学生管理系统------------" "1:添加学生" "2:删除学生" "3:修改学生" "4:查询学生" "5:退出"; "请输入您的选择"
学生类
属性:id 、姓名、年龄、家庭住址
添加功能
键盘录入每一个学生信息并添加,需要满足以下要求:
id唯一
删除功能:
键盘录入要删除学生得id,需要满足以下要求:
id存在删除
id不存在,需要提示不存在,并回到初始菜单
查询功能:
打印所有学生信息,需要,满足以下要求
如果没有学生信息,提示:当前无学生信息,请添加后再查询
如果有学生信息,按照以下格式输出。
id 姓名 年龄 家庭住址
001 张三 23 南京
002 李四 24 北京
003 王五 25 深圳
public class Student {
private String id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystemtext {
public static void main(String[] args) {
ArrayList<Student> student = new ArrayList<>();
loop:
while (true) {
System.out.println("------------欢迎来到学生管理系统------------");
Scanner sc = new Scanner(System.in);
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("5:退出");
System.out.println("请输入您的选择");
int choose = sc.nextInt();
switch (choose) {
case 1: {
addstudent(student);
break;
}
case 2: {
deletestudent(student);
break;
}
case 3: {
modifystudent(student);
break;
}
case 4: {
querystudent(student);
break;
}
case 5: {
System.out.println("5:退出");
break loop;
//System.exit(0);停止虚拟机运行;
}
default:
System.out.println("没有这个选项");
}
}
}
// 添加学生
public static void addstudent(ArrayList<Student> student) {
Scanner sc = new Scanner(System.in);
Student s = new Student();
System.out.println("添加学生");
System.out.println("请输入id、姓名、年龄、家庭住址");
String id =sc.next();
if(only(id,student)) {
s.setId(id);
String name = sc.next();
s.setName(name);
int age = sc.nextInt();
s.setAge(age);
String address = sc.next();
s.setAddress(address);
student.add(s);
}else{
System.out.println("id已经存在,需要重新录入");
}
}
//删除学生
public static void deletestudent(ArrayList<Student>student) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入id");
String newid = sc.next();
int list=exist(newid,student);
if(list!= -1){
student.remove(list);
System.out.println("id为"+list+"的学生信息删除完毕");
}
else{
System.out.println("删除失败");
}
}
//修改学生
public static void modifystudent(ArrayList<Student>student){
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要修改的id");
String id = sc.next();
int list = exist(id,student);
if(list!=-1){
String name =sc.next();
student.get(list).setName(name);
int age = sc.nextInt();
student.get(list).setAge(age);
String address= sc.next();
student.get(list).setAddress(address);
}else{
System.out.println("id不存在,请继续");
return;
}
}
//查询学生
public static void querystudent(ArrayList<Student> student) {
if (student.size() == 0) {
System.out.println("当前无学生信息,请添加后再查询");
return;
} else {
System.out.println("id\t" + "姓名\t" + "年龄\t" + "家庭住址");
for (int i = 0; i < student.size(); i++) {
Student s = student.get(i);
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
}
//id不存在
public static boolean only(String id,ArrayList<Student>student){
for (int i = 0; i <student.size() ; i++) {
if(student.get(i).getId().equals(id))
return false;
}
return true;
}
//id存在并返回索引
public static int exist(String id,ArrayList<Student>student){
for (int i = 0; i <student.size() ; i++) {
if(student.get(i).getId().equals(id))
return i;
}
return -1;
}
}