class StudentManagementSystem {
public static String[][] stu = new String[5][5];
public static int point = 0;
public static void main(String[] args) {
add("小红", "女", "18", "1804", "001");
add("小黄", "女", "18", "1804", "002");
add("小绿", "女", "18", "1804", "003");
add("小兰", "女", "18", "1804", "004");
update("1804", "003", 1, "小蓝");
delete("1804", "003");
System.out.println("姓名\t性别\t年龄\t班级\t学号");
print();
}
// 添加
public static boolean add(String name, String sex, String age, String classId, String id) {
// 判断合法性
if (!MyUtils.isOk(name, sex, age, classId, id)) {
System.out.println("学生信息不合法");
return false;
}
// 判断是否存在
if (query(classId, id) != -1) {
System.out.println("此学生已存在");
return false;
}
// 扩容
if (point == stu.length) {
addCapacity();
}
// 添加
stu[point][0] = name;
stu[point][1] = sex;
stu[point][2] = age;
stu[point][3] = classId;
stu[point][4] = id;
point++;
System.out.println("添加成功");
return true;
}
// 删除学生
public static boolean delete(String classId, String Id) {
// 判断合法性
if (!MyUtils.isClassId(classId) || !MyUtils.isId(Id)) {
System.out.println("学生信息不合法");
return false;
}
// 判断是否存在
int index = query(classId, Id);
if (index == -1) {
System.out.println("此学生不存在");
return false;
}
// 删除
for (int i = index; i < point - 1; i++) {
for (int j = 0; j < 5; j++) {
stu[index][j] = stu[index + 1][j];
}
}
point--;
System.out.println("删除成功");
return true;
}
// 修改学生
public static boolean update(String classId, String Id, int type, String value) {
// 判断合法性
if (!MyUtils.isClassId(classId) || !MyUtils.isId(Id)) {
System.out.println("学生信息不合法");
return false;
}
// 判断是否存在
int index = query(classId, Id);
if (index == -1) {
System.out.println("此学生不存在");
return false;
}
// 修改
switch (type) {
case Type.NAME:
// 判断合法性
if (!MyUtils.isName(value)) {
return false;
}
// 修改
stu[index][0] = value;
break;
case Type.SEX:
// 判断合法性
if (!MyUtils.isSex(value)) {
return false;
}
// 修改
stu[index][1] = value;
break;
case Type.AGE:
// 判断合法性
if (!MyUtils.isAge(value)) {
return false;
}
// 修改
stu[index][2] = value;
break;
case Type.CLASS_ID:
// 判断合法性
if (!MyUtils.isClassId(value)) {
return false;
}
if (query(value, Id) != -1) {
return false;
}
// 修改
stu[index][3] = value;
break;
case Type.ID:
// 判断合法性
if (!MyUtils.isId(value)) {
return false;
}
// 修改
stu[index][4] = value;
break;
}
System.out.println("修改成功");
return true;
}
// 查询
public static int query(String classId, String id) {
for (int i = 0; i < point; i++) {
if (stu[i][3].equals(classId) && stu[i][4].equals(id)) {
return i;
}
}
return -1;
}
public static void addCapacity() {
String[][] newStu = new String[stu.length * 2][5];
for (int i = 0; i < point; i++) {
newStu[i][1] = stu[i][1];
newStu[i][2] = stu[i][2];
newStu[i][3] = stu[i][3];
newStu[i][4] = stu[i][4];
newStu[i][5] = stu[i][5];
}
stu = newStu;
}
// 打印
public static void print() {
for (int i = 0; i < point; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(stu[i][j] + "\t");
}
System.out.println();
}
}
}
// 判断合法性的工具类
class MyUtils {
public static boolean isOk(String name, String sex, String age, String classId, String id) {
if (!isName(name) || !isSex(sex) || !isAge(age) || !isClassId(classId) || !isId(id)) {
return false;
}
return true;
}
public static boolean isName(String name) {
if (name.length() < 2 || name.length() > 8) {
return false;
}
return true;
}
public static boolean isSex(String sex) {
if (!sex.equals("男") && !sex.equals("女")) {
return false;
}
return true;
}
public static boolean isAge(String age) {
int i = Integer.parseInt(age);
if (i < 6 || i > 60) {
return false;
}
return true;
}
public static boolean isClassId(String classId) {
if (classId.length() != 4) {
return false;
}
return true;
}
public static boolean isId(String id) {
if (id.length() != 3) {
return false;
}
return true;
}
}
// 学生信息常量
class Type {
public static final int NAME = 1;
public static final int SEX = 2;
public static final int AGE = 3;
public static final int CLASS_ID = 4;
public static final int ID = 5;
}
Java-学生管理系统(二维数组)
最新推荐文章于 2023-04-15 14:34:00 发布