【案例介绍】
- 任务描述
某公司有多个部门,员工信息包含姓名 name、类型 type、部门 department 和底薪
basicSalary,其中员工的类型有三种:管理员、销售员和工人。
公司财务部门工作人员每月要计算员工的实发工资。
实发工资 = 月收入 – 应缴个人所得税。
其中:
( 1)月收入计算方法是根据类型来决定的:
管理员:月收入=底薪+奖金 bonus
销售员:月收入=底薪+销售额 salesAmount * 提成率 rate
工人:月收入=底薪+加班费 overtimePay
(2)应缴个人所得税的方法是统一的,根据月收入扣除不同比例的所得税:
月收入⩽5000 元,所得税: 0 元。
5000<月收入⩽15000,所得税: 月收入*5%。
15000<月收入,所得税:月收入*20%。
要求编写 5 个类,分别是:员工的抽象类 Employee、管理员类 Admin、销售员
类 Salesman 和工人类 Worker,并编写用于测试的演示类 SalayDemo。
要求这这些类都放在 cn.edu.gpnu.company 包中。
【案例目标】
学会分析“计算员工工资”任务实现的逻辑思路。
能够独立完成“计算员工工资”程序的源代码编写、编译以及运行。
掌握 Java 中的继承的知识点。
掌握抽象类、抽象方法的编写与使用。
【运行结果】
【运行代码】
1.
package cn.edu.gpnu.bank.company;
class Admin extends Employee {
double bonus;
public Admin(String name, String type, String department, double basicSalary, double bonus) {
super(name, type, department, basicSalary);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public double getIncome(){
return (getBasicSalary()+getBonus());
}
}
package cn.edu.gpnu.bank.company;
abstract public class Employee {
String name;
String type;
String department;
double basicSalary;
public Employee(String name, String type, String department, double basicSalary) {
this.name = name;
this.type = type;
this.department = department;
this.basicSalary = basicSalary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getBasicSalary() {
return basicSalary;
}
public void setBasicSalary(double basicSalary) {
this.basicSalary = basicSalary;
}
abstract public double getIncome();
public double getTax(){
double income = getIncome();
if(income<=5000)
{
return 0;
}
if(income>5000 && income<=15000)
{
return income*0.05;
}
else
{
return income * 0.2;
}
}
public String info(){
return"姓名:"+this.name+",类型:"+this.type+",部门:"+this.department
+",月收入:"+getIncome()+"元,所得税:"+getTax()+"元,实发工资:"
+(getIncome()-getTax())+"元";
}
}
package cn.edu.gpnu.bank.company;
class Salesman extends Employee {
double saleAmount;
double rate;
public Salesman(String name, String type, String department, double basicSalary, double saleAmount, double rate) {
super(name, type, department, basicSalary);
this.saleAmount = saleAmount;
this.rate = rate;
}
public double getSaleAmount() {
return saleAmount;
}
public void setSaleAmount(double saleAmount) {
this.saleAmount = saleAmount;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getIncome() {
return (getBasicSalary()+getSaleAmount()*getRate());
}
}
package cn.edu.gpnu.bank.company;
class Worker extends Employee {
double overtimePay;
public Worker(String name, String type, String department, double basicSalary, double overtimePay) {
super(name, type, department, basicSalary);
this.overtimePay = overtimePay;
}
public double getOvertimePay() {
return overtimePay;
}
public void setOvertimePay(double overtimePay) {
this.overtimePay = overtimePay;
}
public double getIncome() {
return (getBasicSalary()+getOvertimePay());
}
}
package cn.edu.gpnu.bank.company;
public class SalayDemo {
public static void main(String[] args) {
Admin admin = new Admin("张三", "管理员",
"研发部", 14000, 5600);
Salesman salesman = new Salesman("李四", "销售员",
"天河销售部", 6000, 98900, 0.03);
Worker worker = new Worker("王五", "工人",
"南沙制造场", 2500, 1350);
System.out.println(admin.info());
System.out.println(salesman.info());
System.out.println(worker.info());
}
}