-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveEmployeeEvent.java
More file actions
61 lines (54 loc) · 1.88 KB
/
Copy pathRemoveEmployeeEvent.java
File metadata and controls
61 lines (54 loc) · 1.88 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class removeEmployeeEvent {
public static void removeEmployeeEvent() {
//get user input
Scanner scn = new Scanner(System.in);
System.out.println("Enter Employee ID to remove: ");
String idToRemove = scn.nextLine();
//scn.nextLine();
//List<String> lines = new ArrayList<>();
//access employeedatabase.txt
try {
BufferedReader reader = new BufferedReader(new FileReader("employees.txt"));
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
return;
}
boolean found = false;
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
//if (line.startsWith("ID:" + idToRemove)) {
if (line.startsWith(idToRemove)) {
lines.remove(i);
found = true;
break;
}
}
if (!found) {
System.out.println("Employee with ID " + idToRemove + " not found.");
return;
}
try {
FileWriter writer = new FileWriter("employees.txt");
for (String line : lines) {
writer.write(line + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println("Error writing file: " + e.getMessage());
return;
}
System.out.println("Employee with ID " + idToRemove + " was removed.");
}
}