Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
201 lines (167 loc) · 7.34 KB
/
Copy pathMain.java
File metadata and controls
201 lines (167 loc) · 7.34 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
import java.sql.*;
import java.util.Scanner;
import reports.Reports;
public class Main {
static UserRole user_role = UserRole.GUEST; // start the user as a guest
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inp = "";
// Add new
while (!inp.equals("exit")) {
System.out.println("");
System.out.println("Welcome to the Employee Managment System. Enter keywords to do commands.");
System.out.print("Logged in as: ");
switch (user_role) {
case UserRole.EMPLOYEE:
System.out.println("Employee");
break;
case UserRole.HR_ADMIN:
System.out.println("HR Admin");
break;
default:
System.out.println("Guest");
break;
}
System.out.println("");
System.out.println("\tlogin - Login as an employee or HR head. Selecting this when logged in will log you out first.");
System.out.println("\treports - Sign in and run payroll reports.");
System.out.println("\texit - Exit program.");
inp = scanner.nextLine().toLowerCase();
switch (inp) {
case "login":
login();
break;
case "reports":
runReportsMenu(scanner);
break;
case "exit":
break;
default:
System.out.println("Keyword not recognized. Please retype.");
break;
}
}
System.out.println("exited loop");
}
// login system
static void login(){ // ask user for a username and password
// logout user
user_role = UserRole.GUEST;
Scanner scanner = new Scanner(System.in);
String inp = "";
System.out.println("Enter your username, or type logout to be reset to Guest.");
inp = scanner.nextLine().toLowerCase();
System.out.println(inp);
// sign in as an employee: username = "employee"
// HR sign in: username = "hr"
switch (inp) {
case "employee":
// mock request password
System.out.println("Signing in as Employee. Please enter a password.");
inp = scanner.nextLine().toLowerCase();
user_role = UserRole.EMPLOYEE;
System.out.println("Now logged in as Employee.");
break;
case "hr":
// mock request password
System.out.println("Signing in as HR Admin. Please enter a password.");
inp = scanner.nextLine().toLowerCase();
user_role = UserRole.HR_ADMIN;
System.out.println("Now logged in as HR Admin.");
break;
case "logout":
System.out.println("Logging out.");
break;
default:
System.out.println("Error: Username not recognized. Defaulting to Guest.");
break;
}
}
static Boolean confirmRole(UserRole role_to_check){
return (user_role == role_to_check);
}
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;
}
}