Java项目实践: 员工管理系统的继承模式

**Employee类**
public class Employee {
private int employeeID;
private String name;
private String position;
private int leaveDays;
private double basicSalary;
public Employee(int employeeID, String name, String position, int leaveDays, double basicSalary) {
this.employeeID = employeeID;
this.name = name;
this.position = position;
this.leaveDays = leaveDays;
this.basicSalary = basicSalary;
}
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public int getLeaveDays() {
return leaveDays;
}
public void setLeaveDays(int leaveDays) {
this.leaveDays = leaveDays;
}
public double getBasicSalary() {
return basicSalary;
}
public void setBasicSalary(double basicSalary) {
this.basicSalary = basicSalary;
}
public double calculateSalary(){
return 0;
}
}
**Manager类**
public class Manager extends Employee{
public Manager(int employeeID, String name, String position, int leaveDays, double basicSalary){
super(employeeID, name, position, leaveDays, basicSalary);
}
public double calculateSalary(){
double salary = 0.0;
salary=getBasicSalary()+getBasicSalary()*0.5+200-this.getLeaveDays()*(getBasicSalary()/30);
return salary;
}
}
**Director类**
public class Director extends Employee{
public Director(int employeeID, String name, String position, int leaveDays, double basicSalary){
super(employeeID, name, position, leaveDays, basicSalary);
}
public double calculateSalary(){
double salary =0.0;
salary= getBasicSalary()+getBasicSalary()*0.08+2000-this.getLeaveDays()*(getBasicSalary()/30);
return salary;
}
}
**Staff类**
public class Staff extends Employee{
public Staff(int employeeID, String name, String position, int leaveDays, double basicSalary){
super(employeeID, name, position, leaveDays, basicSalary);
}
public double calculateSalary(){
double salary = 0.0;
salary = getBasicSalary()+getBasicSalary()*0.1+1000-this.getLeaveDays()*(getBasicSalary()/30);
return salary;
}
}
**ServerManage类**
public class ServerManage {
private Employee e1;
public static int employeeCount = 0;
public ServerManage(){
employeeCount++;
}
public void addEmployee(int employeeID){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入员工姓名");
String name = scanner.next();
System.out.println("请输入员工职务");
String position = scanner.next();
System.out.println("请输入请假天数");
int leaveDays = scanner.nextInt();
System.out.println("请输入基本工资");
double basicSalary=scanner.nextDouble();
scanner.close();
if (position.equals("经理")){
e1 = new Manager(employeeCount, name, position, leaveDays, basicSalary);
} else if (position.equals("董事")) {
e1 = new Director(employeeCount,name , position, leaveDays, basicSalary);
}else{
e1 =new Staff(employeeCount, name, position, leaveDays, basicSalary);
}
System.out.println("信息已录入");
return;
}
public void display(){
System.out.println("========员工信息如下:===========");
System.out.println("员工ID: "+e1.getEmployeeID());
System.out.println("员工姓名: "+e1.getName());
System.out.println("员工职务: "+e1.getPosition());
System.out.println("请假天数: "+e1.getLeaveDays());
System.out.println("基本工资: "+e1.getBasicSalary());
System.out.println("总计薪资: "+e1.calculateSalary());
System.out.println("==============================");
}
}
**Main类**
public class Main {
public static void main(String[] args) {
ServerManage serverManage = new ServerManage();
serverManage.addEmployee(01);
serverManage.display();
}
}
**运行效果:** ![](https://img2024.cnblogs.com/blog/3548383/202502/3548383-20250228103445806-1988639898.png)
posted @   沉浮吖  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律

阅读目录(Content)

此页目录为空

>
点击右上角即可分享
微信分享提示