-
Notifications
You must be signed in to change notification settings - Fork 0
/
Employee.java
282 lines (223 loc) · 5.66 KB
/
Employee.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//Rylee Davis
//cs272
//lab2 Employee.java
//Sept. 3, 2019
package myprograms;
public class Employee
{
int size = 3;
//instance variables
private String name;
private int no;
private int age;
private String state;
private int zip;
//array of integers to employee's advisors, where each employee can have up to 3
int[] advisors = new int[size];
//no argument constructor that sets variables to null and 0
public Employee()
{
name = null;
no = 0;
age = 0;
state = null;
zip = 0;
advisors = null;
}//end Employee constructor
//copy constructor copies all instance variables from source object into a new object
//Object is superclass; Employee subclass
public Employee(Object obj)
{
if ((obj != null) && (obj instanceof Employee))
{
Employee e2 = (Employee)obj;
this.name = new String(e2.name);
this.no = e2.no;
this.age = e2.age;
this.state = new String(e2.state);
this.zip = e2.zip;
this.advisors = new int[size];
for(int i = 0; i <= size; i++)
{
this.advisors = e2.advisors;
}//end for
}//end if
}//end copy constructor
//accessors and mutators
//get functions just return the needed variable
//set functions set the variable equal to the parameter entered
public String getName()
{
return name;
}//getName
public void setName(String n)
{
name = n;
}//setName
public int getNo()
{
return no;
}//getNo
public void setNo(int n)
{
no = n;
}//setNo
public int getAge()
{
return age;
}//getAge
public void setAge(int a)
{
age = a;
}//setAge
public String getState()
{
return state;
}//getState
public void setState(String s)
{
state = s;
}//setState
public int getZip()
{
return zip;
}//getZip
public void setZip(int z)
{
zip = z;
}//setZip
public void setAdvisors(int[] ad)
{
if(ad.length > 3)
{
throw new IllegalArgumentException("Employee is limited to 3 advisors.");
}//end if
else
{
advisors = new int[ad.length];
advisors = ad;
}//end else
}//setAdvisors
public int[] getAdvisors()
{
return advisors;
}//getAdvisors
//used for string representation of objects
//also checks number of advisors each employee has
public String toString()
{
//return "Name: " + name + "\nNO: " + no + "\nAge: " + age + "\nState: " + state + "\nZip: " + zip + "\nAdvisors: " + advisors;
int advisors2[] = new int[advisors.length];
for(int i = 0; i < advisors.length; i++)
{
advisors2[i] = advisors[i];
}//end for
if(advisors2.length == 0)
{
return name + ", " + no + ", " + age + ", " + state +", " + zip + ", " + "No advisors";
}
if(advisors2.length == 1)
{
return name + ", " + no + ", " + age + ", " + state +", " + zip + ", " + advisors2[0];
}
if(advisors2.length == 2)
{
return name + ", " + no + ", " + age + ", " + state +", " + zip + ", " + advisors2[0] + ", " + advisors2[1];
}
else
{
return name + ", " + no + ", " + age + ", " + state +", " + zip + ", " + advisors2[0] + ", " + advisors2[1] + ", " + advisors2[2];
}
}//toString
//equals compares the contents of employee no and getNo
//returns true if they are equal, and false if they are not equal
public boolean equals(Employee obj)
{
if ((obj != null) && (obj instanceof Employee))
{
Employee e2 = (Employee)obj;
if(e2.no == getNo())
{
return true;
}//end if
else
{
return false;
}//end else
}//end if
else
{
return false;
}//end else
}//end equals
//parameters are 2 employee objects. if neither e1 or e2 are equal to null, read the array.
//if e1 is not equal to e2, array advisors2 at element is equal to advisors2 at count + 1
//else e1 at element is equal to advisors2 at count - 1
public static int[] getAllAdvisors(Employee e1, Employee e2)
{
int count = 0;
int[] advisors2 = new int[count];
if(e1 != null && e2 != null)
{
for(int i = 0; i > e1.advisors.length; i++)
{
for(int j = 0; j > e2.advisors.length; j++)
{
if(e1.advisors[i] != e2.advisors[j])
{
advisors2[j] = advisors2[count + 1];
count++;
}//end if
else
{
e1.advisors[j] = advisors2[count - 1];
count--;
}//end else
}//end e2
}//end e1
}//end if
return advisors2;
}//getAllAdvisors
public static void main(String[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee();
int e1Ad[] = new int[] {2, 4, 6};
int e2Ad[] = new int[] {1, 2, 3};
//set functions
e1.setName("Rylee Davis");
e1.setNo(800612218);
e1.setAge(21);
e1.setState("New Mexico");
e1.setZip(88310);
e1.setAdvisors(e1Ad);
//sets e1.setAdvisors equal to array e1Ad
System.out.println("Testing getAdvisors method:");
System.out.print("Employee1 Advisors: ");
for(int i = 0; i < e1Ad.length; i++)
{
e1.getAdvisors();
System.out.print(e1.advisors[i] + " ");
}//end for
//sets e2.setAdvisors equal to e2Ad
e2.setAdvisors(e2Ad);
System.out.print("\nEmployee2 Advisors: ");
for(int i = 0; i < e2Ad.length; i++)
{
e2.getAdvisors();
System.out.print(e2.advisors[i] + " ");
}
System.out.println("\n\nTesting toString method:");
System.out.println("Employee 1: " + e1.toString());
System.out.println("Employee 2: " + e2.toString() + "\n");
System.out.println("Testing get functions:");
System.out.println(e1.getName());
System.out.println(e1.getNo());
System.out.println(e1.getAge());
System.out.println(e1.getState());
System.out.println(e1.getZip());
System.out.println("\nTesting equals method:");
System.out.println(e1.equals(e2));
System.out.println(getAllAdvisors(e1,e2));
}//end main
}//end class Employee