某公司的一次開卷筆試題:
以下是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%。
請注意:
- 員工的固定工資,時薪,提成率和底薪都可能會調整。
- 員工類型可能會增加。
請設計程序解析以下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
重點:
本人設計可能思維比較混亂,如有問題請相互溝通。