Skip to content

Commit 2904e3c

Browse files
authored
Merge pull request #7 from kavindus0/regAndLog
Reg and log
2 parents 0cbec2f + 10a3d79 commit 2904e3c

5 files changed

Lines changed: 180 additions & 60 deletions

File tree

src/EmployDetail.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import java.util.Scanner;
22
class EmployDetail {
33
String name, father_name, email, position, employ_id;
4-
private double employ_salary;
4+
double employ_salary;
55
int employ_contact;
66

77
public void getInfo() {

src/EmployManagementSystem.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/EntryPoint.java

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import java.io.*;
2+
import java.util.Scanner;
3+
4+
public class EntryPoint {
5+
private static final String FILE_NAME = "user_data.txt";
6+
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in);
9+
while (true) {
10+
System.out.println("1. Register");
11+
System.out.println("2. Login");
12+
System.out.println("3. Exit");
13+
System.out.print("Enter your choice: ");
14+
15+
int choice = 0;
16+
try {
17+
choice = scanner.nextInt();
18+
scanner.nextLine();
19+
} catch (Exception e) {
20+
System.out.println("Invalid choice");
21+
choice = 3;
22+
}
23+
switch (choice) {
24+
case 1:
25+
register(scanner);
26+
break;
27+
case 2:
28+
login(scanner);
29+
break;
30+
case 3:
31+
System.out.println("Exiting...");
32+
scanner.close();
33+
System.exit(0);
34+
break;
35+
default:
36+
System.out.println("Invalid choice. Please try again.");
37+
}
38+
}
39+
}
40+
41+
private static void register(Scanner scanner) {
42+
System.out.print("Enter username: ");
43+
String username = scanner.nextLine();
44+
System.out.print("Enter password: ");
45+
String password = scanner.nextLine();
46+
47+
if (isUserExists(username)) {
48+
System.out.println("Username already exists. Please try a different username.");
49+
} else {
50+
saveUser(username, password);
51+
System.out.println("Registration successful!");
52+
}
53+
}
54+
55+
private static void login(Scanner scanner) {
56+
System.out.print("Enter username: ");
57+
String username = scanner.nextLine();
58+
System.out.print("Enter password: ");
59+
String password = scanner.nextLine();
60+
61+
if (authenticateUser(username, password)) {
62+
System.out.println("Login successful!");
63+
64+
EmployeeDataView viewerData = new EmployeeDataView();
65+
Scanner sc = new Scanner(System.in);
66+
MainMenu menu = new MainMenu();
67+
Employee_Show viewer = new Employee_Show();
68+
menu.menu();
69+
int choice = 0;
70+
while (choice != 5) {
71+
StaticsArt.SelectList();
72+
System.out.print("\nEnter your choice: ");
73+
choice = Integer.parseInt(sc.nextLine());
74+
75+
switch (choice) {
76+
case 1:
77+
new Employee_Add().createFile();
78+
break;
79+
80+
case 2:
81+
viewerData.listUserdataFiles();
82+
System.out.print("\nEnter Employee ID to view: ");
83+
String viewId = sc.nextLine();
84+
try {
85+
viewer.viewFile(viewId);
86+
} catch (Exception e) {
87+
System.out.println(e.getMessage());
88+
}
89+
break;
90+
91+
case 3:
92+
System.out.print("\nEnter Employee ID to remove: ");
93+
viewerData.listUserdataFiles();
94+
String removeId = sc.nextLine();
95+
new Employee_Remove().removeFile(removeId);
96+
break;
97+
98+
case 4:
99+
System.out.print("\nEnter Employee ID to update: ");
100+
viewerData.listUserdataFiles();
101+
String updateId = sc.nextLine();
102+
System.out.print("Enter old data to update: ");
103+
String oldData = sc.nextLine();
104+
System.out.print("Enter new data: ");
105+
String newData = sc.nextLine();
106+
new Employee_Update().updateFile(updateId, oldData, newData);
107+
break;
108+
109+
case 5:
110+
new CodeExit().out();
111+
break;
112+
113+
default:
114+
System.out.println("Invalid choice. Please try again.");
115+
}
116+
}
117+
118+
} else {
119+
System.out.println("Invalid username or password!");
120+
}
121+
}
122+
123+
private static boolean isUserExists(String username) {
124+
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
125+
String line;
126+
while ((line = reader.readLine()) != null) {
127+
String[] parts = line.split(",");
128+
if (parts[0].equals(username)) {
129+
return true;
130+
}
131+
}
132+
} catch (IOException e) {
133+
System.out.println("Error reading user data: " + e.getMessage());
134+
}
135+
return false;
136+
}
137+
138+
private static boolean authenticateUser(String username, String password) {
139+
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
140+
String line;
141+
while ((line = reader.readLine()) != null) {
142+
String[] parts = line.split(",");
143+
if (parts[0].equals(username) && parts[1].equals(password)) {
144+
return true;
145+
}
146+
}
147+
} catch (IOException e) {
148+
System.out.println("Error reading user data: " + e.getMessage());
149+
}
150+
return false;
151+
}
152+
153+
private static void saveUser(String username, String password) {
154+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME, true))) {
155+
writer.write(username + "," + password);
156+
writer.newLine();
157+
} catch (IOException e) {
158+
System.out.println("Error saving user data: " + e.getMessage());
159+
}
160+
}
161+
}

src/User.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class User {
2+
private String username;
3+
private String password;
4+
5+
public User(String username, String password) {
6+
this.username = username;
7+
this.password = password;
8+
}
9+
10+
public String getUsername() {
11+
return username;
12+
}
13+
14+
public String getPassword() {
15+
return password;
16+
}
17+
}

user_data.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kavindu,sachinthe

0 commit comments

Comments
 (0)