通过半个月的学习,自己写的一个小项目,学生管理系统。
功能不是很完善,通过以后的学习。再慢慢的去完善它。
加油
package com.itheima_2;
import java.util.ArrayList;
import java.util.Scanner;
public class TestStudent {
public static void main(String[] args) {
ArrayList<Student> arry = 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 str = sc.nextLine();
switch (str) {
case "1":
show(arry);
break;
case "2":
addStudent(arry);
break;
case "3":
removeStudent(arry);
break;
case "4":
setStudent(arry);
break;
case "5":
System.out.println("谢谢使用,退出系统");
System.exit(0);
default:
System.out.println("输入有误,请重新输入");
break;
}
}
}
public static void show(ArrayList<Student> arry) {
if (arry.size() == 0) {
System.out.println("系统内信息为空,请添加学生信息");
} else {
System.out.println("学号\t姓名\t年龄\t地址");
for (int i = 0; i < arry.size(); i++) {
Student s = arry.get(i);
System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
}
public static void addStudent(ArrayList<Student> arry) {
Scanner sc = new Scanner(System.in);
String id;
while (true) {
boolean b = false;
System.out.println("请输入学生学号:");
id = sc.nextLine();
for (int i = 0; i < arry.size(); i++) {
Student s = arry.get(i);
if (s.getId().equals(id)) {
b = true;
break;
}
}
if (b ) {
System.out.println("输入的学号已经存在,请重新输入:");
} 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.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
arry.add(s);
System.out.println("添加成功");
}
public static void removeStudent(ArrayList<Student> arry) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除学生的学号:");
String id = sc.nextLine();
int inder = -1;
for (int i = 0; i < arry.size(); i++) {
Student s = arry.get(i);
if (s.getId().equals(id)) {
inder = i;
break;
}
}
if (inder != -1) {
arry.remove(inder);
System.out.println("删除成功");
} else {
System.out.println("学生信息内,不存在该学号。请重新操作");
}
}
public static void setStudent(ArrayList<Student> arry) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要修改的学生学号:");
String id = sc.nextLine();
int inder = -1;
for (int i = 0; i < arry.size(); i++) {
Student s = arry.get(i);
if (s.getId().equals(id)) {
inder = i;
break;
}
}
if (inder != -1) {
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.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
arry.set(inder, s);
} else {
System.out.println("学生信息内,不存在该学号。请重新操作");
}
}
}
|
|