虽然这是是一个简易的管理模块的代码块也要包含有对信息的增,删,改,查这四个基本操作
思路:我们可以抽象出两个类。一个是教师类,一个是所教授的课程类
增:我们通过老师类来new出老师的实体。通过创建老师对象的数组来存储老师对象,但是数组的长度是不能变化的,所以增的难点在于数组的扩容我们可以在定义一个比原数组长1的数组通过遍历将旧数组中的信息赋值给新数组再在新数组的最后一位添加上新输入的信息的老师对象,通过将新数组的地址赋给旧数组,这样就改变了旧数组的指向就完成了对旧数组的扩容本质上是从新指向了新的存储空间只不过把值拷贝到新数组中了
删:就是数组的缩减同样通过再定义数组就行地址传递,改变内存指向
改和查 就是遍历
Teacher.java
/**
* FileName: Teacher
* Author: 86186
* Date: 2022/3/21 10:20
* Description: Teacher
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package homeworkplus;
/**
* 〈一句话功能简述〉<br>
* 〈Teacher〉
*
* @author Hacker
* @create 2022/3/21
* @since 1.0.0
*/
public class Teacher {
private String TeacherName;
private int TeacherAge;
Courses[] courses;
public Teacher() {
}
public Teacher(String teacherName, int teacherAge, Courses[] courses) {
TeacherName = teacherName;
TeacherAge = teacherAge;
this.courses = courses;
}
public String getTeacherName() {
return TeacherName;
}
public void setTeacherName(String teacherName) {
TeacherName = teacherName;
}
public int getTeacherAge() {
return TeacherAge;
}
public void setTeacherAge(int teacherAge) {
TeacherAge = teacherAge;
}
public Courses[] getCourses() {
return courses;
}
public void setCourses(Courses[] courses) {
this.courses = courses;
}
}
Courses.java
/**
* FileName: Courses
* Author: 86186
* Date: 2022/3/21 10:21
* Description: Courses
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package homeworkplus;
/**
* 〈一句话功能简述〉<br>
* 〈Courses〉
*
* @author Hacker
* @create 2022/3/21
* @since 1.0.0
*/
public class Courses {
private String className;
private int classTime;
private int classCredit;
public Courses() {
}
public Courses(String className, int classTime, int classCredit) {
this.className = className;
this.classTime = classTime;
this.classCredit = classCredit;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getClassTime() {
return classTime;
}
public void setClassTime(int classTime) {
this.classTime = classTime;
}
public int getClassCredit() {
return classCredit;
}
public void setClassCredit(int classCredit) {
this.classCredit = classCredit;
}
}
Test.java
/**
* FileName: Test
* Author: 86186
* Date: 2022/3/21 10:19
* Description: Test
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package homeworkplus;
import java.util.Scanner;
/**
* 〈一句话功能简述〉<br>
* 〈Test〉
*
* @author Hacker
* @create 2022/3/21
* @since 1.0.0
*/
public class Test {
public static Courses[] courses;
public static Teacher[] teacher = new Teacher[0];
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
//初始化课程数组
initCourses();
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.print("请输入你的选择:");
int choice = input.nextInt();
switch (choice){
case 1:
insertTeacher();
break;
case 2:
System.out.print("请输入你要删除信息的老师姓名:");
String deleteName = input.next();
Teacher deleteT = findTeacher(deleteName);
if(deleteName != null){
deleteTeacher(deleteT);
}else{
System.out.println("系统中没有这名老师!!!");
}
break;
case 3:
System.out.print("请输入你要修改信息的老师姓名:");
String updateName = input.next();
Teacher t = findTeacher(updateName);
if(t != null){
updateTeacher(t);
}else{
System.out.println("系统中没有这名老师!!!");
}
break;
case 4:
System.out.println("菜单选择");
System.out.println("1,查询所有老师信息");
System.out.println("2,查询指定老师信息");
System.out.print("请输入你的选择:");
int chocie = input.nextInt();
if(chocie == 1){
selectTeacher();
}else{
System.out.print("请输入老师姓名:");
String selectName = input.next();
Teacher ts = findTeacher(selectName);
if(ts != null){
selectOne(ts);
}else{
System.out.println("系统不存在该名老师!!!!");
}
}
break;
default:
System.out.println("退出系统!!,欢迎下次使用");
return;
}
}
}
//查询指定老师信息
public static void selectOne(Teacher t){
System.out.println("*************************************");
System.out.println("=====" + t.getTeacherName() + "老师信息====");
System.out.println("老师姓名: " + t.getTeacherName() + "\t" + ",年龄: " + t.getTeacherAge());
for(int j = 0;j <= t.courses.length - 1;j ++){
System.out.println("课程名: " + t.courses[j].getClassName());
System.out.println("课时: " + t.courses[j].getClassTime());
System.out.println("学分: " + t.courses[j].getClassCredit());
}
System.out.println("*************************************");
}
//删除老师信息
public static void deleteTeacher(Teacher t){
Teacher[] teachers = new Teacher[teacher.length - 1];
for(int i = 0,j = 0;i < teacher.length;i ++,j ++){
if(teacher[i] == t){
j --;
continue;
}
teachers[j] = teacher[i];
}
teacher = teachers;
System.out.println("删除成功!!!!");
}
//判断老师是否存在
public static Teacher findTeacher(String teacherName){
for(Teacher t : teacher){
if(t.getTeacherName().equals(teacherName)){
return t;
}
}
return null;
}
//修改老师信息
public static void updateTeacher(Teacher t){
System.out.println("=======修改选项======");
System.out.println("1,修改老师姓名");
System.out.println("2,修改老师年龄");
System.out.print("请输入你的选择:");
int choice = input.nextInt();
if(choice == 1){
System.out.print("请输入姓名:");
String updateName = input.next();
t.setTeacherName(updateName);
}else{
System.out.print("请入年龄:");
int updateAge = input.nextInt();
t.setTeacherAge(updateAge);
}
System.out.println( "老师的个人信息修改完成!!!");
}
//显示所有老师信息
public static void selectTeacher(){
if(teacher.length == 0){
System.out.println("系统中没有老师的信息!!!!");
return;
}
System.out.println(teacher.length);
for(int i = 0;i <= teacher.length - 1;i ++){
System.out.println("***************************************************");
System.out.println("老师姓名:" + teacher[i].getTeacherName() + "\t" + "年龄: " + teacher[i].getTeacherAge());
System.out.println("======" + teacher[i].getTeacherName() + "老师的的课程信息======");
for(int j = 0;j <= teacher[i].courses.length - 1;j ++){
System.out.println("课程名: " + teacher[i].courses[j].getClassName());
System.out.println("课时: " + teacher[i].courses[j].getClassTime());
System.out.println("学分: " + teacher[i].courses[j].getClassCredit());
}
System.out.println("***************************************************");
}
}
//添加新的老师信息
public static void insertTeacher(){
System.out.print("请输入要添加的教师人数:");
int num = input.nextInt();
Teacher[] insertTeachers = new Teacher[num + teacher.length];
//将原来数组中的元素
for(int i = 0;i < teacher.length;i ++){
insertTeachers[i] = teacher[i];
}
//循环输入
for(int i = teacher.length;i < insertTeachers.length;i ++){
System.out.print("请输入第" + (i - teacher.length + 1) + "名,老师的姓名:");
String insertName = input.next();
System.out.print("请输入第" + (i - teacher.length + 1) + "名,老师的年龄:");
int insertAge = input.nextInt();
System.out.print("请输入这名老师能教授的课程数:");
int classNum = input.nextInt();
Courses[] courses = new Courses[classNum];
for(int j = 0;j < classNum;j ++){
System.out.print("请输入第" + (j + 1) + "门课:");
String className = input.next();
Courses c = findCourse(className);
if(c == null){
System.out.println("这门课程不存在,请重新输入!!!");
j --;
continue;
}
courses[j] = c;
}
Teacher t = new Teacher(insertName,insertAge,courses);
insertTeachers[i] = t;
}
teacher = insertTeachers;
}
//判断课程是否存在
public static Courses findCourse(String className){
for(int i = 0;i < courses.length;i ++){
if(courses[i].getClassName().equals(className)){
return courses[i];
}
}
return null;
}
//初始化课程数组
public static void initCourses(){
Courses c1 = new Courses("高数",40,5);
Courses c2 = new Courses("大物",30,4);
Courses c3 = new Courses("毛概",20,3);
Courses c4 = new Courses("英语",10,1);
courses = new Courses[]{c1,c2,c3,c4};
}
}