JAVA after-school exercises (three)-employees' job responsibilities (1)

Hello everyone!
I’m Xiao Huang, I’m glad to meet you again!
The update today is:


Creation time : October 23, 2020
Software version : eclipse 2020-06 (4.16.0)


table of Contents:

1. Title:

1. Requirements for this experiment: Experience the application scenarios of "inheritance" and "attribute reuse technology" based on the employees' daily work.

  • 1-1. Business description:
    1-1.1. This experiment is based on the daily work patterns of company employees.
    1-1.2. Every employee of the company needs to carry out daily routine work every day.
    1-1.3. Personnel personnel are not only company employees, but also have specific job responsibilities.
    1-1.4. In addition to daily routine work, personnel also have the job responsibilities of paying employees' salaries.
    1-2. Create a project and configure the environment:
    1-2.1. Restrictions 1. Create an experimental project, named: SE_JAVA_EXP_E032;
    1-2.2. Restrictions 2. Create a package, named: cn.campsg.java.experiment;
    1-2.3 . Restriction 3. Create a package and name it: cn.campsg.java.experiment.entity.
    1-3. Create an employee entity class and its related attributes:
    1-3.1. Restrictions 1. Create an employee class in the cn.campsg.java.experiment.entity package: Employe.
    1-3.2. Create the following attributes for the Employe class:
    1) Employee name: character type;
    2) Employee rank: integer type;
    3) Employee salary: integer type.
    1-3.3. Create getter/setter methods for the properties of the Employe class.
    1-3.4. Create a 0-parameter constructor for the Employe class.
    1-3.5. Create a 3-parameter constructor for the Employe class.
    1) In the 3-parameter constructor, initialize the three attributes of the employee class.
    1-3.6. Create daily routine work methods for the Employe class:
    1) Restrictions 1. The method name is: work.
    2) Restrictions 2. The work method can be set to 0 parameters, without returning public functions.
    3) The function of the work method is to simulate employees' routine work.
    4) The work method needs to output information on the console: employee name + " Done your job!"
    1-4. The creator
    factual class and related attributes: 1-4.1. Restrictions 1. In cn.campsg.java.experiment Create the personnel class in the .entity package: Hrstaff;
    1) The Hrstaff class needs to inherit the Employe class to obtain all the attributes and characteristics of the employee class.
    1-4.2. Create a 0-parameter constructor for Hrstaff.
    1-4.3. Create a 1-parameter constructor for Hrstaff:
    1) The parameter is: personnel name (string type).
    2) Hrstaff uses the unique attribute sharing feature of inheritance technology to realize initialization.
    3) Hrstaff calls the 3-parameter constructor of the parent class Employe with the following parameters:
    Employee name attribute of Employe class = employee name attribute of Hrstaff class.
    The employee rank attribute of the Employe class = 5 (personnel employee rank defaults to 5).
    The employee salary attribute of the Employe class = 5000 (the default salary of personnel employees is 5000).
    1-5. Realize the operation of HR to pay salaries to employees:
    1-5.1. Create a salary method for the Hrstaff class:
    1) Restriction 1. Method name: paySalary;
    2) Restriction 2. The paySalary method has an Employe type parameter , Used to express which employee pays wages.
    3) Restrictions 3. The paySalary method can be set as a public function without return.
    4) paySalary realizes salary payment according to Employe's employee grade. The rules for payment are as follows:
    rank is between [1-5], employee salary = employee salary + 500;
    rank is between [6-9], employee salary = employee salary + 800;
    rank is between [10-15], employee salary = employee salary + 1000;
    beyond the rank range, prompt: "There is no corresponding rank, additional floating salary cannot be issued"
    1-6. Display the employee's job content and salary:
    1-6.1. Restrictions 1. Create a main class in the cn.campsg.java.experiment package: MainClass;
    1-6.2. Create an entry main method for MainClass: main;
    1-6.3. In the main method, create 3 different employees Object and 1 personal object.
    1-6.4. In the main method, display the regular work status of all employees and personnel (call work).
    1-6.5. In the main method, realize the operation of the personnel to all employees (including themselves) to pay salaries.
    1-6.6. After the salary is paid, output the employee's salary information (including personnel) in the main method:
    1) The output information format is: employee name + "salary:" + employee salary

  • 2. Realization ideas

    2-1. Create a project and configure the environment.
    2-2. Create an employee entity class and its related attributes:
    2-2.1. Create an employee class in the cn.campsg.java.experiment.entity package: Employe;
    2-2.2. Define the following private attribute member variables for Employe
    1) Employee Name: String name;
    2) Employee rank: int level;
    3) Employee salary: int salary.
    2-2.3. Create corresponding getter/setter methods for the properties of the Employe class.
    2-2.4. Create a default constructor for the Employ class.
    2-2.5. Create a 3-parameter constructor for the Employe class. The creation rules are as follows:
    1) The parameters of the constructor correspond to the three attribute variables of Employe;
    2) The assignment of the three attributes of the Employe class is completed in the 3-parameter constructor.
    2-2.6. Create a regular work method for Employe: work, the format is as follows:
    Insert picture description here
    1) Output information to the console in the method body, format: employee name + "I have done my job!";
    2-3. Create human facts And related attributes:
    2-3.1. Create the personnel class: Hrstaff in the cn.campsg.java.experiment.entity package.
    1) The personnel category Hrstaff inherits from the employee category Employe.
    2-3.2. Create a default constructor for the personnel class Hrstaff.
    2-3.3. Create a 1-parameter constructor for the personnel class Hrstaff, the parameter is the name of the personnel:
    1) Call Employe's 3-parameter constructor in the 1-parameter constructor to initialize the attributes of personnel.
    2) The initialization of Hrstaff class attributes can be set according to the following rules:
    Employe's name attribute = Hrstaff's name attribute;
    Employe's level attribute = 5;
    Employe's salary attribute = 5000.
    2-4. Realize the operation of personnel to pay salaries to employees
    2-4.1. Create a unique paySalary method for the Hrstaff class, its form is as follows:
    Insert picture description here
    2-4.2. Realize the business logic of salary payment:
    1) In the method, first get The employee object level passed in through the parameter:
    int level = em.getLevel();
    2) According to the experimental requirements, an additional subsidy amount will be added to the original salary based on the employee’s level.
    3) The prompt message when exceeding the rank is: "There is no corresponding rank, no additional floating salary can be issued".
    2-5. Display the employee's work content and salary:
    2-5.1. Create a business main class in the package cn.campsg.java.experiment: MainClass;
    2-5.2. Define the main function main in MainClass.
    2-5.3. Create 3 different employee objects and 1 personal object in the main method.
    2-5.4. Call the work function of 4 objects to output the work content of all employees (including personnel).
    2-5.5. Simulate the operation of HR to pay employees (including themselves):
    1) Call the paySalary method of the Hrstaff object and pass in three employee objects respectively.
    2) Call the paySalary method of the Hrstaff object and pass in the personnel object itself.
    3) Output the salary information of all employees to the console in turn, the information format is as follows:
    employee object.getName() + "salary:" + employee object.getSalary().

  • 3. Verification and testing

    3-1. Locate the main class MainClass in the project.
    3-2. Right-click the MainClass class and select in turn: Run As->Java Application.
    3-3. Run the program to check whether the output result meets expectations.

  • 4. Complete effect preview:
    Insert picture description here

