java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java学生教师管理系统

java实现学生教师管理系统

作者:Zyt119977

这篇文章主要为大家详细介绍了java实现学生教师管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现学生教师管理系统的具体代码,供大家参考,具体内容如下

类设计

1.1 父类抽象类

1.2 子类Student

1.3 子类Teacher

1.4 工具类

1.5 启动类

定义启动类:MainApp启动程序

程序设计

2.1 父类Person类

/*
成员属性有id 名字 性别 年龄 生日 和年龄
成员方法有描述自己的职位和工作
*/
public abstract class Person {
 private int id;
 private String name;
 private String gender;
 private String birthday;
 private int age;

 public Person() {
 }

 public Person(int id, String name, String gender, String birthday) {
  this.id = id;
  this.name = name;
  this.gender = gender;
  this.birthday = birthday;

 }

 public abstract String getType();

 public abstract String getWork();

 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
 public String getBirthday() {
  return birthday;
 }
 public void setBirthday(String birthday) {
  this.birthday = birthday;
 }
 //getAge需要接收的数据需要通过Utils的birthday方法进行计算
 public int getAge() {
  age = Utils.birthday(this.getBirthday());
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 @Override
 public String toString() {
  return   id + "\t\t" +
      name + "\t\t" +
      gender + "\t\t" +
      birthday + "\t" +
      this.getAge() + "\t\t" +
      "我是一名" + getType() + "我的工作是" + getWork();
 }
}

2.2 学生类

/*
重写父类方法
*/
public class Stuent extends Person {
 public Stuent() {
 }

 public Stuent(int id, String name, String gender, String birthday) {
  super(id, name, gender, birthday);
 }

 @Override
 public String getType() {
  return "学习Java";
 }

 @Override
 public String getWork() {
  return "学生";
 }
}

2.3 老师类

/*
重写父类方法
*/
public class Teacher extends Person {
 public Teacher() {
 }

 public Teacher(int id, String name, String gender, String birthday) {
  super(id, name, gender, birthday);
 }

 @Override
 public String getType() {
  return "教师";
 }

 @Override
 public String getWork() {
  return "讲课";
 }
}

2.4 工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
/*
学员ID值(添加学员信息时,编号由此ID加1生成)
教师ID值(添加教师信息时,编号由此ID加1生成)
全局方法
根据生日计算年龄的方法 打印一个Person对象的方法;
 打印一个ArrayList<? extends Person>集合的方法;
 */
public class Utils {
 public static int stuID;
 public static int teaID;
 public static int birthday(String birthday) {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  Date birthdayDate =null;
  try {birthdayDate = sdf.parse(birthday);
  } catch (ParseException e) { return -1;
  //输入的日期不符合模式,返回-1  
  }
  //创建生日日历对象
  Calendar c = Calendar.getInstance();
  //求出生日时间传给日历对象
  c.setTime(birthdayDate);
  //求出生日年份
  c.get(Calendar.YEAR);
  //创建档当前时间的日历对象
  Calendar c1 = Calendar.getInstance();
  c1.get(Calendar.MONTH);
  if (c1.before(c)) {
   return -1;
  }
  //判断月份大小
  if (c.get(Calendar.MONTH) > c1.get(Calendar.MONTH)) {
   return c1.get(Calendar.YEAR) - c.get(Calendar.YEAR) - 1;
  } else if (c.get(Calendar.MONTH) < c1.get(Calendar.MONTH)) {
   return c1.get(Calendar.YEAR) - c.get(Calendar.YEAR);
  } else{
   //判断日期大小
   if (c.get(Calendar.DAY_OF_MONTH) > c1.get(Calendar.DAY_OF_MONTH)) {
    return c1.get(Calendar.YEAR) - c.get(Calendar.YEAR) - 1;
   } else if (c.get(Calendar.DAY_OF_MONTH) < c1.get(Calendar.DAY_OF_MONTH)) {
    return c1.get(Calendar.YEAR) - c.get(Calendar.YEAR);
   } else{
    //今天过生日默认增加一岁
    return c1.get(Calendar.YEAR) - c.get(Calendar.YEAR);
   }
  }
 }
 //打印ArrayList的方法
 public static void PersonList(ArrayList<? extends Person> personlist) {
  System.out.println("***************************************************");
  System.out.println("编号\t\t姓名\t\t性别\t\t生日\t\t\t年龄\t\t描述");
  for (int i = 0; i < personlist.size(); i++) {
   System.out.println(personlist.get(i));
  }
 }
 //打印Person的方法
 public static void printPerson(Person person) {
  System.out.println("***************************************************");
  System.out.println("编号\t\t姓名\t\t性别\t\t生日\t\t\t年龄\t\t描述");
  System.out.println(person);
 }
}

2.5 启动类

import java.util.ArrayList;
import java.util.Scanner;

public class MainApp {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);

  //学生集合
  ArrayList<Stuent> stuents = new ArrayList<>();
  //老师集合
  ArrayList<Teacher> teachers = new ArrayList<>();

