-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
293 lines (266 loc) · 11.4 KB
/
Main.java
File metadata and controls
293 lines (266 loc) · 11.4 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
/*
Nama : Erdafa Andikri
NPM : 2306244993
Kode Asdos : RAC
Tanggal : 5 April 2024
Program ini dibuat untuk memenuhi tugas Lab 05 mata kuliah DDP 2.
Program ini merupakan sebuah aplikasi sederhana untuk mengelola karyawan di sebuah perusahaan bernama PacilValley.
Program ini memiliki 5 menu:
1. Daftar Karyawan
2. Hire Karyawan
3. Menaikkan Gaji Karyawan (Permanent and Contract)
4. Memperpanjang Kontrak Karyawan (Contract and Intern)
5. Keluar
Menu 1 merupakan menu untuk menampilkan daftar karyawan yang terdaftar di perusahaan
Menu 2 merupakan menu untuk menambahkan karyawan baru ke dalam perusahaan
Menu 3 dan Menu 4 merupakan menu untuk mengelola karyawan yang telah dihire sebelumnya
Menu 5 merupakan menu untuk keluar dari program
Program ini memiliki error handling untuk memastikan input yang diberikan valid.
*/
import java.util.ArrayList;
import java.util.Scanner;
// Main class
public class Main {
static ArrayList<Employee> employeeList = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
// Main method
public static void main(String[] args) {
System.out.println("Selamat Datang di PacilRekrutment");
while (true) {
printWelcomingMsg();
System.out.print("Input: ");
int actionCode = sc.nextInt();
switch (actionCode) {
case 1:
printEmployeeList();
break;
case 2:
hireEmployee();
break;
case 3:
askForRaise();
break;
case 4:
extendContract();
break;
case 5:
System.out.println("Terima kasih telah menggunakan layanan PacilRekrutment ~ !");
sc.close();
return;
default:
unknownActionMsg();
break;
}
}
}
// Menu 1
public static void printEmployeeList() {
if (employeeList.size() == 0)
System.out.println("Tidak ada karyawan yang terdaftar");
else {
if (getPermanentEmployee().size() != 0)
displayPermanentEmployee();
if (getContractEmployee().size() != 0)
displayContractEmployee();
if (getInternEmployee().size() != 0)
displayInternEmployee();
}
}
// Menu 2
public static void hireEmployee() {
System.out.print("Nama: ");
String name = sc.next();
if (getEmployeeByNameOrId(name) != null) {
System.out.println("Nama sudah terdaftar!!!\n");
return;
}
System.out.print("Base Salary: ");
double salary = sc.nextDouble();
System.out.print("Status Employee (Permanent/Contract/Intern): ");
String status = sc.next();
if (status.equals("Permanent")) {
PermanentEmployee employee = new PermanentEmployee(name, salary);
employeeList.add(employee);
System.out.print("PermanentEmployee dengan ID " + employee.employeeId + " bernama " + name + " berhasil ditambahkan!\n\n");
} else if (status.equals("Contract")) {
System.out.print("Lama Kontrak (Bulan): ");
int contractDuration = sc.nextInt();
ContractEmployee employee = new ContractEmployee(name, salary, contractDuration);
employeeList.add(employee);
System.out.print("ContractEmployee dengan ID " + employee.employeeId + " bernama " + name + " berhasil ditambahkan!\n\n");
} else if (status.equals("Intern")) {
System.out.print("Lama Kontrak (Bulan): ");
int contractDuration = sc.nextInt();
InternEmployee employee = new InternEmployee(name, salary, contractDuration);
employeeList.add(employee);
System.out.print("InternEmployee dengan ID " + employee.employeeId + " bernama " + name + " berhasil ditambahkan!\n\n");
} else {
System.out.println("Status Employee tidak valid!\n\n");
}
}
// Menu 3
public static void askForRaise() {
if (getContractEmployee().size() == 0 && getPermanentEmployee().size() == 0){
System.out.println("Tidak Ada Permanent atau Contract Employee yang Terdaftar!!!\n");
return;
}
if (getPermanentEmployee().size() != 0)
displayPermanentEmployee();
if (getContractEmployee().size() != 0)
displayContractEmployee();
System.out.print("Masukan Nama/ID Employee: ");
String nameOrId = sc.next();
Employee employee = getEmployeeByNameOrId(nameOrId);
if (employee == null) {
System.out.println("Employee dengan Nama/ID " + nameOrId + " Tidak Ditemukan!!!\n");
return;
} else if (employee instanceof InternEmployee) {
System.out.println("Intern Employee Tidak Bisa Mendapatkan Raise!!!\n");
return;
}
System.out.print("Masukan Jumlah Kenaikan Gaji: ");
double raise = sc.nextDouble();
if (raise < 0) {
System.out.println("Kenaikan Gaji Tidak Boleh Negatif!!!\n");
return;
}
if (employee instanceof PermanentEmployee) {
PermanentEmployee permanentEmployee = (PermanentEmployee) employee;
permanentEmployee.askRaise(raise);
} else if (employee instanceof ContractEmployee) {
ContractEmployee contractEmployee = (ContractEmployee) employee;
contractEmployee.askRaise(raise);
}
System.out.println("Employee dengan Nama/ID " + nameOrId + " Berhasil Dinaikkan Gajinya Sebesar " + String.format("%.0f", raise) + "\n\n");
}
// Menu 4
public static void extendContract() {
if (getContractEmployee().size() == 0 && getInternEmployee().size() == 0){
System.out.println("Tidak Ada Contract atau Intern Employee yang Terdaftar!!!\n");
return;
}
if (getContractEmployee().size() != 0)
displayContractEmployee();
if (getInternEmployee().size() != 0)
displayInternEmployee();
System.out.print("Masukan Nama/ID Employee: ");
String nameOrId = sc.next();
Employee employee = getEmployeeByNameOrId(nameOrId);
if (employee == null) {
System.out.println("Employee dengan Nama/ID " + nameOrId + " Tidak Ditemukan!!!\n");
return;
} else if (employee instanceof PermanentEmployee) {
System.out.println("PermanentEmployee Tidak Bisa Extend Kontrak!!!\n");
return;
}
System.out.print("Masukan Jumlah Perpanjangan Kontrak: ");
int duration = sc.nextInt();
if (duration < 0) {
System.out.println("Perpanjangan Kontrak Tidak Boleh Negatif!!!\n");
return;
}
if (employee instanceof InternEmployee) {
InternEmployee internEmployee = (InternEmployee) employee;
internEmployee.extendContract(duration);
} else if (employee instanceof ContractEmployee) {
ContractEmployee contractEmployee = (ContractEmployee) employee;
contractEmployee.extendContract(duration);
}
System.out.println("Employee dengan Nama/ID " + nameOrId + " Berhasil Diperpanjang Kontraknya Selama " + duration + " Bulan\n");
}
// Kumpulan Helper Method
public static Employee getEmployeeByNameOrId(String nameOrId) {
// Return employee if exists, otherwise null
for (Employee employee : employeeList) {
if (employee.name.equals(nameOrId) || Integer.toString(employee.employeeId).equals(nameOrId)) {
return employee;
}
}
return null;
}
public static void displayPermanentEmployee() {
if (PermanentEmployee.employeeCnt == 0) {
return;
}
System.out.println("===== Pegawai Tetap =====");
ArrayList<PermanentEmployee> permanentEmployees = getPermanentEmployee();
for (PermanentEmployee employee : permanentEmployees) {
System.out.println(employee);
}
System.out.println();
}
public static void displayContractEmployee() {
if (ContractEmployee.employeeCnt == 0) {
return;
}
System.out.println("===== Pegawai Kontrak =====");
ArrayList<ContractEmployee> contractEmployees = getContractEmployee();
for (ContractEmployee employee : contractEmployees) {
System.out.println(employee);
}
System.out.println();
}
public static void displayInternEmployee() {
if (InternEmployee.employeeCnt == 0) {
return;
}
System.out.println("===== Pegawai Intern =====");
ArrayList<InternEmployee> internEmployees = getInternEmployee();
for (InternEmployee employee : internEmployees) {
System.out.println(employee);
}
System.out.println();
}
// Penggunaan Generics dapat digunakan (akan dipelajari di week mendatang)
// untuk mengurangi pengulangan 3 method ini
public static ArrayList<InternEmployee> getInternEmployee() {
ArrayList<InternEmployee> internEmployees = new ArrayList<>();
for (Employee employee : employeeList) {
if (employee instanceof InternEmployee) {
internEmployees.add((InternEmployee) employee);
}
}
return internEmployees;
}
public static ArrayList<ContractEmployee> getContractEmployee() {
ArrayList<ContractEmployee> contractEmployees = new ArrayList<>();
for (Employee employee : employeeList) {
if (employee instanceof ContractEmployee) {
contractEmployees.add((ContractEmployee) employee);
}
}
return contractEmployees;
}
public static ArrayList<PermanentEmployee> getPermanentEmployee() {
ArrayList<PermanentEmployee> permanentEmployees = new ArrayList<>();
for (Employee employee : employeeList) {
if (employee instanceof PermanentEmployee) {
permanentEmployees.add((PermanentEmployee) employee);
}
}
return permanentEmployees;
}
// Printing Function
public static void printWelcomingMsg() {
System.out.println("Silakan pilih salah satu opsi berikut:");
System.out.println("[1] Employee List");
System.out.println("[2] Hire Employee");
System.out.println("[3] Raise Salary");
System.out.println("[4] Extend Contract");
System.out.println("[5] Exit");
System.out.println("=".repeat(64));
}
public static void unknownActionMsg() {
System.out.println("Mohon masukkan opsi yang valid!\n");
}
}
// It's The End of The Program
// Thank You RAC!
//
//
//
// Here's a null
// :(
// FYI: This lab was done on a tablet using replit because at the moment i am on the way of reaching my hometown.
// So, i can't use my laptop for this lab. Sorry if it's not as good as the previous labs i've done.
// Have a Nice Day!