建立一个包含学生、家长、老师、班级的学校管理模型,系统录入、查询学生信息、家长信息、老师,以及班级的信息,注意,要建立各信息间的关联
//学生类
package demo;
public class Student {
private String name;
private int Cid;
private String gender;
private Parents[] parents;
private Teacher[] teacher;
private ClassGrade cg;
public Student() {
}
public Student(String name, int cid, String gender) {
this.name = name;
this.Cid = cid;
this.gender = gender;
}
public String getInfo() {
return "学生姓名:" + this.name + ",学号:" + this.Cid + ",学生性别:" + this.gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCid() {
return Cid;
}
public void setCid(int cid) {
Cid = cid;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Parents[] getParents() {
return parents;
}
public void setParents(Parents[] par) {
this.parents = par;
}
public Teacher[] getTeacher() {
return teacher;
}
public void setTeacher(Teacher[] teacher) {
this.teacher = teacher;
}
public ClassGrade getCg() {
return cg;
}
public void setCg(ClassGrade cg) {
this.cg = cg;
}
}
//家长类
public class Parents {
private String name;
private String gender;
private Student[] student;
public Parents() {
}
public Parents(String name, String gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public Student[] getStudent() {
return student;
}
public void setStudent(Student[] student) {
this.student = student;
}
public String getInfo() {
return "家长姓名:" + this.name + ",家长性别:" + this.gender;
}
}
//班级类
public class ClassGrade {
private String id;
private String level;
private Student[] students;
private Teacher[] teachers;
public ClassGrade() {
}
public ClassGrade(String id, String level) {
this.id = id;
this.level = level;
}
public String getId() {
return id;
}
public String getLevel() {
return level;
}
public void setId(String id) {
this.id = id;
}
public void setLevel(String level) {
this.level = level;
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public Teacher[] getTeachers() {
return teachers;
}
public void setTeachers(Teacher[] teachers) {
this.teachers = teachers;
}
public String getInfo() {
return "班级编号:" + this.id + ",类别:" + this.level;
}
}
//教师类
public class Teacher {
private String name;
private String subject;
private Student[] students;
private ClassGrade[] cgs;
public Teacher() {
}
public Teacher(String name, String subject) {
this.name = name;
this.subject = subject;
}
public String getName() {
return name;
}
public String getSubject() {
return subject;
}
public void setName(String name) {
this.name = name;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public ClassGrade[] getCgs() {
return cgs;
}
public void setCgs(ClassGrade[] cgs) {
this.cgs = cgs;
}
public String getInfo() {
return "教师姓名:" + this.name + ",所教科目:" + this.subject;
}
}
//静态测试
public class SchoolManagement {
// public static void main(String[] args) {
// Student a = new Student("张三", 1601, "男");
// Student b = new Student("李四", 1602, "女");
// Student c = new Student("王二小", 1501, "男");
// Student d = new Student("孙骁骁", 1502, "女");
// Student e = new Student("李大伟", 1701, "男");
// Student f = new Student("赵子龙", 1702, "男");
// Parents a1 = new Parents("张钱", "男");
// Parents a2 = new Parents("孙倩", "女");
// Parents b1 = new Parents("李勇", "男");
// Parents b2 = new Parents("李妈妈", "女");
// Parents c1 = new Parents("王二爸", "男");
// Parents c2 = new Parents("王二妈", "女");
// Parents d1 = new Parents("孙骁骁爸", "男");
// Parents d2 = new Parents("孙骁骁妈", "女");
// Parents e1 = new Parents("李大伟爸", "男");
// Parents e2 = new Parents("李大伟妈", "女");
// Parents f1 = new Parents("赵子龙爸", "男");
// Parents f2 = new Parents("赵子龙妈", "女");
// Teacher t1 = new Teacher("龙思文", "数学");
// Teacher t2 = new Teacher("孙丽", "数学");
// Teacher t3 = new Teacher("李文", "语文");
// Teacher t4 = new Teacher("赵丽颖", "语文");
// Teacher t5 = new Teacher("万芳", "英语");
// Teacher t6 = new Teacher("王林杰", "英语");
// ClassGrade g1 = new ClassGrade("一六班", "实验班");
// ClassGrade g2 = new ClassGrade("一五班", "重点班");
// ClassGrade g3 = new ClassGrade("一七班", "普通班");
// a.setCg(g1);
// b.setCg(g1);
// c.setCg(g2);
// d.setCg(g2);
// e.setCg(g3);
// f.setCg(g3);
// g1.setStudents(new Student[] { a, b });// 省略班级里添加学生1对多;
// // a.setParents(new Parents[] { a1, a2 });// 省略父母添加学生;
// // b.setParents(new Parents[] { b1, b2 });
// // c.setParents(new Parents[] { c1, c2 });
// // d.setParents(new Parents[] { d1, d2 });
// // e.setParents(new Parents[] { e1, e2 });
// // f.setParents(new Parents[] { f1, f2 });
// g1.setTeachers(new Teacher[] { t1, t3, t5 });
// g2.setTeachers(new Teacher[] { t1, t4, t5 });
// g3.setTeachers(new Teacher[] { t2, t4, t6 });
// for (Student s : g1.getStudents()) {
// System.out.println(s.getInfo());
// for (Parents p : s.getParents())
// System.out.println(" |-" + p.getInfo());
// System.out.println(" |-" + s.getCg().getInfo());
// for (Teacher t : s.getCg().getTeachers()) {
// System.out.println(" |-" + t.getInfo());
// }
//
// }
//
// }
}
//录入信息
import java.util.Scanner;
public class InfoEnter {
static Student[] Stu = new Student[100];// 定义学生数组,容量为100
static Parents[] Par = new Parents[50];
static ClassGrade[] Clg = new ClassGrade[6];
static Teacher[] Tea = new Teacher[50];
static int number = 0;
static Scanner scanner = new Scanner(System.in);
{
int choice;
System.out.println("这是一个学生信息管理系统,最大容量为100,欢迎您对本系统的使用");
while (true) {
System.out.println("1.学生录入");
System.out.println("2.班级录入");
System.out.println("3.学生信息");
System.out.println("4.信息删除");
System.out.println("5.信息删除");
System.out.println("6.详细信息");
System.out.println("7.退出");
System.out.println("请输入你要进行的操作");
choice = scanner.nextInt();
if (choice == 7)
break;
switch (choice) {
case 1:
addStu();
break;// 添加学生信息
case 2:
addClg();
break; // 添加班级信息
case 3:
show();
break;// 查询学生信息
case 4:
change();
break;// 修改学生信息
case 5:
delete();
break;// 删除学生信息
case 6:
query();
break;// 查询学生信息
}
}
System.out.println("感谢您对本系统的使用,欢迎下次继续使用");
}
static void addStu() {// 添加学生信息
while (true) {
System.out.println("点击任意键添加学生信息,停止添加输入0");
if (scanner.nextInt() == 0)
break;
System.out.println("请输入该学生的学号,姓名,性别");
Stu[number] = new Student();
Stu[number].setCid(scanner.nextInt());
Stu[number].setName(scanner.next());
Stu[number].setGender(scanner.next());
System.out.println("添加成功");
System.out.println("请添加该学生父母信息:");
addPar();
Stu[number].setParents(Par);
Stu[number].setCg(Clg[number]);
Stu[number].setTeacher(Tea);
number++;
}
}
static void addPar() {// 添加学生父母信息
while (true) {
System.out.println("点击任意键继续添加学生父母信息,停止添加输入0");
if (scanner.nextInt() == 0)
break;
System.out.println("请输入该学生家长的姓名,性别");
Par[number] = new Parents();
Par[number].setName(scanner.next());
Par[number].setGender(scanner.next());
System.out.println("添加成功");
}
}
static void addClg() {// 添加学生班级信息
while (true) {
System.out.println("点击任意键添加班级信息,停止添加输入0");
if (scanner.nextInt() == 0)
break;
System.out.println("请输入班级编号,类别");
Clg[number] = new ClassGrade();
Clg[number].setId(scanner.next());
Clg[number].setLevel(scanner.next());
System.out.println("添加成功");
System.out.println("请添加班级教师信息:");
addTea();
number++;
}
}
static void addTea() {// 添加科任老师信息
while (true) {
System.out.println("点击任意键继续科任老师,停止添加输入0");
if (scanner.nextInt() == 0)
break;
System.out.println("请输入该学生科任老师的姓名,所教科目");
Tea[number] = new Teacher();
Tea[number].setName(scanner.next());
Tea[number].setSubject(scanner.next());
System.out.println("添加成功");
}
}
static void show() {// 展示学生信息
System.out.println("*******************************************");
System.out
.println(" 学号 姓名 性别 ");
for (int i = 0; i < number; i++)
System.out.println(Stu[i].getCid() + "\t" + Stu[i].getName() + "\t"
+ "\t" + Stu[i].getGender());
System.out.println("*******************************************");
System.out.println();
}
static void change() {// 改变学生信息
System.out.println("请输入要修改信息的学生学号");
String Stunumber = scanner.next();
for (int i = 0; i < number; i++) {
if (Stunumber.equals(Stu[i].getCid())) {
System.out.println("请输入要修改的学生的信息,学号,姓名,性别,");
Stu[i].setCid(scanner.nextInt());
Stu[i].setName(scanner.next());
Stu[i].setGender(scanner.next());
break;
}
System.out.println("对不起查无此人");
}
}
static void query() {// 查询
System.out.println("请输入要查询的学生姓名");
String Stunumber = scanner.next();
for (Student s : Stu) {
if (Stunumber.equals(s.getName())) {
System.out
.println("*******************************************");
System.out.println(s.getInfo());
for (Parents p : s.getParents()) {
System.out.println(" |-" + p.getInfo());
}
System.out.println(" |-" + s.getCg().getInfo());
for (Teacher t : s.getCg().getTeachers()) {
System.out.println(" |-" + t.getInfo());
}
}
}
System.out.println("对不起查无此人");
}
static void delete() {// 删除学生信息
System.out.println("请输入要删除信息的学生学号");
String Stunumber = scanner.next();
int i;
for (i = 0; i < number; i++)
if (Stunumber.equals(Stu[i].getCid()))
break;
if (number == i)
System.out.println("对不起查无此人");
for (; i < number - 1; i++)
Stu[i] = Stu[i + 1];
number--;
System.out.println("删除完成");
}
}
//测试
public class MianStudent {
public static void main(String[] args) {
new InfoEnter();
}
}