  while (true) {
   System.out.println("1.学员信息管理 2.教师信息管理 3.退出");
   int a = sc.nextInt();
   switch (a) {
    case 1:
     studentManage(stuents, sc);
     break;
    case 2:
     teacherManage(teachers, sc);
     break;
    case 3:
     System.out.println("谢谢使用");
     System.exit(0);
    default:
     System.out.println("输入有误");
     break;
   }
  }

 }
 //教师二级列表
 private static void teacherManage(ArrayList<Teacher> teachers, Scanner sc) {
  while (true) {
   System.out.println("教师管理系统");
   System.out.println("1.添加教师信息 2.修改教师信息 3.删除教师信息 4.查询教师 5.返回");
   int a = sc.nextInt();
   switch (a) {
    case 1:
     addTeacher(teachers, sc);
     break;
    case 2:
     updateTeacher(teachers, sc);
     break;
    case 3:
     delTeacher(teachers, sc);
     break;
    case 4:
     findTeacher(teachers);
     break;
    case 5:
     return;
   }
  }
 }
 //教师添加
 private static void addTeacher(ArrayList<Teacher> teachers, Scanner sc) {
  System.out.println("输入姓名");
  String name = sc.next();
  System.out.println("输入性别");
  String gender = sc.next();
  System.out.println("输入生日");
  String birthday = sc.next();
  teachers.add(new Teacher(++Utils.teaID, name, gender, birthday));
  System.out.println("添加成功");
 }
 //修改教师信息
 private static void updateTeacher(ArrayList<Teacher> teachers, Scanner sc) {
  System.out.println("请输入要改教师的id");
  int updateid = sc.nextInt();
  for (int i = 0; i < teachers.size(); i++) {
   Teacher tea = teachers.get(i);
   if (tea.getId() == updateid) {
    Utils.printPerson(tea);
    System.out.println("其输入新的姓名(保留输入0)");
    String newName = sc.next();
    System.out.println("输入新的性别(保留输入0)");
    String newGender = sc.next();
    System.out.println("输入新的生日(保留输入0)");
    String newbirthday = sc.next();

    if (!"0".equals(newName)) {
     tea.setName(newName);
    }
    if (!"0".equals(newGender)) {
     tea.setGender(newGender);
    }
    if (!"0".equals(newbirthday)) {
     tea.setBirthday(newbirthday);
    }
    System.out.println("修改成功");
    return;
   }
  }
  System.out.println("没有找到" + updateid + "id");
 }
 //删除教师信息
 private static void delTeacher(ArrayList<Teacher> teachers, Scanner sc) {

  System.out.println("请输入要删除学教师的的id");
  int delid = sc.nextInt();
  for (int i = 0; i < teachers.size(); i++) {
   Teacher tea = teachers.get(i);
   //
   if (delid == tea.getId()) {
    //输出要删除的信息
    Utils.printPerson(tea);
    //判断是否要删除信息
    System.out.println("是否要删除这条信息(y/n)");
    String input = sc.next();
    if ("y".equals(input)) {
     teachers.remove(i);
     System.out.println("删除成功");
     return;
    } else if ("n".equals(input)) {
     System.out.println("操作被取消");
     return;
    }
   }
  }
  System.out.println("没有找到id为" + delid + "的成员");
 }
 //查询教师信息
 private static void findTeacher(ArrayList<Teacher> teachers) {
  if (teachers.size() == 0) {
   System.out.println("查询不到数据");
  } else {
    Utils.PersonList(teachers);
  }
 }

 //学生二级列表
 private static void studentManage(ArrayList<Stuent> stuents, Scanner sc) {
  while (true) {
   System.out.println("学生管理系统");
   System.out.println("1.添加学员 2.修改学员 3.删除学员 4.查询学员 5.返回");
   int a = sc.nextInt();
   switch (a) {
    case 1:
     addStudent(stuents, sc);
     break;
    case 2:
     updateStudent(stuents, sc);
     break;
    case 3:
     delStudent(stuents, sc);
     break;
    case 4:
     findStudent(stuents);
     break;
    case 5:
     return;
   }
  }
 }
 //添加学生信息
 private static void addStudent(ArrayList<Stuent> stuents, Scanner sc) {
  System.out.println("输入姓名");
  String name = sc.next();
  System.out.println("输入性别");
  String gender = sc.next();
  System.out.println("输入生日");
  String birthday = sc.next();
  stuents.add(new Stuent(++Utils.stuID, name, gender, birthday));
  System.out.println("添加成功");
 }
 //修改学生信息
 private static void updateStudent(ArrayList<Stuent> stuents, Scanner sc) {
  System.out.println("请输入要改学生的id");
  int updateid = sc.nextInt();
  for (int i = 0; i < stuents.size(); i++) {
   Stuent stu = stuents.get(i);
   if (stu.getId() == updateid) {
    Utils.printPerson(stu);
    System.out.println("其输入新的姓名(保留输入0)");
    String newName = sc.next();
    System.out.println("输入新的性别(保留输入0)");
    String newGender = sc.next();
    System.out.println("输入新的生日(保留输入0)");
    String newbirthday = sc.next();

    if (!"0".equals(newName)) {
     stu.setName(newName);
    }
    if (!"0".equals(newGender)) {
     stu.setGender(newGender);
    }
    if (!"0".equals(newbirthday)) {
     stu.setBirthday(newbirthday);
    }
    System.out.println("修改成功");
    return;
   }
  }
  System.out.println("没有找到" + updateid + "id");
 }
 //删除学生信息
 private static void delStudent(ArrayList<Stuent> stuents, Scanner sc) {
  System.out.println("请输入要删除学生的的学号");
  int delid = sc.nextInt();
  for (int i = 0; i < stuents.size(); i++) {
   Stuent stu = stuents.get(i);
   //
   if (delid == stu.getId()) {
    //输出要删除的信息
    Utils.printPerson(stu);
    //判断是否要删除信息
    System.out.println("是否要删除这条信息(y/n)");
    String input = sc.next();
    if ("y".equals(input)) {
     stuents.remove(i);
     System.out.println("删除成功");
     return;
    } else if ("n".equals(input)) {
     System.out.println("操作被取消");
     return;
    }
   }
  }
  System.out.println("没有找到id为" + delid + "的成员");
 }
 //查找学生信息
 private static void findStudent(ArrayList<Stuent> stuents) {
  System.out.println("查询结果");
  if (stuents.size() == 0) {
   System.out.println("没有数据");
  } else {
   Utils.PersonList(stuents);
  }
 }

}

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文