-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteToDatabase.java
More file actions
202 lines (179 loc) · 6.56 KB
/
WriteToDatabase.java
File metadata and controls
202 lines (179 loc) · 6.56 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
202
package poised;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.LocalDate;
/**
* A class with a dedicated method that writes new project information to
* the PoisePMS database.
*
* @author Nadia
* @version 2.0
* @since 1 June 2022
*
*/
public abstract class WriteToDatabase {
// ################ Constants and Constructor #################
/**
* Constructor made explicit and private so the class can't be instantiated
* by the implicit public one.
*/
private WriteToDatabase() {
}
/**
* Formats numbers used for currency to two decimal places {
* @value #DEC_FORMATTER}
*/
static final NumberFormat DEC_FORMATTER = new DecimalFormat("#0.00");
// ######################## Method ###############################
/**
* Takes in a Project object, connects to the PoisePMS database,
* gets the Project and associated Person object's information and
* updates the relevant tables in the database with that information.
* <p>
* Used to add a new project to the database.
*
* @param projObj a Project and it's Person attributes that have
* values entered into by the user.
* @throws SQLException a JDBC API error
*/
public static void addProjectToDatabase(Project projObj)
throws SQLException {
// connect to the DB
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/PoisePMS?useSSL=false",
"otheruser",
"swordfish");
Statement statement1 = connection.createStatement();
Statement statement2 = connection.createStatement();
Statement statement3 = connection.createStatement();
Statement statement4 = connection.createStatement();
Statement statement5 = connection.createStatement();
Statement statement6 = connection.createStatement();
// get project information
String proj_num = projObj.getProjNumber();
String proj_name = projObj.getProjName();
String proj_addr = projObj.getProjAddress();
String proj_erf = projObj.getProjErf();
String proj_type = projObj.getProjType();
/* format prices to 2 decimals and change the default
* comma separator into a period */
String proj_cost = DEC_FORMATTER.format(projObj.getFeeTotal()).replace(',', '.');
String proj_paid = DEC_FORMATTER.format(projObj.getFeePaid()).replace(',', '.');
LocalDate proj_due_on = projObj.getProjDeadline();
Boolean proj_is_done = projObj.isProjFinalised();
// leaving proj_done_on as null
LocalDate proj_done_on = null;
// customer
String cust_Fname = projObj.getCustomer().getFirstName();
String cust_Lname = projObj.getCustomer().getLastName();
String cust_phone = projObj.getCustomer().getPhoneNumber();
String cust_email = projObj.getCustomer().getEmail();
String cust_addr = projObj.getCustomer().getWorkAddress();
// contractor
String cont_Fname = projObj.getContractor().getFirstName();
String cont_Lname = projObj.getContractor().getLastName();
String cont_phone = projObj.getContractor().getPhoneNumber();
String cont_email = projObj.getContractor().getEmail();
String cont_addr = projObj.getContractor().getWorkAddress();
// architect
String arch_Fname = projObj.getArchitect().getFirstName();
String arch_Lname = projObj.getArchitect().getLastName();
String arch_phone = projObj.getArchitect().getPhoneNumber();
String arch_email = projObj.getArchitect().getEmail();
String arch_addr = projObj.getArchitect().getWorkAddress();
// architect
String engr_Fname = projObj.getEngineer().getFirstName();
String engr_Lname = projObj.getEngineer().getLastName();
String engr_phone = projObj.getEngineer().getPhoneNumber();
String engr_email = projObj.getEngineer().getEmail();
String engr_addr = projObj.getEngineer().getWorkAddress();
// architect
String mang_Fname = projObj.getManager().getFirstName();
String mang_Lname = projObj.getManager().getLastName();
String mang_phone = projObj.getManager().getPhoneNumber();
String mang_email = projObj.getManager().getEmail();
String mang_addr = projObj.getManager().getWorkAddress();
// make changes to the project database with the information
statement1.executeUpdate(
"INSERT INTO project VALUES ("
+ "'" + proj_num + "', "
+ "'" + proj_name + "', "
+ "'" + proj_addr + "', "
+ "'" + proj_erf + "', "
+ "'" + proj_type + "', "
+ proj_cost + ", "
+ proj_paid + ", "
+ "'" + proj_due_on + "', "
+ proj_is_done + ", "
+ proj_done_on + ", "
+ "'" + cust_Fname + "', "
+ "'" + cust_Lname + "', "
+ "'" + cont_Fname + "', "
+ "'" + cont_Lname + "', "
+ "'" + arch_Fname + "', "
+ "'" + arch_Lname + "', "
+ "'" + engr_Fname + "', "
+ "'" + engr_Lname + "', "
+ "'" + mang_Fname + "', "
+ "'" + mang_Lname + "'"
+ ");");
// make changes to the customer database with the information
statement2.executeUpdate(
"INSERT INTO customer VALUES ("
+ "'" + cust_Fname + "', "
+ "'" + cust_Lname + "', "
+ "'" + cust_phone + "', "
+ "'" + cust_email + "', "
+ "'" + cust_addr + "', "
+ "'" + proj_num + "'"
+ ");");
statement3.executeUpdate(
"INSERT INTO contractor VALUES ("
+ "'" + cont_Fname + "', "
+ "'" + cont_Lname + "', "
+ "'" + cont_phone + "', "
+ "'" + cont_email + "', "
+ "'" + cont_addr + "', "
+ "'" + proj_num + "'"
+ ");");
// make changes to the architect database with the information
statement4.executeUpdate(
"INSERT INTO architect VALUES ("
+ "'" + arch_Fname + "', "
+ "'" + arch_Lname + "', "
+ "'" + arch_phone + "', "
+ "'" + arch_email + "', "
+ "'" + arch_addr + "', "
+ "'" + proj_num + "'"
+ ");");
// make changes to the engineer database with the information
statement5.executeUpdate(
"INSERT INTO engineer VALUES ("
+ "'" + engr_Fname + "', "
+ "'" + engr_Lname + "', "
+ "'" + engr_phone + "', "
+ "'" + engr_email + "', "
+ "'" + engr_addr + "', "
+ "'" + proj_num + "'"
+ ");");
// make changes to the manager database with the information
statement6.executeUpdate(
"INSERT INTO manager VALUES ("
+ "'" + mang_Fname + "', "
+ "'" + mang_Lname + "', "
+ "'" + mang_phone + "', "
+ "'" + mang_email + "', "
+ "'" + mang_addr + "', "
+ "'" + proj_num + "'"
+ ");");
// close connections
connection.close();
statement1.close();
statement2.close();
statement3.close();
statement4.close();
statement5.close();
statement6.close();
}
}