薪水管理系统(Java)

本程序使用Java编写了一个薪水管理系统,通过这一程序实现对Java对象与类进行学习,这一系统可以实现新增加一个员工,同时记录这一员工姓名,初始薪水以及入职日期(通过使用java.time.LocalDate)。同时可以通过成员函数对以上信息进行查询。通过成员函数changeSalary可以对薪水进行更改。同时可以对任意员工的信息进行输出。输出内容使用两种输出方式。本程序使用Java™ SE Runtime Environment (build 12.0.2+10),编译器使用IntelliJ IDEA。编写该程序需要使用的Java相关技术为:

  1. Java类与对象;
  2. java.time.LocalDate;
  3. 使用BufferedWriter方法追加写入.txt文件;

这一程序由两个源文件构成。staff.java定义了这一类及其成员函数,EM_System.java对这一系统进行了测试。staff.java源码如下:

import java.io.*;
import java.time.LocalDate;
import java.util.Scanner;


public class staff {
    private String name;
    private double salary;
    private LocalDate hireDay;

    staff(String n, double s, int day, int month, int year) throws FileNotFoundException, UnsupportedEncodingException {
        name = n;
        salary = s;
        hireDay = LocalDate.of(year,month,day);
        PrintWriter outfile = new PrintWriter(name+".txt","UTF-8");
        outfile.println("This is information of "+name+":");
        outfile.printf("\n");
        outfile.close();
    }

    public String getName()
    {
        return name;
    }

    public double getSalary()
    {
        return salary;
    }

    public LocalDate getHireDay()
    {
        return hireDay;
    }

    void changeSalary()
    {
        Scanner in = new Scanner(System.in);
        System.out.println("please input salary amount of increase %");
        System.out.println("this number negative means salary will decrease.");
        double salaryRate = in.nextDouble();
        if(salaryRate <0)
        {
            System.out.println("the salary will decrease %"+Math.abs(salaryRate)+".");
            salary = salary*(1+ salaryRate /100);
        }
        else if(salaryRate == 0)
        {
            System.out.println("the salary will not change.");
        }
        else if(salaryRate > 0)
        {
            System.out.println("the salary will increase %"+ salaryRate +".");
            salary = salary*(1+ salaryRate /100);
        }
        else {
            System.out.println("illegal input!");
        }
    }

    void show() {
        System.out.println("name: "+name);
        System.out.println("salary: "+salary);
        System.out.println("hire day: "+hireDay);
    }

    void fileShow() throws IOException {
        File f = new File(name+".txt");
        if(f.exists())
        {
            System.out.println("file exists.");
        }
        else{
            System.out.println("file not exists.");
            f.createNewFile();
        }
        //PrintWriter outfile = new PrintWriter(name+".txt","UTF-8");
        BufferedWriter outfile = new BufferedWriter(new FileWriter(f,true));
        outfile.write("name: "+name+"\n");
        outfile.write("salary: "+salary+"\n");
        outfile.write("hire day: "+hireDay+"\n");
        outfile.write("\n");
        outfile.close();
    }
}

EM_System.java源码如下:

import java.io.IOException;

public class EM_System {
    public static void main(String [] args) throws IOException {
        staff Linda = new staff("Linda",15000,3,10,2019);
        Linda.show();
        Linda.fileShow();
        Linda.changeSalary();
        Linda.show();
        Linda.fileShow();
    }
}

控制台运行结果如下:

/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49836:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/liangqian/IdeaProjects/薪水管理系统/out/production/薪水管理系统 EM_System
name: Linda
salary: 15000.0
hire day: 2019-10-03
file exists.
please input salary amount of increase %
this number negative means salary will decrease.
10
the salary will increase %10.0.
name: Linda
salary: 16500.0
hire day: 2019-10-03
file exists.

Process finished with exit code 0


输出文件如下:

This is information of Linda:

name: Linda
salary: 15000.0
hire day: 2019-10-03

name: Linda
salary: 16500.0
hire day: 2019-10-03


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

在这里插入图片描述

你可能感兴趣的:(Java)