薪资计算程序设计-Java

时间:2024-02-23 18:48:49

某公司的一次开卷笔试题:

以下是Java试题,需要您注意:

1.一定要考虑到封装性和体现面向对象的设计思想,需要创建类,留意类的设计和代码的可扩展性方面,面试官主要考核的是良好的设计思想和结构以及最终结果;

2.此试题需要在Java开发环境中做,做完后可自行运行下结果是否正确,再做调整;

3. 此试题您做完压缩打包邮件发我,试题最终提交截止时间:2020/07/05  24:00

如有任何问题 ,可随时邮件联系。

 

题目:

请为XX公司设计一套工资计算系统。

 

XX公司有三种类型的雇工, 不同类型的员工有不同的工资计算方式, 具体如下。 另外该公司有一种福利,如果该月员工过生日,则公司会额外奖励100元。

  • 固定工资的员工,每月固定工资为6000元。
  • 小时工,每小时薪资为35元。每月工作160小时,超出部分薪资按照1.3倍发放。
  • 销售人员, 基础工资为每月3000,每月基础销售额应为20000,如果销售额为20000-30000,则超出部分(超出20000部分)提成率为5%,如果销售额为30000及以上,则超出部分(超出20000部分)提成率为8%。

 

请注意:

  1. 员工的固定工资,时薪,提成率和底薪都可能会调整。
  2. 员工类型可能会增加。

 

请设计程序解析以下xml数据,并计算9月和10月份,公司应支付员工工资总额。最终结果应该四舍五入为两位小数。

 

<department>

              <month value="9">

                             <employee name="xiao wang" type="salary" birthday="1990-10-11" /> 

                             <employee name="xiao zhang" type="hour" workingHours="170.5" birthday="1990-11-11"/>           

                             <employee name="xiao hong" type="sale" amount ="34100.8" birthday="1990-12-11"/>

              </month>

              <month value="10">

                             <employee name="xiao wang" type="salary" birthday="1990-10-11" />

                             <employee name="xiao zhang" type="hour" workingHours ="155.75" birthday="1990-11-11"/>

                             <employee name="xiao hong" type="sale" amount ="23500.7" birthday="1990-12-11"/>

                             <employee name="xiao liu" type=" hour " workingHours ="188.25" birthday="1989-10-11"/>

              </month>

</department>

问题分析:

1. 首先所有的工资计算基本上都用策略模式。

2. 员工类型可拓展,必然要创建一个员工的接口。

代码试下如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

测试类:

package com.interview.web;

import com.interview.inteface.EmployeeSalaryStrategy;
import org.jdom.*;
import org.jdom.input.SAXBuilder;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class Test {

public static void main(String[] args) {

//员工信息存储
Map<Integer, List<Employee>> map = new HashMap<Integer, List<Employee>>();

//创建SAXBuilder对象
SAXBuilder saxBuilder = new SAXBuilder();
//创建输入流
InputStream is = null;
try {
is = new FileInputStream(new File("src/main/resources/demo.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//将输入流加载到build中
Document document = null;
try {
document = saxBuilder.build(is);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//获取根节点
Element rootElement = document.getRootElement();
//获取子节点
List<Element> children = rootElement.getChildren();
for (Element child : children) {
List<Attribute> attributes = child.getAttributes();
//打印属性
Integer key = 0;
for (Attribute attr : attributes) {
try {
key = attr.getIntValue();
} catch (DataConversionException e) {
e.printStackTrace();
}
}
List<Employee> employees = new ArrayList<Employee>();
List<Element> childrenList = child.getChildren();
for (Element o : childrenList) {
List<Attribute> oAttributes = o.getAttributes();
Employee employee = new Employee();
for (Attribute attr : oAttributes) {
try {
switch (attr.getName()) {
case "name":
employee.setName(attr.getValue());
break;
case "type":
employee.setType(attr.getValue());
break;
case "birthday":
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
employee.setBirthday(sdf.parse(attr.getValue()));
} catch (ParseException e) {
e.printStackTrace();
}
break;
case "workingHours":
employee.setWorkingHours(attr.getDoubleValue());
break;
case "amount":
employee.setAmount(attr.getDoubleValue());
break;
}
} catch (DataConversionException ex) {
ex.printStackTrace();
}
}
employees.add(employee);
}
map.put(key, employees);
}
try {
if (Objects.nonNull(is)) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}

//总薪资
double sumSalary = 0;
for (Map.Entry<Integer, List<Employee>> entry : map.entrySet()) {
if (9 == entry.getKey()) {
sumSalary = getSumSalary(sumSalary, entry.getValue(), 9);
} else if (10 == entry.getKey()) {
sumSalary = getSumSalary(sumSalary, entry.getValue(), 10);
}
}

System.out.println("公司应支付员工工资总额为:" + new java.text.DecimalFormat("#.00").format(sumSalary));
}

/**
* 计算公司员工总薪资
*
* @param sumSalary
* @param employees
* @param month
* @return
*/
private static double getSumSalary(double sumSalary, List<Employee> employees, int month) {
for (Employee employee : employees) {
switch (employee.getType()) {
case "salary":
EmployeeSalaryStrategy employeeSalaryStrategy = new CommonEmployee();
if (employee.getBirthday().getMonth() == month) {
//普通员工过生日薪资
BirthdaySalary birthdaySalary = new BirthdaySalary(employeeSalaryStrategy);
sumSalary = sumSalary + birthdaySalary.calcBirthdaySalary(new CommonEmployee(2000), 100);
} else {
//普通员工
Salary salary = new Salary(employeeSalaryStrategy);
sumSalary += salary.calcSalay(new CommonEmployee(2000));
}
break;
case "hour":
EmployeeSalaryStrategy hourEmployee = new HourEmployee();
if (employee.getBirthday().getMonth() == 9) {
//小时工生日薪资
BirthdaySalary birthdayHourSalary = new BirthdaySalary(hourEmployee);
sumSalary += birthdayHourSalary.calcBirthdaySalary(new HourEmployee(40, employee.getWorkingHours()), 100);
} else {
//小时工薪资
Salary hourSalary = new Salary(hourEmployee);
sumSalary += hourSalary.calcSalay(new HourEmployee(40, employee.getWorkingHours()));
}
break;
case "sale":
EmployeeSalaryStrategy salesEmployee = new SalesEmployee();
if (employee.getBirthday().getTime() == 9) {
//销售过生日薪资
BirthdaySalary birthdaySalesSalary = new BirthdaySalary(salesEmployee);
sumSalary += birthdaySalesSalary.calcBirthdaySalary(new SalesEmployee(employee.getAmount(), 0.05, 0.08), 100);
;
} else {
//销售薪资
Salary salarySales = new Salary(salesEmployee);
sumSalary += salarySales.calcSalay(new SalesEmployee(employee.getAmount(), 0.05, 0.08));

}
break;
}
}
return sumSalary;
}
}

源码地址:
https://github.com/jamesbaoyi/interview

重点:
本人设计可能思维比较混乱,如有问题请相互沟通。