Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
136 lines (117 loc) · 5.22 KB
/
Copy pathMain.java
File metadata and controls
136 lines (117 loc) · 5.22 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
import java.sql.*;
import java.util.Scanner;
import reports.Reports;
public class Main {
String user = "";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("starting");
String inp = "";
// Add new
while (!inp.equals("exit")) {
System.out.println("Welcome to the Employee Managment System. Enter keywords to do commands.");
System.out.println("\tlogin - Login as an employee or HR head.");
System.out.println("\treports - Sign in and run payroll reports.");
System.out.println("\texit - Exit program.");
inp = scanner.nextLine().toLowerCase();
switch (inp) {
case "login":
authenticate();
break;
case "reports":
runReportsMenu(scanner);
break;
case "exit":
break;
default:
System.out.println("Keyword not recognized. Please retype.");
break;
}
}
System.out.println("exited loop");
}
static void authenticate(){
// user types either MANAGER or [employee name] and enters password "password"
System.out.println("authentication would go here");
}
// ---- Payroll reports
static final String URL = Env.get("DB_URL");
static final String USER = Env.get("DB_USER");
static final String PASSWORD = Env.get("DB_PASSWORD");
/** Sign in by employee ID, then dispatch the requested payroll report. */
static void runReportsMenu(Scanner keyboard) {
if (!configComplete()) {
return;
}
System.out.print("Enter your employee ID: ");
int userId = keyboard.nextInt();
keyboard.nextLine();
String role = getUserRole(userId, URL, USER, PASSWORD);
if (role == null) {
System.out.println("\n\n****ERROR**** Employee ID " + userId + " is not authorized\n\n");
return;
}
System.out.println("\nSigned in as employee " + userId + " [role: " + role + "]");
while (true) {
System.out.println("\n==== PAYROLL REPORTS ====");
System.out.println("1) My pay statement history ................ [employee]");
System.out.println("2) Total pay for month by job title ........ [HR admin]");
System.out.println("3) Total pay for month by division ......... [HR admin]");
System.out.println("4) New employee hires within a date range .. [all employees]");
System.out.println("5) All employees grouped by job title ...... [HR admin]");
System.out.println("0) Exit");
System.out.print("Choose a report: ");
String choice = keyboard.nextLine().trim();
switch (choice) {
case "1": Reports.Reports("paystub", userId, role, URL, USER, PASSWORD, keyboard); break;
case "2": Reports.Reports("paybytitle", userId, role, URL, USER, PASSWORD, keyboard); break;
case "3": Reports.Reports("paybydivision", userId, role, URL, USER, PASSWORD, keyboard); break;
case "4": Reports.Reports("newhires", userId, role, URL, USER, PASSWORD, keyboard); break;
case "5": Reports.Reports("employeesbytitle", userId, role, URL, USER, PASSWORD, keyboard); break;
case "0": System.out.println("Goodbye."); return;
default: System.out.println("\n\n****ERROR**** Invalid report requested\n\n");
}
}
}
private static boolean configComplete() {
boolean complete = true;
if (isBlank(URL)) {
System.out.println("ERROR: DB_URL is not set.");
complete = false;
}
if (isBlank(USER)) {
System.out.println("ERROR: DB_USER is not set.");
complete = false;
}
if (isBlank(PASSWORD)) {
System.out.println("ERROR: DB_PASSWORD is not set.");
complete = false;
}
if (!complete) {
System.out.println("Set these values in .env (see .env.example) or as environment variables.");
}
return complete;
}
private static boolean isBlank(String value) {
return value == null || value.isEmpty();
}
public static String getUserRole(int userId, String url, String user, String password) {
String sqlcommand = "SELECT role FROM user_roles WHERE empID = ?";
String role = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try (Connection myConn = DriverManager.getConnection(url, user, password);
PreparedStatement myStmt = myConn.prepareStatement(sqlcommand)) {
myStmt.setInt(1, userId);
try (ResultSet myRS = myStmt.executeQuery()) {
if (myRS.next()) {
role = myRS.getString("role");
}
}
}
} catch (Exception e) {
System.out.println("ERROR " + e.getLocalizedMessage());
}
return role;
}
}