2. Code:

  • eclipse project directory:
    Insert picture description here
  • 包名 :cn.campsg.java.experiment
  • Class name: MainClass
package cn.campsg.java.experiment;

import cn.campsg.java.experiment.entity.Employe;
import cn.campsg.java.experiment.entity.Hrstaff;

public class MainClass {
    
    

	public static void main(String[] args) {
    
    
		Employe employe1=new Employe("南帝",1,5000);
		Employe employe2=new Employe("北丐",7,6000);
		Employe employe3=new Employe("中神",11,7000);
		Hrstaff hr=new Hrstaff("HR");
		employe1.work();
		employe2.work();
		employe3.work();
		hr.work();
		System.out.println(hr.getName()+"开始发放工资:");
		hr.paySalary(employe1);
		hr.paySalary(employe2);
		hr.paySalary(employe3);
		hr.paySalary(hr);
	}

}

  • 包名 :cn.campsg.java.experiment.entity
  • Class name: Employe
package cn.campsg.java.experiment.entity;

public class Employe {
    
    
	private String name;
	private int level;
	private int salary;

	public Employe() {
    
    
		super();
	}

	public Employe(String name, int level, int salary) {
    
    
		super();
		this.name = name;
		this.level = level;
		this.salary = salary;
	}

	public String getName() {
    
    
		return name;
	}

	public void setName(String name) {
    
    
		this.name = name;
	}

	public int getLevel() {
    
    
		return level;
	}

	public void setLevel(int level) {
    
    
		this.level = level;
	}

	public int getSalary() {
    
    
		return salary;
	}

	public void setSalary(int salary) {
    
    
		this.salary = salary;
	}

	public void work() {
    
    
		System.out.println(name + " 做好了本职工作! ");
	}
}

  • 包名 :cn.campsg.java.experiment.entity
  • Class name: Hrstaff
package cn.campsg.java.experiment.entity;

public class Hrstaff extends Employe {
    
    
	public Hrstaff() {
    
    
		super();
	}

	public Hrstaff(String name) {
    
    
		super(name, 5, 5000);
	}

	public Hrstaff(String name, int level, int salary) {
    
    
		super(name, level, salary);
	}

	public void paySalary(Employe em) {
    
    
		if (em.getLevel() > 0 && em.getLevel() < 6)
			em.setSalary(em.getSalary() + 500);
		else if (em.getLevel() < 10)
			em.setSalary(em.getSalary() + 800);
		else if (em.getLevel() < 16)
			em.setSalary(em.getSalary() + 1000);
		else
			System.out.println("不存在对应的职级,无法发放额外浮动薪水");
		System.out.println("员工" + em.getName() + "的薪水:" + em.getSalary());
	}
}


Friends passing by, if you think you can learn something , please give a thumbs up and let's go. Welcome to the big guys passing by to comment, correct mistakes, and welcome friends who have questions to comment, leave messages, and send private messages.

The attention of every small partner is my motivation to update my blog! ! !
Please search WeChat for [ Zaixiaxiaohuang ] article updates will be read as soon as possible!
Insert picture description here

Grasp the present, look to the future, come on!


Due to the limited level, there will inevitably be some shortcomings in the writing. I urge everyone to take your advice!

Guess you like

Origin blog.csdn.net/weixin_44519789/article/details/109250592