-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee.java
More file actions
86 lines (76 loc) · 2.97 KB
/
Copy pathEmployee.java
File metadata and controls
86 lines (76 loc) · 2.97 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
import java.util.Scanner;
import java.io.IOException;
import java.io.*;
import java.util.*;
public class Employee {
// username and password storage
private String filename;
private Map<String, String> employees = new HashMap<>();
// Constructor
Employee(String filename1)
{
filename=filename1;
}
// Method to load employee data from file
public void load_EmployeeData() {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2) {
String username = parts[0].trim(); // remove extra spaces
String pass = parts[1].trim(); // remove extra spaces
employees.put(username, pass);
}
}
reader.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public void displayEmployees() {
for (Map.Entry<String, String> entry : employees.entrySet()) {
System.out.println("Username: " + entry.getKey() + ", Password: " + entry.getValue());
}
}
// Method to check if an employee is valid (login)
public boolean is_Employee() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
return employees.containsKey(username) && employees.get(username).equals(password);
}
// Method to change password
public void Change_password() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter current password: ");
String password = scanner.nextLine();
// Check if the current username and password match
if (employees.containsKey(username) && employees.get(username).equals(password)) {
System.out.print("Enter new password: "); // Asking for new password
String newPassword = scanner.nextLine();
employees.put(username, newPassword); // Updating password
System.out.println("Password updated successfully for user: " + username);
saveEmployeeData();
} else {
System.out.println("You have entered the wrong password.");
}
}
public void saveEmployeeData() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (Map.Entry<String, String> entry : employees.entrySet()) {
writer.write(entry.getKey() + "," + entry.getValue());
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}