java实现信息管理系统案例(多态)

/*
    成员属性:
        id(编号)
        name(姓名)
        sex(性别)
        birthday(生日)
        age(年龄-由生日计算得出)
    构造方法:
        无参构造
        全参构造
    成员方法:
        toString()
    抽象方法:
        getType():由各子类实现,返回各自的"类型"字符串。
        getWork():由各子类实现,返回各自的"工作"字符串。
 */
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, int age) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.birthday = birthday;
        this.age = age;
    }

    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;
    }

    @Override
    public String toString() {
        return "**************************************************************************************\n"
                + "编号\t\t姓名\t\t性别\t\t生日\t\t年龄\t\t描述\n" +
                id + "\t\t" +
                name + "\t\t" +
                gender + "\t\t" +
                birthday + "\t\t" +
                Utils.birthdayToAge(birthday) + "\t\t" +
                "我是一名" + getType() + ";" + "我的工作是" + getWork() +
                "\n**************************************************************************************";

    }

    public abstract String getType();

    public abstract String getWork();
}
/*
    学生类:
        构造方法
            无参构造
            全参构造(super调用父类全参构造)
        重写抽象方法
            重写getType()
            重写getWork()
 */
public class Student extends Person{
    public Student() {
    }

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

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

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

/*
    教师类:
        构造方法
            无参构造
            全参构造(super调用父类全参构造)
        重写抽象方法
            重写getType()
            重写getWork()
 */
public class Teacher extends Person{
    public Teacher() {
    }

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

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

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

import java.util.Scanner;

public class Manager {
    //这里使用多态,将Student和Teacher的父类作为参数
    public static void personManager(Person p) {
        Scanner sc = new Scanner(System.in);

        while (true) {
            //获取当前引用的Type值
            String type = p.getType();
            System.out.println("1.添加" + type + "\t\t" + "2.修改" + type + "\t\t" + "3.删除" + type + "\t\t" + "4.查询" + type + "\t\t" + "5.返回");
            System.out.println("请输入功能序号:");
            String choose = sc.next();
            System.out.println("----------------------------------------------");
            switch (choose) {
                case "1":
                    DAO.add(p);
                    System.out.println("【添加" + type + "成功】");
                    System.out.println("----------------------------------------------");
                    break;
                case "2":
                    DAO.update(p);
                    System.out.println("----------------------------------------------");
                    break;
                case "3":
                    DAO.delete(p);
                    System.out.println("----------------------------------------------");
                    break;
                case "4":
                    System.out.println("【查询结果】");
                    DAO.find(p);
                    System.out.println("----------------------------------------------");
                    break;
                case "5":
                    System.out.println("【返回上一级菜单】");
                    System.out.println("----------------------------------------------");
                    return;
                default:
                    System.out.println("您输入的数据有误!");
                    System.out.println("----------------------------------------------");
                    break;
            }
        }
    }
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.itheima.demo02.domain.Person;

/*
    工具类:
        全局变量
            学员ID值(添加学员信息时,编号由此ID加1生成)
            教师ID值(添加教师信息时,编号由此ID加1生成)
        全局方法
            根据生日计算年龄的方法
            打印一个Person对象的方法;
            打印一个ArrayList集合的方法;

 */
public class Utils {
    public static int stuID;
    public static int teaID;

    //使用静态代码块,实现在Utils类加载的时候为两个常量赋值
    static {
        stuID = 0;
        teaID = 0;
    }

    public static int birthdayToAge(String birthday) {
        //获取当前日期对象
        Calendar cal = Calendar.getInstance();
        Date birth = null;
        try {
            birth = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
        //获取当前系统的年月日
        int nowYear = cal.get(Calendar.YEAR);
        int nowMonth = cal.get(Calendar.MONTH);
        int nowDay = cal.get(Calendar.DAY_OF_MONTH);

        //把当前时间设置成生日日期
        cal.setTime(birth);
        //获取生日的年月日
        int birthYear = cal.get(Calendar.YEAR);
        int birthMonth = cal.get(Calendar.MONTH);
        int birthDay = cal.get(Calendar.DAY_OF_MONTH);
        //计算当前时间距出生日期有多少年
        int age = nowYear - birthYear;
        //如果当前月份小于等于出生月份,说明不满周岁,则age减1
        if (nowMonth <= birthMonth) {
            //如果两个月份一样,则比较day
            if (nowMonth == birthMonth) {
                //如果nowDay小于birthDay,则age--
                if (nowDay < birthDay) {
                    age--;
                }
            } else {
                age--;
            }
        }
        return age;
    }

    public static void printPerson(Person person) {
        System.out.println(person);
    }

    public static void printArrayList(ArrayList<? extends Person> list) {

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
import java.util.Scanner;

/*
    测试类
 */
public class MainApp {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("1.学员信息管理\t\t2.教师信息管理\t\t3.退出");
            System.out.println("请输入功能序号:");
            String choose = sc.next();
            System.out.println("----------------------------------------------");
            switch (choose){
                case "1":
                    System.out.println("【学员信息管理】");
                    Person s = new Student();
                    Manager.personManager(s);
                    break;
                case "2":
                    System.out.println("【教师信息管理】");
                    Person t = new Teacher();
                    Manager.personManager(t);
                    break;
                case "3":
                    System.out.println("谢谢使用!欢迎下次再来!");
                    System.exit(0);
                default:
                    System.out.println("您输入的数据有误!请重新输入...");
                    break;
            }
        }
    }
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值