-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
105 lines (92 loc) · 3.82 KB
/
MainClass.java
File metadata and controls
105 lines (92 loc) · 3.82 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
import java.util.*;
public class MainClass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
empServices service = new empServices();
boolean exit = false;
while (!exit) {
System.out.println("\n=== Employee Management Menu ===");
System.out.println("1. Insert New Employees");
System.out.println("2. View All Employees");
System.out.println("3. View Sorted Employees by Name");
System.out.println("4. View Employees from Noida");
System.out.println("5. Exit");
System.out.print("Enter your choice (1-5): ");
String choice = sc.nextLine().trim();
switch (choice) {
case "1":
insertEmployees(sc, service);// new change
break;
case "2":
displayEmployees(service.selectFromTable());
break;
case "3":
List<Employee> sorted = service.selectFromTable();
service.arraySortComparator(sorted);
break;
case "4":
List<Employee> allEmp = service.selectFromTable();
List<Employee> noidaEmp = service.getNoidaEmp(allEmp);
displayEmployees(noidaEmp);
break;
case "5":
System.out.println("Exiting...");
exit = true;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
service.closeService();
sc.close();
System.out.println("Program terminated.");
}
private static void insertEmployees(Scanner sc, empServices service) {
ArrayList<Employee> employees = new ArrayList<>();
System.out.print("Enter number of employees to add: ");
int n = getValidIntInput(sc);
for (int i = 0; i < n; i++) {
System.out.println("\nEnter details for Employee " + (i + 1));
String name = getValidStringInput(sc, "Name");
int age = getValidIntInput(sc, "Age");
String dept = getValidStringInput(sc, "Department");
String location = getValidStringInput(sc, "Location");
employees.add(new Employee(name, age, dept, location));
}
service.insertIntoTable(employees);
System.out.println("Employees added successfully.");
}
private static void displayEmployees(List<Employee> employees) {
if (employees.isEmpty()) {
System.out.println("No employee records found.");
} else {
for (Employee e : employees) {
System.out.println(e);
}
}
}
private static String getValidStringInput(Scanner sc, String field) {
while (true) {
System.out.print("Enter " + field + ": ");
String input = sc.nextLine().trim();
if (!input.isEmpty())
return input;
System.out.println(field + " cannot be empty. Try again.");
}
}
private static int getValidIntInput(Scanner sc, String field) {
while (true) {
try {
System.out.print("Enter " + field + ": ");
return Integer.parseInt(sc.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println("Invalid number. Try again.");
}
}
}
private static int getValidIntInput(Scanner sc) {
return getValidIntInput(sc, "number");
}
}
// javac -cp ".;mysql-connector-j-9.2.0.jar" *.java
// java -cp ".;mysql-connector-j-9.2.0.jar" MainClass