/
EmployeeTest.java
71 lines (59 loc) · 1.67 KB
/
EmployeeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
/**
* @author youkpter
* 2014年 09月 18日 星期四 19:02:57 CST
*/
public class EmployeeTest
{
public static void main(String[] args)
{
//fill the staff array with three Employee objects
Employee[] staff = new Employee[3];
staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
//raise everyone's salary by 5%
for(Employee e : staff)
e.raiseSalary(5);
//print out information about all Employee objects
for(Employee e : staff)
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() +
",hireDay=" + e.getHireDay());
System.out.println("other name=" + staff[1].getOtherName(staff[2]));
}
}
class Employee
{
private String name;
private double salary;
private Date hireDay;
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
//GregorianCalendar uses 0 for January
hireDay = calendar.getTime();
}
public String getName()
{
return name;
}
public String getOtherName(Employee other)
{
return other.name;
}
public double getSalary()
{
return salary;
}
public Date getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}