-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryPoint.java
More file actions
209 lines (184 loc) · 7.53 KB
/
Copy pathEntryPoint.java
File metadata and controls
209 lines (184 loc) · 7.53 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
import java.io.*;
import java.util.Scanner;
public class EntryPoint {
private static final File USER_DATA_FILE = new File("userdata.txt");
private static final String USER_DATA_DIR = "./userdata/";
private static final String EMERGENCY_DATA_DIR = "./emergencydata/";
private static final String SALARY_DATA_DIR = "./salarydata/";
public static void main(String[] args) {
// Create necessary directories and files
createDirectories();
createUserDataFile();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice;
try {
choice = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a number.");
continue;
}
switch (choice) {
case 1:
register(scanner);
break;
case 2:
try {
login(scanner);
} catch (IOException e) {
System.out.println("An error occurred during login: " + e.getMessage());
}
break;
case 3:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void createDirectories() {
File userDataDir = new File(USER_DATA_DIR);
File emergencyDataDir = new File(EMERGENCY_DATA_DIR);
File salaryDataDir = new File(SALARY_DATA_DIR);
if (!userDataDir.exists()) {
userDataDir.mkdirs();
}
if (!emergencyDataDir.exists()) {
emergencyDataDir.mkdirs();
}
if (!salaryDataDir.exists()) {
salaryDataDir.mkdirs();
}
}
private static void createUserDataFile() {
if (!USER_DATA_FILE.exists()) {
try {
USER_DATA_FILE.createNewFile();
} catch (IOException e) {
System.out.println("Error creating userdata.txt: " + e.getMessage());
}
}
}
private static void login(Scanner scanner) throws IOException {
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
if (authenticateUser(username, password)) {
System.out.println("Login successful!");
EmployeeDataView viewerData = new EmployeeDataView();
Scanner sc = new Scanner(System.in);
MainMenu menu = new MainMenu();
Employee_Show viewer = new Employee_Show();
menu.menu();
int choice = 0;
while (choice != 5) {
StaticsArt.SelectList();
System.out.print("\nEnter your choice: ");
try {
choice = Integer.parseInt(sc.nextLine());
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a number.");
continue;
}
switch (choice) {
case 1:
try {
new Employee_Add().createFileBaseHTML();
} catch (IOException e) {
System.out.println("An error occurred while adding an employee: " + e.getMessage());
}
break;
case 2:
viewerData.listUserdataFiles();
System.out.print("\nEnter Employee ID to view: ");
String viewId = sc.nextLine();
try {
viewer.viewFile(viewId);
} catch (Exception e) {
System.out.println("An error occurred while viewing the employee: " + e.getMessage());
}
break;
case 3:
System.out.print("\nEnter Employee ID to remove: ");
viewerData.listUserdataFiles();
String removeId = sc.nextLine();
new Employee_Remove().removeFile(removeId);
break;
case 4:
System.out.print("\nEnter Employee ID to update: ");
viewerData.listUserdataFiles();
String updateId = sc.nextLine();
System.out.print("Enter old data to update: ");
String oldData = sc.nextLine();
System.out.print("Enter new data: ");
String newData = sc.nextLine();
new Employee_Update().updateFile(updateId, oldData, newData);
break;
case 5:
new CodeExit().out();
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
} else {
System.out.println("Invalid username or password!");
}
}
static boolean isUserExists(String username) {
try (BufferedReader reader = new BufferedReader(new FileReader(USER_DATA_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts[0].equals(username)) {
return true;
}
}
} catch (IOException e) {
System.out.println("Error reading user data: " + e.getMessage());
}
return false;
}
static void register(Scanner scanner) {
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
if (isUserExists(username)) {
System.out.println("Username already exists. Please try a different username.");
} else {
saveUser(username, password);
System.out.println("Registration successful!");
}
}
static boolean authenticateUser(String username, String password) {
try (BufferedReader reader = new BufferedReader(new FileReader(USER_DATA_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts[0].equals(username) && parts[1].equals(password)) {
return true;
}
}
} catch (IOException e) {
System.out.println("Error reading user data: " + e.getMessage());
}
return false;
}
static void saveUser(String username, String password) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(USER_DATA_FILE, true))) {
writer.write(username + "," + password);
writer.newLine();
} catch (IOException e) {
System.out.println("Error saving user data: " + e.getMessage());
}
}
}