这是一个结合面向对象封装、继承、多态三者相结合的综合性学生管理系统,写的可能还不太完善,1.0版本
下面是运行效果的部分截图
package p1;
//String number,String name,int age,boolean sex,double score,int clazz
//String number,String userName,String password
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
Manage s = new Manage();
s.start();
}
}
class Manage {
private int howManyCz = 3;
Clazz[] cz = new Clazz[4];
Scanner s = new Scanner(System.in);
Person person = new Person();
Management[] m = new Management[2];
private String thisMaNumber;
public Manage() {
// 编号、姓名、年龄、性别、分数、班级
for (int i = 0; i < cz.length; i++) {
cz[i] = new Clazz();
}
cz[0].st[0] = new Student(200101, "李亮", 21, '男', 91.0, 2001);
cz[1].st[0] = new Student(200201, "王丽", 19, '女', 92.0, 2002);
cz[2].st[0] = new Student(200301, "阴子航", 20, '男', 93.0, 2003);
cz[2].st[1] = new Student(200302, "赵旭", 23, '男', 95.0, 2003);
cz[2].st[2] = new Student(200303, "张娟", 20, '女', 94.0, 2003);
cz[0].setHowManySt(1);
cz[1].setHowManySt(1);
cz[2].setHowManySt(3);
cz[3].setHowManySt(0);
for (int i = 0; i < cz.length; i++) {
cz[i].setClazzName(2001 + i);
}
// 编号、帐号、密码
m[0] = new Management("1301", "zxw1998", "123456");
m[1] = new Management("1302", "zmj1997", "123456");
}
public void setHowManyCz(int howManyCz) {
this.howManyCz = howManyCz;
}
public int getHowManyCz() {
return howManyCz;
}
public void setThisMaNumber(String thisMaNumber) {
this.thisMaNumber = thisMaNumber;
}
public String getThisMaNumber() {
return thisMaNumber;
}
// 起始界面
public void start() {
int n;
do {
n = 0;
System.out.println("--------------欢迎使用百知学生管理系统-------------");
System.out.println("1.注册 2.登录 3.退出");
boolean bn;
do {
bn = false;
System.out.println("请输入你要执行的操作选项:");
n = s.nextInt();
if (n == 1) {
register();
} else if (n == 2) {
login();
} else if (n == 3) {
exit();
} else {
System.out.println("输入有误!请重新输入~~~");
bn = true;
}
} while (bn);
} while (n != 3);
}
// 注册
public void register() {
boolean bn;
do {
bn = false;
if (person.getMaCount() == m.length) {
m = (Management[]) addArrays(m);
bn = true;
} else {
// 帐号
do {
bn = false;
System.out.print("请输入帐号:");
String userName = s.next();
if (maIfExist("userName", userName)) {
m[person.getMaCount()] = new Management();
m[person.getMaCount()].setUserName(userName);
System.out.println();
} else {
System.out.println("该账号已被注册!请重新输入~~~");
bn = true;
}
} while (bn);
// 密码
System.out.print("请输入密码:");
m[person.getMaCount()].setPassword(s.next());
System.out.println();
// 编号
do {
bn = false;
System.out.print("请输入编号:");
String number = s.next();
if (maIfExist("number", number)) {
m[person.getMaCount()].setNumber(number);
System.out.println();
} else {
System.out.println("该编号已存在!请重新输入~~~");
bn = true;
}
} while (bn);
System.out.println("注册成功!");
person.setMaCount(person.getMaCount() + 1);
}
} while (bn);
}
// 登录
public void login() {
boolean bn;
do {
bn = false;
System.out.print("请输入帐号:");
String userName = s.next();
System.out.print("请输入密码:");
String password = s.next();
System.out.print("请输入编号:");
String number = s.next();
if (ifTrue(userName, password, number)) {
setThisMaNumber(number);
showMenu();
} else {
System.out.println("重新输入-(y/Y) 退出-(n/N)");
System.out.print("请输入指令:");
char c = s.next().charAt(0);
bn = toBoolean(c);
}
} while (bn);
}
// 退出
public void exit() {
}
// 显示菜单栏
public void showMenu() {
int choice;
do {
System.out.println("*************显示菜单栏**************");
System.out.println("***1.添加学员信息 2.查询指定学员***");
System.out.println("***3.查询班级信息 4.查询所有信息***");
System.out.println("***5.修改学员信息 6.删除学员信息***");
System.out.println("***7.修改登录密码 8.退出当前账号***");
System.out.println("**************版权所有***************");
System.out.print("请输入操作编号:");
choice = s.nextInt();
switch (choice) {
case (1):
addStudentInformation();
break;
case (2):
querySpecifiedInformation();
break;
case (3):
queryClassInformation();
break;
case (4):
queryAllInformation();
break;
case (5):
modifyStudentInformation();
break;
case (6):
deleteStudentInformation();
break;
case (7):
choice = changeLoginPassword();
break;
case (8):
exitThisAccount();
break;
default:
System.out.println("输入有误!请重新输入!");
break;
}
} while (choice != 8);
}
// 添加学员
public void addStudentInformation() {
boolean bn;
String name;
int age, clazz, number, x;
double score;
char sex;
do {
bn = false;
System.out.println("请输入学员班级:");
clazz = s.nextInt();
if (czIfExist(clazz) == false) {
x = returnCzByClazzName(clazz);
if (cz[x].getHowManySt() == cz[x].st.length) {
cz[x].st = (Student[]) addArrays(cz[x].st);
bn = true;
} else {
cz[x].st[cz[x].getHowManySt()] = new Student();
number = inputNumberExist(clazz);
name = inputName();
age = inputAge();
sex = inputSex();
score = inputScore();
boolean bo;
do {
bo = false;
System.out.println("请确认信息是否正确!");
System.out.println("编号-" + number + " 姓名-" + name + " 年龄-" + age + " 性别-" + sex + " 班级-" + clazz
+ " 分数-" + score);
System.out.println("需要修改-y/Y 正确-n/N");
bo = toBoolean(s.next().charAt(0));
if (bo == false) {
cz[x].st[cz[x].getHowManySt()].setNumber(number);
cz[x].st[cz[x].getHowManySt()].setName(name);
cz[x].st[cz[x].getHowManySt()].setAge(age);
cz[x].st[cz[x].getHowManySt()].setSex(sex);
cz[x].st[cz[x].getHowManySt()].setClazz(clazz);
cz[x].st[cz[x].getHowManySt()].setScore(score);
System.out.println("添加成功!");
cz[x].setHowManySt(cz[x].getHowManySt() + 1);
System.out.println("是否继续添加 (继续-y/Y 返回-n/N)");
bn = toBoolean(s.next().charAt(0));
} else {
System.out.println("1.修改编号 2.修改姓名 3.修改年龄 4.修改性别 5.修改班级 6.修改分数");
System.out.print("请输入操作编号:");
do {
bn = false;
int a = s.nextInt();
switch (a) {
case (1):
number = inputNumberExist(clazz);
break;
case (2):
name = inputName();
break;
case (3):
age = inputAge();
break;
case (4):
sex = inputSex();
break;
case (5):
clazz = inputClazz();
break;
case (6):
score = inputScore();
break;
default:
System.out.println("输入有误!请重新输入!");
bn = true;
break;
}
} while (bn);
System.out.println("修改成功!");
}
} while (bo);
}
} else {
System.out.println("暂时还没有这个班级!");
System.out.print("是否继续 (继续查询-y/Y 返回-n/N):");
bn = toBoolean(s.next().charAt(0));
}
} while (bn);
}
// 查询指定学员
public void querySpecifiedInformation() {
boolean bo;
do {
bo = false;
System.out.print("请输入学员编号:");
int number = s.nextInt();
System.out.println();
boolean bn = true;
for (int i = 0; i < cz.length; i++) {
for (int j = 0; j < cz[i].getHowManySt(); j++) {
if (cz[i].st[j].getNumber() == number) {
cz[i].st[i].showStInformation();
bn = false;
break;
}
}
}
if (bn) {
System.out.println("该学员不存在!");
}
System.out.println("继续查询-y/Y 返回-n/N");
bo = toBoolean(s.next().charAt(0));
} while (bo);
}
// 查询班级信息
public void queryClassInformation() {
boolean bo;
int a;
do {
bo = false;
System.out.print("请输入班级号:");
int clazzName = s.nextInt();
int x = returnCzByClazzName(clazzName);
System.out.println();
if (czIfExist(clazzName) == false) {
if (cz[x].getHowManySt() == 0) {
System.out.println(cz[x].getClazzName() + "班为预留班,暂无学员!");
} else {
for (int j = 0; j < cz[x].getHowManySt(); j++) {
cz[x].st[j].showStInformation();
System.out.println();
}
}
} else {
System.out.println("没有该班级!");
}
System.out.println("是否继续(继续查询-y/Y 返回-n/N)");
bo = toBoolean(s.next().charAt(0));
} while (bo);
}
// 查询所有信息
public void queryAllInformation() {
for (int i = 0; i < cz.length; i++) {
for (int j = 0; j < cz[i].getHowManySt(); j++) {
cz[i].st[j].showStInformation();
System.out.println();
}
}
}
// 修改学员信息
public void modifyStudentInformation() {
boolean bo;
int number, age, clazz;
String name;
char sex;
double score;
do {
bo = false;
System.out.print("请输入你要修改的学员编号:");
number = s.nextInt();
if (stIfExist(number) == false) {
int x = returnCzByClazzName(number / 100);
int y = returnStByCz(x);
System.out.println("请确认这是否为你要修改的学员");
System.out.println("编号-" + cz[x].st[y].getNumber() + " 姓名-" + cz[x].st[y].getName() + " 班级-"
+ cz[x].st[y].getClazz());
System.out.print("请按指令输入(是-y/Y 不是-n/N):");
boolean bool = toBoolean(s.next().charAt(0));
System.out.println();
if (bool) {
System.out.println("1.修改编号 2.修改姓名 3.修改年龄 4.修改性别 5.修改班级 6.修改分数");
System.out.print("请输入操作编号:");
boolean bn;
do {
bn = false;
int a = s.nextInt();
switch (a) {
case (1):
number = inputNumberExist(cz[x].getClazzName());
cz[x].st[y].setNumber(number);
break;
case (2):
name = inputName();
cz[x].st[y].setName(name);
break;
case (3):
age = inputAge();
cz[x].st[y].setAge(age);
break;
case (4):
sex = inputSex();
cz[x].st[y].setSex(sex);
break;
case (5):
clazz = inputClazz();
Student stud = new Student();
for (int j = y; j < cz[x].getHowManySt() - 1; j++) {
stud = cz[x].st[y];
cz[x].st[y] = cz[x].st[y + 1];
}
cz[x].st[cz[x].getHowManySt() - 1] = null;
int t = returnCzByClazzName(number / 100);
if (cz[t].getHowManySt() == cz[t].st.length) {
cz[t].st = (Student[]) addArrays(cz[t].st);
} else {
cz[t].st[cz[t].getHowManySt()] = stud;
cz[t].setHowManySt(cz[t].getHowManySt() + 1);
System.out.println("修改成功!");
}
break;
case (6):
score = inputScore();
cz[x].st[y].setScore(score);
break;
default:
System.out.println("输入有误!请重新输入!");
bn = true;
break;
}
} while (bn);
System.out.println("修改成功!");
System.out.println("是否继续修改 (继续-y/Y 返回-n/N)");
bo = toBoolean(s.next().charAt(0));
} else {
System.out.println("是否继续修改 (继续-y/Y 返回-n/N)");
bo = toBoolean(s.next().charAt(0));
}
} else {
System.out.println("该学员不存在!请重新输入!");
bo = true;
}
} while (bo);
}
// 删除学员信息
public void deleteStudentInformation() {
boolean bo;
int number, age, clazz;
String name;
char sex;
double score;
do {
bo = false;
System.out.print("请输入你要删除的学员编号:");
number = s.nextInt();
if (stIfExist(number) == false) {
int x = returnCzByClazzName(number / 100);
int y = returnStByCz(x);
System.out.println("请确认这是否为你要删除的学员");
cz[x].st[y].showStInformation();
System.out.println();
System.out.print("请按指令输入(是-y/Y 不是-n/N):");
boolean bool = toBoolean(s.next().charAt(0));
System.out.println();
if (bool) {
if (y < cz[x].getHowManySt()) {
for (int i = x; i < cz[x].getHowManySt() - 1; i++) {
cz[x].st[i] = cz[x].st[i + 1];
}
cz[x].st[cz[x].getHowManySt() - 1] = null;
}
cz[x].setHowManySt(cz[x].getHowManySt() - 1);
if (cz[x].getHowManySt() == 0) {
System.out.println("该学员已删除~~你已经没有学员了!!赶紧去招生吧~~~");
} else {
System.out.println("该学员已删除~~请问你是否继续删除其他学员~~~~");
System.out.print("继续-y/Y,返回-n/N:");
bo = toBoolean(s.next().charAt(0));
}
} else {
System.out.println("是否继续删除 (继续-y/Y 返回-n/N)");
bo = toBoolean(s.next().charAt(0));
}
} else {
System.out.println("该学员不存在!请重新输入!");
bo = true;
}
} while (bo);
}
// 修改登录密码
public int changeLoginPassword() {
int a = 0;
for (int i = 1; i <= 3; i++) {
boolean bn;
bn = true;
System.out.print("原密码:");
String password = s.next();
int x = returnMaByNumber(getThisMaNumber());
if (password.equals(m[x].getPassword())) {
boolean bo;
do {
bo = false;
System.out.print("新密码:");
String newPassword1 = s.next();
System.out.print("请再次输入你的新密码:");
String newPassword2 = s.next();
if (newPassword1.equals(newPassword2)) {
m[x].setPassword(newPassword1);
System.out.println("修改成功!");
} else {
System.out.println("两次密码输入不一致!请重新输入~~");
bo = true;
}
} while (bo);
break;
} else if (i < 3) {
System.out.println("输入错误,你今天还有" + (3 - i) + "次机会");
} else {
System.out.println("今日输入密码的次数已达上限!账户已被冻结~~");
a = 8;
}
}
return a;
}
// 退出当前账号
public void exitThisAccount() {
}
// 根据编号返回数组ma的下标
public int returnMaByNumber(String number) {
int x = 0;
for (int i = 0; i < person.getMaCount(); i++) {
if (number.equals(m[i].getNumber())) {
x = i;
}
}
return x;
}
// 根据班级名返回数组cz的下标
public int returnCzByClazzName(int clazz) {
int x = 0;
for (int i = 0; i < cz.length; i++) {
if (cz[i].getClazzName() == clazz) {
x = i;
}
}
return x;
}
// 根据数组Cz下标返回数组st的下标
public int returnStByCz(int n) {
int x = 0;
for (int i = 0; i < cz[n].getHowManySt(); i++) {
if (cz[n].st[i].getClazz() == cz[n].getClazzName()) {
x = i;
}
}
return x;
}
// 输入编号判断是否存在或合法
public int inputNumberExist(int clazz) {
boolean bn;
int number;
do {
bn = false;
System.out.println("请按照格式输入(例如2001班最小编号为200101,最大编号200199)");
System.out.print("请输入学员编号:");
number = s.nextInt();
System.out.println();
if (stIfOk(number, clazz)) {
if (stIfExist(number) == false) {
System.out.println("该编号已存在!请重新输入!");
bn = true;
}
} else {
System.out.println("编号不合法!请重新输入!");
bn = true;
}
} while (bn);
return number;
}
/*
* //判断编号是否合法 public boolean inputNumberOk(int clazz){ boolean bn; int number;
* System.out.println("请按照格式输入(例如2001班最小编号为200101,最大编号200199)"); do{ bn = false;
* System.out.print("请输入学员编号:"); number = s.nextInt(); System.out.println();
* if(stIfOk(number,clazz) == false){ System.out.println("该编号不合法!请重新输入!"); bn =
* true; } }while(bn); return number; }
*/
// 输入姓名
public String inputName() {
System.out.print("请输入学员姓名:");
String name = s.next();
System.out.println();
return name;
}
// 输入年龄并判断是否合法
public int inputAge() {
int age;
boolean bn;
do {
bn = false;
System.out.print("请输入学员年龄:");
age = s.nextInt();
System.out.println();
if (ageIfValid(age) == false) {
System.out.println("年龄不合法!请重新输入!");
bn = true;
}
} while (bn);
return age;
}
// 输入性别(男/女)并判断是否合法
public char inputSex() {
boolean bn;
char sex;
do {
bn = false;
System.out.print("请输入学员性别(男/女):");
sex = s.next().charAt(0);
System.out.println();
if (sexIfValid(sex) == false) {
System.out.println("性别不合法!请重新输入!");
bn = true;
}
} while (bn);
return sex;
}
// 输入班级并判断是否存在
public int inputClazz() {
boolean bn;
int clazz;
do {
bn = true;
System.out.print("请输入学员班级:");
clazz = s.nextInt();
System.out.println();
if (clazzIfValid(clazz)) {
if (czIfExist(clazz) == false) {
System.out.println("该班级不存在!请重新输入!");
}
}
} while (bn);
return clazz;
}
// 输入分数并判断是否合法
public double inputScore() {
double score;
boolean bn;
do {
bn = false;
System.out.print("请输入学员分数:");
score = s.nextDouble();
System.out.println();
if (scoreIfValid(score) == false) {
System.out.println("分数不合法!请重新输入!");
bn = true;
}
} while (bn);
return score;
}
// 数组扩容
public Person[] addArrays(Person[] p) {
return java.util.Arrays.copyOf(p, p.length * 2);
}
// 判断 管理员 帐号/密码/编号 是否存在(存在-false 不存在-true)
public boolean maIfExist(String what, String sc) {
boolean bn = true;
for (int i = 0; i < this.person.getMaCount(); i++) {
if (what.equals("userName")) {
if (m[i].getUserName().equals(sc)) {
bn = false;
}
} else if (what.equals("password")) {
if (m[i].getPassword().equals(sc)) {
bn = false;
}
} else {
if (m[i].getNumber().equals(sc)) {
bn = false;
}
}
}
return bn;
}
// 判断 学生 编号是否存在 (不存在-true 存在-false)
public boolean stIfExist(int sc) {
boolean bn = true;
for (int i = 0; i < cz.length; i++) {
for (int j = 0; j < cz[i].getHowManySt(); j++) {
if (cz[i].st[j].getNumber() == sc) {
bn = false;
break;
}
}
}
return bn;
}
// 判断 学生 编号是否合法 (合法-true 不合法-false)
public boolean stIfOk(int sc, int clazz) {
boolean bn = true;
for (int i = 0; i < cz[returnCzByClazzName(clazz)].getHowManySt(); i++) {
if (sc < (clazz * 100 + 1)) {
bn = false;
}
}
return bn;
}
// 判断班级名是否合法
public boolean clazzIfValid(int clazz) {
if (clazz >= 2001) {
return true;
} else {
System.out.println("班级输入有误!请输入一个大于2000的班级号!");
return false;
}
}
// 判断班级名是否存在
public boolean czIfExist(int clazz) {
boolean bn = true;
for (int i = 0; i < cz.length; i++) {
if (cz[i].getClazzName() == clazz) {
bn = false;
}
}
return bn;
}
// 判断 年龄 是否合法
public boolean ageIfValid(int age) {
if (age >= 0 && age <= 150) {
return true;
} else {
return false;
}
}
// 判断 性别 是否合法
public boolean sexIfValid(char sex) {
if (sex == '男' || sex == '女') {
return true;
} else {
return false;
}
}
// 判断 分数 是否合法
public boolean scoreIfValid(double score) {
if (score >= 0 && score <= 100) {
return true;
} else {
return false;
}
}
// 判断是否登录成功
public boolean ifTrue(String userName, String password, String number) {
boolean bn;
do {
bn = false;
for (int i = 0; i < person.getMaCount(); i++) {
if (m[i].getUserName().equals(userName) && m[i].getPassword().equals(password)
&& m[i].getNumber().equals(number)) {
System.out.println("登录成功!");
bn = true;
}
}
if (bn == false) {
System.out.println("该账户不存在!");
}
return bn;
} while (bn);
}
// 将字符类型的指令转换成boolean类型
public boolean toBoolean(char c) {
if (c == 'y' || c == 'Y') {
return true;
} else {
return false;
}
}
}
class Person {
// 扫描仪
Scanner s = new Scanner(System.in);
// 管理员个数
private int maCount = 2;
/*
* public void setStCount(int stCount) { this.stCount = stCount; }
*
* public int getStCount() { return stCount; }
*/
public void setMaCount(int maCount) {
this.maCount = maCount;
}
public int getMaCount() {
return maCount;
}
}
class Student extends Person {
private String name;
private int age;
private char sex;
private double score;
private int clazz;
private int number;
public Student() {
}
public Student(int number, String name, int age, char sex, double score, int clazz) {
setNumber(number);
setName(name);
setAge(age);
setSex(sex);
setScore(score);
setClazz(clazz);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setSex(char sex) {
this.sex = sex;
}
public char getSex() {
return sex;
}
public void setScore(double score) {
this.score = score;
}
public double getScore() {
return score;
}
public void setClazz(int clazz) {
this.clazz = clazz;
}
public int getClazz() {
return clazz;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public void showStInformation() {
StringBuffer sb1 = new StringBuffer("编号:");
sb1.append(getNumber());
StringBuffer sb2 = new StringBuffer("姓名:");
sb2.append(getName());
StringBuffer sb3 = new StringBuffer("年龄:");
sb3.append(getAge());
StringBuffer sb4 = new StringBuffer("性别:");
sb4.append(getSex());
StringBuffer sb5 = new StringBuffer("班级:");
sb5.append(getClazz());
StringBuffer sb6 = new StringBuffer("分数:");
sb6.append(getScore());
System.out.println(sb1.toString());
System.out.println(sb2.toString());
System.out.println(sb3.toString());
System.out.println(sb4.toString());
System.out.println(sb5.toString());
System.out.println(sb6.toString());
}
}
class Management extends Person {
private String userName;
private String password;
private String number;
public Management() {
}
public Management(String number, String userName, String password) {
setNumber(number);
setUserName(userName);
setPassword(password);
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setNumber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
}
class Clazz {
private int howManySt;
private int clazzName;
Student[] st = new Student[5];
public Clazz() {
}
public Clazz(int clazzName, Student[] st) {
setClazzName(clazzName);
}
public void setClazzName(int clazzName) {
this.clazzName = clazzName;
}
public int getClazzName() {
return clazzName;
}
public void setHowManySt(int howManySt) {
this.howManySt = howManySt;
}
public int getHowManySt() {
return howManySt;
}
}