-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee.java
More file actions
194 lines (142 loc) · 4.48 KB
/
Copy pathEmployee.java
File metadata and controls
194 lines (142 loc) · 4.48 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//import javax.naming.InvalidNameException //if you found this which I didn't know existed
//What I intended, and if you didn't notice and use the above
import java.util.Scanner;
class InvalidNameException extends Exception {
public InvalidNameException() {
}
public InvalidNameException(String m) {
super(m);
}
}
public abstract class Employee {
private String name;
private double payRate;
public Employee(String name, double payRate) throws InvalidNameException {
if (name.equals("")) // (name.length() == 0), name.isEmpty()
throw new InvalidNameException();
this.name = name;
this.payRate = payRate;
}
public Employee(String name) throws InvalidNameException {
if (name.equals("")) // (name.length() == 0), name.isEmpty()
throw new InvalidNameException();
this.name = name;
this.payRate = 0;
}
public String getName() {return name;}
public void setName(String s) throws InvalidNameException {
if (s.equals("")) // (s.length() == 0), s.isEmpty()
throw new InvalidNameException();
name = s;
}
public double getRate() {return payRate;}
public void setRate(double rate) {payRate = rate;}
public abstract double calculatePay();
public boolean equals(Object o) {
if (o == null) return false;
if(o.getClass() != this.getClass()) {return false;}
//Class objects will be compared to see if this and o are exactly the same class.
Employee temp = (Employee)o;
return (payRate == temp.payRate);
}
}
class HourlyEmployee extends Employee {
private double hours;
public HourlyEmployee(String name, double rate) throws InvalidNameException {
super(name, rate);
if (rate < 15) setRate(15);
//super(name, rate < 15 ? 15 : rate);
//super(name);
//setRate(r); //once you have setRate overridden in HourlyEmployee
}
public HourlyEmployee(String name) throws InvalidNameException {
super(name, 15);
}
public void addHours(double d) {
hours += d;
}
public double getHours() {return hours;}
public double calculatePay() {
double pay = hours * getRate();
hours = 0;
return pay;
}
public void setRate(double r) {
if (r < 15) super.setRate(15);
else super.setRate(r);
}
public boolean equals(Object o) {
if (o == null) return false;
if(o.getClass() != this.getClass()) {return false;}
HourlyEmployee temp = (HourlyEmployee)o;
//These three lines could be ommitted as super.equals will return false if the cast
//would fail. Therefore, the cast can be made inline in the second half of the and.
return super.equals(temp) && this.hours == temp.hours;
}
}
class SalariedEmployee extends Employee {
public SalariedEmployee(String name, double rate) throws InvalidNameException{
super(name, rate < 0 ? 0 : rate);
//or any other way as in Hourly
}
public SalariedEmployee(String name) throws InvalidNameException {
super(name);
}
public double calculatePay(){
return getRate() / 12;
}
}
class EmployeeTest {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
Employee [] ems = new Employee[6];
int sorh;
double rate;
String name;
for (int i = 0; i < ems.length; i++) {
System.out.print("Hourly or Salaried (1 or 2): ");
sorh = kb.nextInt();
kb.nextLine();
try {
switch(sorh) {
case 1:
System.out.print("Name: ");
name = kb.nextLine();
System.out.print("Hourly Wage: ");
rate = kb.nextDouble();
ems[i] = new HourlyEmployee(name, rate);
break;
case 2:
System.out.print("Name: ");
name = kb.nextLine();
System.out.print("Annual Salary: ");
rate = kb.nextDouble();
ems[i] = new SalariedEmployee(name, rate);
break;
default:
System.out.println("Invalid employee type.");
i--;
}
}
catch(InvalidNameException e) {
System.out.println("Employee names cannot be empty. Please re-enter this employee.");
i--;
}
}
for(int i = 0; i < 6; i ++) {
if(ems[i] instanceof HourlyEmployee) {
//instanceof is an operator that returns true if the object referred to by the
//reference on the left "is a" the class on the right -- of that class or
//any descendant of it.
((HourlyEmployee)ems[i]).addHours((int)(Math.random() * 20 +30));
//or
//HourlyEmployee temp = (HourlyEmployee)ems[i];
//temp.addHours((int)(Math.random() * 20 +30)));
}
}
for (int i = 0; i < 6; i ++) {
System.out.println("Name: " + ems[i].getName() + ", pay rate: "
+ ems[i].getRate() + ", Pay: " + ems[i].calculatePay());
}
}
}