PTA 校园角色类设计-2 JAVA

该博客介绍了一个Java编程实现的学校综合系统,包括Student、Faculty和Staff三个角色类。每个角色都有基本属性如姓名、年龄、性别和电话,而学生有学号、班级职务,教员有职称和专业,职员有职位称号和部门信息。博客通过多态展示了如何根据用户输入显示不同角色的信息。

在校园角色类设计-1题的基础上,增加了部分要求,加粗部分为增加的内容: 学校需要构建综合系统,使用者包含多种角色。角色Role分两类:学生Student和雇员Employee;雇员又分为教员Faculty和职员Staff。 每个角色都有姓名、年龄、性别、电话号码。学生有学号、班级、班级职务(班长、副班长、学习委员等)。一个雇员有工号、入职日期、所在部门。教员有职称和专业。职员有职位称号。 请以如下Main类为基础,构建各个角色类,将代码补充完整。 public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Faculty fac = new Faculty(“张三”,32,“33006”,“2019”,“10”,“25”,“讲师”); Student stu=new Student(“李四”,19,“20201103”,“202011”); Staff sta = new Staff(“王五”,27,“32011”,“2015”,“06”,“17”,“教务员”);
fac.setSex(‘男’); fac.setTel(“13600770077”); fac.setDepartment(“数信学院”); fac.setMajor(“数学”); stu.setSex(‘女’); stu.setTel(“18000009999”); stu.setPosition(“班长”); sta.setSex(‘男’); sta.setTel(“18966666666”); sta.setDepartment(“航制学院”); Scanner input = new Scanner(System.in); int i = input.nextInt(); /* 使用多态根据用户的输入,输出不同的角色人物的信息, 输入1:教员 输入2:学生 输入3:职员 其他输入:输出"Wrong Format" */ } }

import java.util.Arrays;
import java.util.Scanner;

class Role{
    String name;
    int age;
    char sex;
    String tel;

    Role(String name, int age){
        this.name = name;
        this.age = age;
    }
}

class Faculty extends Role{
    private String id;
    private String f_name;

    private String year;
    private String month;
    private String day;


    private String department;
    private String major;

    Faculty(String name, int age, String id, String year, String month, String day, String f_name){
        super(name, age);
        this.id = id;
        this.year = year;
        this.month = month;
        this.day = day;
        this.f_name = f_name;
    }

    public void show(){
        System.out.println("我是" + super.name + "," + this.sex +  ",年龄" + super.age + "岁。电话是" + super.tel +  "。工号是" + this.id + "," + this.year + "年" + Integer.parseInt(this.month) + "月" + Integer.parseInt(this.day) + "日入职。" + "就职于"+ this.department + "。是一名教师," +  this.major + "专业," + this.f_name + "职称。");
    }

    public void setTel(String tel) {
        super.tel = tel;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public void setSex(char sex) {
        super.sex = sex;
    }
}

class Student extends Role{
    private String id;
    private String cl;

    private String position;

    Student(String name, int age, String id, String cl){
        super(name, age);
        this.cl = cl;
        this.id = id;
    }

    public void show(){
        System.out.println("我是" + super.name + "," + this.sex +  ",年龄" + super.age + "岁。电话是" + super.tel + "。学号是" + id + ",来自" + cl + "班。" + "担任" + this.position + "职务。");
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public void setSex(char sex){
        super.sex = sex;
    }

    public void setTel(String tel){
        super.tel = tel;
    }
}

class Staff extends Role{
    private String id;
    private String f_name;

    private String year;
    private String month;
    private String day;

    private String department;

    Staff(String name, int age, String id, String year, String month, String day, String f_name){
        super(name, age);
        this.id = id;
        this.year = year;
        this.month = month;
        this.day = day;
        this.f_name = f_name;
    }

    public void show(){
        System.out.println("我是" + super.name + "," + this.sex +  ",年龄" + super.age + "岁。电话是" + super.tel + "。工号是" + this.id + "," + this.year + "年" + Integer.parseInt(this.month) + "月" + Integer.parseInt(this.day) + "日入职。就职于" + this.department + "。是一名" + this.f_name + "。");
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public void setSex(char sex){
        super.sex = sex;
    }

    public void setTel(String tel){
        super.tel = tel;
    }
}

public class Main { public static void main(String[] args) { // TODO Auto-generated method stub
    Faculty fac = new Faculty("张三",32,"33006","2019","10","25","讲师");
    Student stu=new Student("李四",19,"20201103","202011");
    Staff sta = new Staff("王五",27,"32011","2015","06","17","教务员");

    fac.setSex('男');
    fac.setTel("13600770077");
    fac.setDepartment("数信学院");
    fac.setMajor("数学");

    stu.setSex('女');
    stu.setTel("18000009999");
    stu.setPosition("班长");

    sta.setSex('男');
    sta.setTel("18966666666");
    sta.setDepartment("航制学院");
    Scanner input = new Scanner(System.in);
    int i = input.nextInt(); /* 使用多态根据用户的输入,输出不同的角色人物的信息, 输入1:教员 输入2:学生 输入3:职员 其他输入:输出"Wrong Format" */

    if(i == 1){
        fac.show();
    }
    else if(i == 2){
        stu.show();
    }
    else if(i == 3){
        sta.show();
    }
    else{
        System.out.println("Wrong Format");
    }
} }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值