实验题目:
编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日,则将该雇员的工资增加100元。
实验目的:对象、继承、封装、多态、抽象类的组合应用。
实验说明:
(1)定义一个Employee类,该类包含:
private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
abstract方法earnings();toString()方法输出对象的name,number和birthday。
(2)MyDate类包含:
private成员变量month,day,year;
toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
(3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。该类包括:
private成员变量monthlySalary;
实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输出员工类型信息及员工的name,number,birthday。
(4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括:
private成员变量wage和hour;
实现父类的抽象方法earnings(),该方法返回wage*hour值;toString()方法输出员工类型信息及员工的name,number,birthday。
(4)定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。
提示:
//定义People类型的数组
People c1[]=new People[10];
//数组元素赋值
c1[0]=new People(“John”,“0001”,20);
c1[1]=new People(“Bob”,“0002”,19);
//若People有两个子类Student和Officer,则数组元素赋值时,可以使父类类型的数组元素指向子类。
c1[0]=new Student(“John”,“0001”,20,85.0);
c1[1]=new Officer(“Bob”,“0002”,19,90.5);
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
abstract class Employee{
private String name; //名字
private int number; //编号
private MyDate birthday; //出生日期
Employee(){}
Employee(String name,int number,MyDate birthday){
this.name=name;
this.number=number;
this.birthday=birthday;
}
abstract int earnings(); //抽象方法
public String toString(){
return “姓名:”+getName()+" 员工编号:"+getNumber()+" 出生日期:"+birthday.toDateString();
}
String getName(){
return this.name;
}
int getNumber(){
return this.number;
}
MyDate getBirthay(){
return this.birthday;
}
void setName(String name){
this.name=name;
}
void setNumber(int number){
this.number=number;
}
void setBirthay(MyDate birthday){
this.birthday=birthday;
}
}
class MyDate{
private int year,month,day;
MyDate(){}
MyDate(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
int getYear(){
return this.year;
}
int getMonth(){
return this.month;
}
int getDay(){
return this.day;
}
void setYear(int year){
this.year=year;
}
void setMonth(int month){
this.month=month;
}
void setDay(int day){
this.day=day;
}
String toDateString(){
return getYear()+"年 "+this.getMonth()+"月 "+this.getDay()+"日";
}
}
class SalariedEmployee extends Employee{
private int monthlySalary;//月薪
SalariedEmployee(){
super();
}
SalariedEmployee(String name,int number,MyDate birthday,int monthlySalary){
super(name,number,birthday);
this.monthlySalary=monthlySalary;
}
int getMonthlySakary(){
return this.monthlySalary;
}
void setMonthaySalary(int monthlySalary){
this.monthlySalary=monthlySalary;
}
int earnings(){ //工资
return getMonthlySakary();
}
public String toString(){
return "合同工 "+super.toString();
}
}
class HourEmployee extends Employee{
private int wage,hour;
HourEmployee(){
super();
}
HourEmployee(String name,int number,MyDate birthday,int wage,int hour){
super(name,number,birthday);
this.wage=wage;
this.hour=hour;
}
//小时工工资
int earnings(){
return this.getWage()*getHour();
}
public String toString(){
return “小时工 “+ super.toString();
//return “合同工 “+“姓名:”+getName()+” 员工编号:”+getNumber()+” 出生日期:”+birthay.toDateString();
}
int getWage(){
return this.wage;
}
int getHour(){
return this.hour;
}
void setWage(int wage){
this.wage=wage;
}
void setHour(int hour){
this.hour=hour;
}
}
class PayrollSystem{
Employee[] createEmployARR (Employee e ,Employee[] empArr){
for(int x;x<empArr.length;x++){
empArr[x]=e;
}
return empArr;
}
Employee getEmp(){
///从数据库里面读出来数据 JDBC IO
Employee e = new Employee();
return e
}
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MM");
String dateString = formatter.format(currentTime);
return dateString;
}
boolean judegment(Employee e){
String date_str = PayrollSystem.getStringDate();
char[] x = date_str.toCharArray();
//char - int
// String ---》int
String r = x[1]+"" ;
currentMonth = Integer.parseInt(r);
if(e != null){
if( e.getMonth() == currentMonth ){
e.earnings()+100;
}
}
return false;
}
}
class Test{
public static void main(String[] args){
/*
Employee[] emp= new Employee[6];
emp[0]=new SalariedEmployee(“赵十一”,001,new MyDate(1990,2,17),4000);
emp[1]=new SalariedEmployee(“赵十二”,002,new MyDate(1991,4,27),5000);
emp[2]=new SalariedEmployee(“赵十三”,003,new MyDate(1992,6,11),6000);
emp[3]=new HourEmployee("十一",004,new MyDate(1980,3,17),40,2);
emp[4]=new HourEmployee("十二",005,new MyDate(1981,5,27),50,3);
emp[5]=new HourEmployee("十三",006,new MyDate(1982,7,11),60,5);
System.out.println("请在控制台输入月份");
Scanner sc=new Scanner(System.in);
int y=sc.nextInt();
System.out.println("****************************财务单****************************");
System.out.println();
for(int x=0;x<emp.length;x++){
if(emp[x].getBirthay().getMonth() == y){
System.out.println("--------------------------------------------------------------");
System.out.println(emp[x].toString());
System.out.println();
System.out.println("生日 工资加一百");
System.out.println();
System.out.println("实得工资:"+(emp[x].earnings()+100));
System.out.println("--------------------------------------------------------------");
}else{
System.out.println(emp[x].toString());
System.out.println("实得工资:"+emp[x].earnings());
}
System.out.println();
}
System.out.println("**************************************************************");
*/
Employee[] emp= new Employee[6];
PayrollSystem ps = new PayrollSystem()
emp = ps.createEmployARR(emp);
emp.judegment()
}
}