用Java写PTA 7-10 校园角色类设计-2
在校园角色类设计-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"
*/
}
}
输入格式:
数值
输出格式:
人物的基本信息。根据角色的不同而有差别。
输入样例1:
在这里给出一组输入。例如:
1
输出样例1:
在这里给出相应的输出。例如:
我是张三,男,年龄32岁。电话是13600770077。工号是33006,2019年10月25日入职。就职于数信学院。是一名教师,数学专业,讲师职称。
输入样例2:
在这里给出一组输入。例如:
2
输出样例2:
在这里给出相应的输出。例如:
我是李四,女,年龄19岁。电话是18000009999。学号是20201103,来自202011班。担任班长职务。
输入样例3:
在这里给出一组输入。例如:
3
输出样例3:
在这里给出相应的输出。例如:
我是王五,男,年龄27岁。电话是18966666666。工号是32011,2015年6月17日入职。就职于航制学院。是一名教务员。
输入样例3:
在这里给出一组输入。例如:
5
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
import java.util.Scanner;
class Faculty{
String name;
int age;
String id;
char sex;
String year,month,day;
String title;
String major;
String tel;
String department;
Faculty(String name,int age,String id,String year,String month,String day,String title){
this.name=name;
this.age=age;
this.id=id;
this.year=year;
this.month=month;
this.day=day;
this.title=title;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
void show(){
System.out.println("我是"+name+","+sex+",年龄"+age+"岁。电话是"+tel+"。工号是"+id+","+year+"年"+month+"月"+day+"日入职。就职于"+department+"。是一名教师,"+major+"专业,"+title+"职称。");
}
}
class Student{
String name;
int age;
String id;
char sex;
String bj;
String tel;
String position;
Student(String name,int age,String id,String bj){
this.name=name;
this.age=age;
this.id=id;
this.bj=bj;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
void show(){
System.out.println("我是"+name+","+sex+",年龄"+age+"岁。电话是"+tel+"。学号是"+id+",来自"+bj+"班。担任"+position+"职务。");
}
}
class Staff{
String name;
int age;
String id;
char sex;
String year,month,day;
String title;
String tel;
String department;
Staff(String name,int age,String id,String year,String month,String day,String title){
this.name=name;
this.age=age;
this.id=id;
this.year=year;
this.month=month;
this.day=day;
this.title=title;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
void show(){
System.out.println("我是"+name+","+sex+",年龄"+age+"岁。电话是"+tel+"。工号是"+id+","+year+"年"+month+"月"+day+"日入职。就职于"+department+"。是一名"+title+"。");
}
}
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","6","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();
if(i==1) fac.show();
else if(i==2) stu.show();
else if(i==3) sta.show();
else System.out.println("Wrong Format");
}
}