-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.java
More file actions
96 lines (68 loc) · 2.3 KB
/
Worker.java
File metadata and controls
96 lines (68 loc) · 2.3 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
import java.sql.*;
import java.util.*;
public class Worker {
public static Scanner scan = new Scanner(System.in);
public static void newWorker(Connection con) throws SQLException{
System.out.println("New Worker Entry");
String first,last,ssn,select;
int globalID, jobID,x;
String tableName = "worker";
System.out.print("First Name: ");
first = scan.next();
System.out.print("Last Name: ");
last = scan.next();
System.out.print("SSN: ");
ssn = scan.next();
System.out.println("Select Job Title: ");
String[] id = {"title","jobID"};
String[][] titles = TableMethods.selectXFrom("jobTitle",id, con);
//List available Job Titles
for(int i = 0; i < titles.length; i++){
System.out.println(" " + (i+1) + ": " + titles[i][0]);
}
System.out.print("Enter: ");
//convert string id to int id
select = scan.next();
x = Integer.parseInt(select);
jobID = Integer.parseInt(titles[x-1][1]);
globalID = getNextID(jobID,con);
System.out.println(globalID + "");
String query = "INSERT INTO " + tableName + " VALUES (" +
"'" + first + "', " +
"'" + last + "', " + globalID + ", " + ssn + "," + jobID+ ");";
TableMethods.insertAll(con, query);
System.out.println("\n"+first + " " +
last + " " +
ssn + " " +
jobID + " " +
globalID + " ");
} // end new worker
/* =======================================================================
* Since global ID is not incremented manually and is set based on
* jobTitle this will find the next available id for a specific job title.
* @params int, Connection
* @return int
* =====================================================================*/
public static int getNextID(int category, Connection con) throws SQLException{
int globalID = 0;
String tableName = "worker";
String id = "globalID";
int low = category * 100000000;
int high = (category + 1) * 100000000;
Statement stmt =con.createStatement();
String query = "SELECT max(" + id + ") FROM " + tableName +
" WHERE " + id + ">=" + low + " and " + id + " < " + high + ";";
ResultSet rs = stmt.executeQuery(query);
int max = 0;
while (rs.next()){
max = rs.getInt(1);
}
if(max == 0){
max = low;
}
else {
++max;
}
return max;
}
}