编写一个Java应用程序,设计工资类,包括但不限职工工号、职工姓名、岗位工资、薪级工资、职务津贴、绩效工资、应发工资、个人所得税和实发工资。通过键盘操作读取指定工资文本信息,对工资类对象赋值并在控制台输出打印结果。
时间: 2023-10-20 13:38:56 浏览: 105
好的,我会帮您解答这个问题。
首先,我们需要创建一个工资类,包括以下属性:
```
public class Salary {
private String employeeId; // 职工工号
private String employeeName; // 职工姓名
private double jobSalary; // 岗位工资
private double gradeSalary; // 薪级工资
private double allowance; // 职务津贴
private double performance; // 绩效工资
private double grossSalary; // 应发工资
private double personalTax; // 个人所得税
private double netSalary; // 实发工资
// 构造方法
public Salary(String employeeId, String employeeName, double jobSalary, double gradeSalary,
double allowance, double performance) {
this.employeeId = employeeId;
this.employeeName = employeeName;
this.jobSalary = jobSalary;
this.gradeSalary = gradeSalary;
this.allowance = allowance;
this.performance = performance;
}
// 计算应发工资
public void calculateGrossSalary() {
this.grossSalary = jobSalary + gradeSalary + allowance + performance;
}
// 计算个人所得税
public void calculatePersonalTax() {
if (grossSalary <= 3500) {
personalTax = 0;
} else {
double taxableIncome = grossSalary - 3500;
if (taxableIncome <= 1500) {
personalTax = taxableIncome * 0.03;
} else if (taxableIncome <= 4500) {
personalTax = taxableIncome * 0.1 - 105;
} else if (taxableIncome <= 9000) {
personalTax = taxableIncome * 0.2 - 555;
} else if (taxableIncome <= 35000) {
personalTax = taxableIncome * 0.25 - 1005;
} else if (taxableIncome <= 55000) {
personalTax = taxableIncome * 0.3 - 2755;
} else if (taxableIncome <= 80000) {
personalTax = taxableIncome * 0.35 - 5505;
} else {
personalTax = taxableIncome * 0.45 - 13505;
}
}
}
// 计算实发工资
public void calculateNetSalary() {
netSalary = grossSalary - personalTax;
}
// 打印工资信息
public void printSalary() {
System.out.println("职工工号:" + employeeId);
System.out.println("职工姓名:" + employeeName);
System.out.println("岗位工资:" + jobSalary);
System.out.println("薪级工资:" + gradeSalary);
System.out.println("职务津贴:" + allowance);
System.out.println("绩效工资:" + performance);
System.out.println("应发工资:" + grossSalary);
System.out.println("个人所得税:" + personalTax);
System.out.println("实发工资:" + netSalary);
}
}
```
然后,我们需要编写一个程序,通过键盘读取指定工资文本信息,创建工资类对象并赋值,计算应发工资、个人所得税和实发工资,并输出打印结果。具体代码如下:
```
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class SalaryApplication {
public static void main(String[] args) {
try {
// 读取指定工资文本信息
Scanner scanner = new Scanner(new File("salary.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] fields = line.split(",");
// 创建工资类对象并赋值
Salary salary = new Salary(fields[0], fields[1], Double.parseDouble(fields[2]),
Double.parseDouble(fields[3]), Double.parseDouble(fields[4]),
Double.parseDouble(fields[5]));
// 计算应发工资、个人所得税和实发工资
salary.calculateGrossSalary();
salary.calculatePersonalTax();
salary.calculateNetSalary();
// 输出打印结果
salary.printSalary();
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("指定工资文本文件不存在!");
}
}
}
```
在上述代码中,我们假设工资文本文件名为"salary.txt",每行包括职工工号、职工姓名、岗位工资、薪级工资、职务津贴和绩效工资,以逗号分隔。程序通过Scanner类逐行读取工资文本信息,创建工资类对象并赋值,计算应发工资、个人所得税和实发工资,最后输出打印结果。
希望这个示例代码能够帮到您。
阅读全文