-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tester.java
174 lines (148 loc) · 4.51 KB
/
Tester.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
public class Tester {
public static void main(String[] args){
int N = 10;
OrgHierarchyInterface org = new OrgHierarchy();
System.out.println("\n--Initialized an Empty Tree--");
//isEmpty()
System.out.println("\nIs Tree Empty- " + org.isEmpty());
//hireowner
try{
org.hireOwner(1);
System.out.println("Hired Owner successfully with id = 1" );
}
catch(NotEmptyException e)
{
System.out.println(e);
}
//isEmpty()
System.out.println("\nIs Tree Empty- "+ org.isEmpty()+"\n");
// catch notempty exception
System.out.println("\n--Trying to hire owner with id = 2--");
try{
org.hireOwner(2);
}
catch(NotEmptyException e)
{
System.out.println(e);
}
/*
Tree-
1
/ | \
3 2 12
/ \ | |
8 4 7 10
/ \
16 5
*/
//insert employees
try{
org.hireEmployee(3,1);
System.out.println("\nHired employee successfully with id = 3" );
org.hireEmployee(2,1);
System.out.println("Hired employee successfully with id = 2" );
org.hireEmployee(12,1);
System.out.println("Hired employee successfully with id = 12" );
org.hireEmployee(8,3);
System.out.println("Hired employee successfully with id = 8" );
org.hireEmployee(4,3);
System.out.println("Hired employee successfully with id = 4" );
org.hireEmployee(7,2);
System.out.println("Hired employee successfully with id = 7" );
org.hireEmployee(10,12);
System.out.println("Hired employee successfully with id = 10" );
org.hireEmployee(16,8);
System.out.println("Hired employee successfully with id = 16" );
org.hireEmployee(5,8);
System.out.println("Hired employee successfully with id = 5\n" );
}
catch(IllegalIDException e){
System.out.println(e);
}
//print tree
int size1 = org.size();
System.out.println("\nNew Organization Size: " + size1);
try{
System.out.println("\nOrganization = "+org.toString(1)); // "1,2 3 12,4 7 8 10,5 16"
}
catch (IllegalIDException e) {
System.out.println(e);
}
//check size
if(size1 != N){
System.out.println("FAIL");
}
else{
System.out.println("PASS");
}
//check level
try{
System.out.println("\nWhat is the level of employee 5?" );
int l = org.level(5);
System.out.println("Level = "+l);
if(l != 4) {
System.out.println("FAIL");
}else{
System.out.println("PASS");
}
}
catch (IllegalIDException e){
System.out.println(e);
}
//check fireEmployee
try{
System.out.println("\n--Trying to fire employee with id = 7--");
org.fireEmployee(7);
size1 = org.size();
System.out.println("--Employee fired successfully--");
System.out.println("Size: "+size1);
System.out.println("Updated Organization = "+ org.toString(1));
if(size1 != N-1){
System.out.println("FAIL");
}
else{
System.out.println("PASS");
}
}
catch (IllegalIDException e) {
System.out.println(e);
}
//check fireEmployee(id, Manageid)
try{
System.out.println("\n-- fire employee with id = 8 and make id = 4 new boss of its children--");
org.fireEmployee(8,4);
size1 = org.size();
System.out.println("--Employee fired successfully--");
System.out.println("Size: "+ size1);
System.out.println("New Tree- "+ org.toString(1));
System.out.println("Subtree rooted at 3- "+ org.toString(3));
//check if boss is changed after deletion
int boss1 = org.boss(5);
if(boss1 != 4){
System.out.println("FAIL");
}
else{
System.out.println("--Employee 3 is made new boss successfully--");
System.out.println("PASS");
}
}
catch (IllegalIDException e) {
System.out.println("Employee does not exist");
}
//check lowest common boss-
try{
System.out.println("\n--Find lowest common boss of employees 3 and 10--");
int lcb = org.lowestCommonBoss(3,10);
System.out.println("Lowest common boss = "+lcb);
if(lcb != 1){
System.out.println("FAIL");
}
else{
System.out.println("PASS");
}
}
catch (IllegalIDException e) {
System.out.println("Employee does not exist");
}
}
}