薪水管理系统2.1(Java接口的使用)

本程序使用Java编写了一个薪水管理系统(2.1版本),对前文的薪水管理系统进行了升级,针对校长和员工类分别增加了一个接口,可以用来比较两个员工的工资收入。本程序使用Java™ SE Runtime Environment (build 12.0.2+10),编译器使用IntelliJ IDEA。编写该程序需要使用的Java相关技术为:

  1. java.time.LocalDate;
  2. Java类的继承;
  3. Java类的重载;
  4. 泛型数组列表;
  5. Java接口
  6. Java子类构造器(super);

这一程序由6个源文件构成。WorkersTest.java为测试程序,测试程序使用泛型数组列表生成了两个校长的信息,两个实习生的信息,并分别针对各类员工的薪水使用接口进行比较。Person.java定义了抽象类person,作为整个程序的基类。Workers.java中用抽象类person派生了普通员工类Workers,head_master.java用Workers类派生了校长类head_master,类Workers和head_masters分别使用了compareToOther.java和h_compareToOther.java中的接口进行薪水对比。其中person.java源码如下:

package java_interface;
import java.util.Scanner;

public abstract class Person {
    private String name;
    private int age;
    public Person()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("please input name for this person:");
        name = in.nextLine();
        System.out.println("please input age for this person:");
        age = in.nextInt();
    }

    public Person(String name,int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }
}

Workers.java源码如下:

package java_interface;
import java.time.LocalDate;
import java.util.*;
public class Workers extends Person implements compareToOther
{
    private static int nextId;

    private int id;
    private double salary;
    private LocalDate hireDay;

    static
    {
        Random generator = new Random();
        nextId = generator.nextInt(10000);
    }

    {
        id = nextId;
        nextId++;
    }

    Workers(String name, int age,double salary, int day, int month, int year) {
        super(name,age);
        this.salary = salary;
        hireDay = LocalDate.of(year,month,day);
    }

    Workers() {
        System.out.println("please input salary for this staff:");
        Scanner in = new Scanner(System.in);
        salary = in.nextDouble();
        System.out.println("please input hire day for this staff:");
        int day = in.nextInt();
        int month = in.nextInt();
        int year = in.nextInt();
        hireDay = LocalDate.of(year,month,day);
    }

    public double getSalary()
    {
        return salary;
    }

    public int getId()
    {
        return id;
    }

    public LocalDate getHireDay()
    {
        return hireDay;
    }

    public int compareTo(Workers other)
    {
        return Double.compare(salary,other.salary);
    }
}


head_master.java源码如下:

package java_interface;
import java.util.*;

public class head_master extends Workers implements h_compareToOther
{
    private double bonus;

    public head_master(String name, int age,double salary, int day, int month, int year) {
        super(name, age,salary, day, month, year);
        bonus = 0;
    }

    public head_master()
    {
        System.out.println("please input bonus for this head master:");
        Scanner in = new Scanner(System.in);
        bonus = in.nextDouble();
    }

    public double getSalary()
    {
        double baseSalary = super.getSalary();
        return baseSalary+bonus;
    }

    public void setBonus(double b)
    {
        bonus = b;
    }
    public int compareTo(head_master other)
    {
        return Double.compare(getSalary(),other.getSalary());
    }
}

compareToother.java源码如下:

package java_interface;

public interface compareToOther {
    public int compareTo(Workers other);
}

h_compareToOther.java源码如下:

package java_interface;

public interface h_compareToOther {
    public int compareTo(head_master other);
}

WorkersTest.java源码如下:

package java_interface;

import java.util.ArrayList;

public class WorkersTest {
    public static void main(String [] args)
    {
        System.out.println("input information for these two head master:");
        ArrayList<head_master> headMasters = new ArrayList<>();
        headMasters.add(new head_master());
        headMasters.add(new head_master());
        System.out.println(headMasters.get(0).compareTo(headMasters.get(1)));
        System.out.println("input information for these two interns:");
        ArrayList<Workers> intern = new ArrayList<>();
        intern.add(new Workers());
        intern.add(new Workers());
        System.out.println(intern.get(0).compareTo(intern.get(1)));
    }
}

运行结果如下:

/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=51782:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/liangqian/IdeaProjects/Java接口/out/production/Java接口 java_interface.WorkersTest
input information for these two head master:
please input name for this person:
Li
please input age for this person:
40
please input salary for this staff:
230000
please input hire day for this staff:
12
3
2019
please input bonus for this head master:
120000
please input name for this person:
Wang
please input age for this person:
39
please input salary for this staff:
200000
please input hire day for this staff:
8
8
2007
please input bonus for this head master:
80000
1
input information for these two interns:
please input name for this person:
Zhang
please input age for this person:
19
please input salary for this staff:
3000
please input hire day for this staff:
6
8
2017
please input name for this person:
Wu
please input age for this person:
21
please input salary for this staff:
3200
please input hire day for this staff:
7
2
2018
-1

Process finished with exit code 0

全文完。

如果您想了解更多C++/Java/机器学习相关的知识,欢迎扫描下方的二维码,关注“梁公子的备忘录”,每天一篇相关的技术分享,期待与您一起学习,共同进步!

在这里插入图片描述

你可能感兴趣的:(Java)