Java object-oriented programming practice - employee salary payment program

This article will introduce how to use Java object-oriented programming to implement an employee salary payment program that can calculate the wages of managers, salespeople, and workers.

  1. Define Employee class

First, we need to define an Employee class, which contains a getSalary() method to calculate the employee's salary. Since different types of employees calculate wages differently, the getSalary() method is an abstract method and needs to be rewritten in subclasses. Following is the code of Employee class:

public abstract class Employee {
    public abstract int getSalary();
}
  1. Define Manager class

Next, we need to define a Manager class, which inherits from the Employee class and represents the manager. This class contains a fixed salary attribute, which represents the monthly salary. Since the manager's salary is fixed, the value of the salary attribute can be returned directly. The following is the code of the Manager class:

public class Manager extends Employee {
    private int salary;

    public Manager(int salary) {
        this.salary = salary;
    }

    @Override
    public int getSalary() {
        return salary;
    }
}
  1. Define the Salesman class

Next, we need to define a Salesman class, which inherits from the Employee class and represents a salesperson. This class contains a basic salary salary and a commission attribute, which represents the monthly basic salary and sales commission. Since the salesperson's salary is the basic salary plus sales commission, the salary can be calculated and returned in the getSalary() method. Below is the code for the Salesman class:

public class Salesman extends Employee {
    private int salary;
    private int commission;

    public Salesman(int salary, int commission) {
        this.salary = salary;
        this.commission = commission;
    }

    @Override
    public int getSalary() {
        return salary + commission;
    }
}
  1. Define Worker class

Next, we need to define a Worker class, which inherits from the Employee class and represents a general worker. This class contains a workDays attribute and a dailySalary attribute, which represent the number of working days per month and the daily salary respectively. Since a general worker's salary is calculated based on the number of days he works per month, the salary can be calculated and returned in the getSalary() method. The following is the code of the Worker class:

public class Worker extends Employee {
    private int workDays;
    private int dailySalary;

    public Worker(int workDays, int dailySalary) {
        this.workDays = workDays;
        this.dailySalary = dailySalary;
    }

    @Override
    public int getSalary() {
        return workDays * dailySalary;
    }
}
  1. Used in main function

In the main function, we can construct Manager, Salesman and Worker objects and call their getSalary() method to calculate wages. The following is the code of the main function:

public static void main(String[] args) {
    Employee manager = new Manager(12000);
    Employee salesman = new Salesman(3000, 5000);
    Employee worker = new Worker(22, 200);

    System.out.println(manager.getSalary());
    System.out.println(salesman.getSalary());
    System.out.println(worker.getSalary());
}

In the above code, we first construct a Manager, a Salesman and a Worker object respectively, and then call their getSalary() methods to calculate wages and output the results.

  1. Summarize

This article describes how to use Java object-oriented programming to implement an employee salary payment program that can calculate the wages of managers, salespeople, and workers. By learning the methods introduced in this article, readers can further master the basic knowledge of Java object-oriented programming and improve their programming abilities.

Guess you like

Origin blog.csdn.net/qq_61433567/article/details/131140016