-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeSet.java
More file actions
342 lines (268 loc) · 8.08 KB
/
Copy pathEmployeeSet.java
File metadata and controls
342 lines (268 loc) · 8.08 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package myprograms;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
//Rylee Davis
//cs272 lab3
//Sept. 9, 2019
public class EmployeeSet
{
//instance variables
private int datacap; //capacity of array data
private int size; //counts number of employees
private Employee[] data; //Employee array
//no-argument constructor that initializes EmployeeSet instance, sets it capacity to 10,
//and creates new array to store 10 Employee objects
public EmployeeSet()
{
size = 0;
datacap = 10;
data = new Employee[datacap];
}//end constructor
/*
* @precondition: the obj should not be null and should be an instance of EmployeeSet
*
* copy constructor that creates new EmployeeSet instance and copies the content of the given object to the new instance
*/
public EmployeeSet(Object obj)
{
if((obj != null) && (obj instanceof EmployeeSet))
{
EmployeeSet es2 = new EmployeeSet();
this.size = es2.size;
this.datacap = es2.datacap;
for(int i = 0; i <= 10; i++)
{
this.data = es2.data;
}//end for
}//end if
}//end copy constructor
//method to get Employee Set
public Employee[] getES()
{
return data;
}//end
//method which returns the actual number of elements in collection
public int size()
{
// for(int i = 0; i < datacap; i++)
// {
// if(data[i] != null)
// {
// size++;
//
// }//end if
//
// }//end for
return size;
}//end size()
//returns length of the whole array
public int capacity()
{
datacap = data.length;
return datacap;
}//end capacity
/*
* @precondition: the employee object a should not be null
*
* method which adds given Employee obj to first available space of the new employee array
*/
public void add(Employee a)
{
if(a == null)
{
throw new IllegalArgumentException("Employee object cannot be null");
}//end if
//if size is less than capacity, array data of size is equal to
if(size < data.length)
{
data[size] = a;
size++;
}//end if
else
{
ensureCapacity(capacity() * 2);
data[size()] = a;
}//end else
}//end add()
//method to check whether this collection contains the input parameter
public boolean contains(Employee a)
{
if(a == null)
{
return false;
}//if
else
{
for(int i = 0; i < size; i++)
{
if(data[i].equals(a))
{
return true;
}//end if
}//end for
}//end else
return false;
}//end contains()
//method to remove from the collection the employee with the given employee number eno
public boolean remove(int eno)
{
if(eno == 0)
{
return false;
}//end if
int i;
for(i = 0; (i < size) && (eno != data[i].getNo()); i++);
if(i == size)
{
return false;
}//end if
else
{
size--;
data[i] = data[size];
return true;
}//end else
}//end remove
/*
* @precondition: the input parameter minimumCapacity should be positive
*
* Guarantees the capacity of the collection. If capacity is smaller than input parameter, method sets the capacity to
* minimumCapacity and enlarges the array
*/
public void ensureCapacity(int minimumCapacity)
{
if(minimumCapacity < 0)
{
throw new IllegalArgumentException("minimumCapacity should be positive");
}//end if
if(datacap <= minimumCapacity)
{
Employee temp[] = new Employee[minimumCapacity];
for(int i = 0; i < size; i++)
{
temp[i] = data[i];
}//end for
data = temp;
datacap = datacap * 2; //doubles array size
}//end if
}//end ensureCapacity
/*
* @precondition: the employee object a should not be null and the objects in the
* collection's employee array are already in ascending order of employee numbers
*/
public void addOrdered(Employee a)
{
if(a == null)
{
throw new IllegalArgumentException("Employee object should not be null");
}//end if
if(size >= capacity())
{
ensureCapacity(capacity() * 2);
}//end
Arrays.sort(data);
System.out.printf(Arrays.toString(data));
}//end addOrdered
private static EmployeeInformation[] numEmployees = null;
public static void read(String fname) //function read
{
int no = 0;
String line = "";
try
{
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fname);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
String input;
int counter = 0;
while((input = bufferedReader.readLine()) != null)
{
counter++;
}//end while
System.out.println("Count: " + counter);
while((line = bufferedReader.readLine()) != null) //will read until end ?
{
//parse line
if(no==0)
{
int totalNumber = Integer.parseInt(line);
numEmployees = new EmployeeInformation[totalNumber];
}//end if
else
{
String[] linestr = line.split(","); //use comma as separator
if(linestr.length!= 6) continue;
if(no > numEmployees.length)
{
System.out.println("There are too many employees in the file. ");
break;
}//end if
numEmployees[no-1] = new EmployeeInformation();
numEmployees[no-1].name = linestr[0];
numEmployees[no-1].number = Integer.parseInt(linestr[1]);
numEmployees[no-1].state = linestr[2];
numEmployees[no-1].zip = Integer.parseInt(linestr[3]);
numEmployees[no-1].age = Integer.parseInt(linestr[4]);
numEmployees[no-1].sex = linestr[5];
}//end else
}//end while
no++;
bufferedReader.close(); // Always close files.
}//end try
catch(FileNotFoundException ex)
{
System.out.println("Unable to open file '" + fname + "'");
}//end catch
catch(IOException ex)
{
System.out.println("Error reading file '" + fname + "'");
}//end catch
}//end read
public static void main(String[] args)
{
//read("/home/ugrad25/rdavis/cs272/myprograms/core_dataset.csv");
//declaring new employee objects
Employee e1 = new Employee();
Employee e2 = new Employee();
//declaring new employeeset object
EmployeeSet es1 = new EmployeeSet();
//set functions
e1.setName("Rylee Davis");
e1.setNo(800612218);
e1.setAge(21);
e1.setState("New Mexico");
e1.setZip(88310);
e2.setName("Employee Name");
e2.setNo(800123456);
e2.setAge(34);
e2.setState("California");
e2.setZip(90210);
//print all 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());
//add employee e1 to employeeset es1
es1.add(e1);
//print capacity and size after adding e1
System.out.println("\nCapacity of es1: " + es1.capacity());
System.out.println("Size of es1: " + es1.size());
//does es1 contain e1 and e2
System.out.println("\nEmployeeSet es1 contains e1: " + es1.contains(e1));
System.out.println("EmployeeSet es1 contains e2: " + es1.contains(e2));
es1.add(e2);
//print capacity and size after adding e2
System.out.println("\nAdd e2 to EmployeeSet: ");
System.out.println("Capacity: " + es1.capacity());
System.out.println("Size: " + es1.size());
//does remove employee with the following number
System.out.println(es1.remove(800612218));
System.out.println("\nCapacity: " + es1.capacity());
System.out.println("Size: " + es1.size());
}//end main
}//end class