/**
- 4、给一个字符串
- name=小红&age=17&salary=8000.00&hireDate=1991-07-03;
- name=小明&age=23&salary=10000.00&hireDate=1992-12-11;
- name=小白&age=18&salary=12000.00&hireDate=1996-01-09,
请把字符串中的数据封装到 Employee 的对象中,
类 Employee 有 name(String 类型)、age(int 类型)、salary(薪水是 BigDecimal 类型)、hireDate(入职日期是 Date 类型)
四个字段且符合 JavaBean 规范
把三个创建的对象存放数组中,求出在 1996 年前入职的员工的的平均薪资。
*/
class Employee {
private String name;
private int age;
private BigDecimal salary;//薪水
private Date hireDate;//入职日期
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
@Override
public String toString() {
return this.name + " [name=" + name + ", age=" + age + ", salary=" + salary + ", hireDate="
+ hireDate.toLocaleString() + "]";
}
}
public class Test4 {
//把数据封装到对象的方法
public static void postting(String[] strArr1, Employee temp) throws ParseException {
//解析日期格式
SimpleDateFormat sld = new SimpleDateFormat();
String sDate = "yyyy-MM-dd";
sld.applyPattern(sDate);
//封装到Employee对象
for (int i = 0; i < strArr1.length; i++) {
//装入name
if (i % 4 == 0) {
//获取元素 = 索引位置的后一个索引
int t = strArr1[i].indexOf("=") + 1;
//切割出需要的信息
temp.setName(strArr1[i].substring(t));
}
//装入年龄
if (i % 4 == 1) {
int t = strArr1[i].indexOf("=") + 1;
temp.setAge(Integer.parseInt(strArr1[i].substring(t)));
}
//装入薪水
if (i % 4 == 2) {
int t = strArr1[i].indexOf("=") + 1;
temp.setSalary(new BigDecimal(strArr1[i].substring(t)));
}
//转入日期
if (i % 4 == 3) {
int t = strArr1[i].indexOf("=") + 1;
temp.setHireDate(sld.parse(strArr1[i].substring(t)));
}
}
}
//主方法
public static void main(String[] args) throws ParseException {
String str = "name=小红&age=17&salary=8000.00&hireDate=1991-07-03;"
+ "name=小明&age=23&salary=10000.00&hireDate=1992-12-11;"
+ "name=小白&age=18&salary=12000.00&hireDate=1996-01-09";
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
//按 ; 切割
String[] strArr = str.split(";");
String[] strArr1 = null;
for (String string : strArr) {
//按 & 切割
strArr1 = string.split("&");
//判断各对象是否有元素
if (e1.getName() != null && e1.getAge() != 0 && e1.getSalary() != null && e1.getHireDate() != null) {
if (e2.getName() != null && e2.getAge() != 0 && e2.getSalary() != null && e2.getHireDate() != null) {
//调用把数据存入对象的方法
postting(strArr1, e3);
} else {
postting(strArr1, e2);
}
} else {
postting(strArr1, e1);
}
}
System.out.println(e1.toString());
System.out.println(e2.toString());
System.out.println(e3.toString());
//总薪水
BigDecimal totalSalary = new BigDecimal("0.00");
//总人数
BigDecimal totalP = new BigDecimal("0");
//创建时间1996
String sDate = "1996-01-01";
String sformat = "yyyy-MM-dd";
SimpleDateFormat sld = new SimpleDateFormat(sformat);
//解析时间
Date date = sld.parse(sDate);
//System.out.println(date.toLocaleString());
//判断是否为1996年前
if (e1.getHireDate().before(date)) {
totalP = totalP.add(new BigDecimal("1"));
totalSalary = e1.getSalary().add(totalSalary);
}
if (e2.getHireDate().before(date)) {
totalP = totalP.add(new BigDecimal("1"));
totalSalary = e2.getSalary().add(totalSalary);
}
if (e3.getHireDate().before(date)) {
totalP = totalP.add(new BigDecimal("1"));
totalSalary = e3.getSalary().add(totalSalary);
}
System.out.println("1996年前总薪水 : " + totalSalary);
System.out.println("人数 : " + totalP);
//求平均薪水
System.out.println("平均薪水 : " + totalSalary.divide(totalP));
}
}