-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSupplier.java
341 lines (322 loc) · 13.6 KB
/
Supplier.java
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import java.sql.*;
import java.util.*;
import java.sql.SQLException;
import java.text.ParseException;
public class Supplier {
static Scanner scan = new Scanner(System.in);
static int input = 0;
static ResultSet rs = null;
//Declaring and Instantiating all the attribute of a Supplier
static int supplierID = -1;
static String name = "";
static String phone = "";
static String email = "";
static String location = "";
static double amountOwed = -1.00;
/**
* This method is called in the MainMenu file when the user wants to add/edit/delete/view suppliers in the database.
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static void supplierMenu() throws ClassNotFoundException, SQLException, ParseException{
do{
System.out.println("Supplier Menu");
System.out.println();
System.out.println("0. Return to Information Processing Menu");
System.out.println("1. View All Suppliers");
System.out.println("2. View Supplier by ID");
System.out.println("3. Add Supplier");
System.out.println("4. Edit Supplier");
System.out.println("5. Delete Supplier");
System.out.println();
System.out.println("Please choose an option from the menu: ");
input = scan.nextInt();
switch(input){
case 0:
System.out.println("Going back to Information Processing Menu");
return;
case 1:
viewAllSuppliers();
break;
case 2:
viewSupplier();
break;
case 3:
addSupplier();
break;
case 4:
editSupplier();
break;
case 5:
deleteSupplier();
break;
default:
System.out.println("Invalid input");
break;
}
input = -1;
} while(input != 6);
}
/**
* Method displays all suppliers in the database
*/
public static void viewAllSuppliers() throws ClassNotFoundException, SQLException, ParseException{
try{
rs = SupplierSQL.viewAllSuppliers();
printSupplier(rs);
}
catch(SQLException e){
System.out.println("SQL Exception");
e.getStackTrace();
}
}
/**
* Method displays information of a single supplier
*/
public static void viewSupplier() throws ClassNotFoundException, SQLException, ParseException {
do{
System.out.println("In order to go back to Supplier Menu, enter 0");
System.out.println("Please enter a supplier ID to view a supplier");
System.out.println();
System.out.println("Supplier ID: ");
input = scan.nextInt();
scan.nextLine();
if(input > 0){
rs = SupplierSQL.viewSupplier(input);
printSupplier(rs);
rs.close();
} else if(input < 0){
System.out.println("Invalid input");
} else if(input == 0){
System.out.println("Going back to Supplier Menu");
return;
}
} while(input != 0);
}
/**
* Method adds a suppliers information to database by calling the addSupplier in the SupplierSQl file
*/
public static void addSupplier() throws ParseException, ClassNotFoundException, SQLException{
//All the current attributes on the first iteration will be empty, as the user begins to fill in attributes
//of the supplier the display will be updated. Before the user selects 10, all the attributes must be filled
//so that the supplier can be created in the database.
do{
System.out.println("0. Go back to Supplier menu: ");
System.out.println("1. Supplier ID: " + supplierID);
System.out.println("2. Supplier Name: " + name);
System.out.println("3. Phone Number: " + phone);
System.out.println("4. Email: " + email);
System.out.println("5. Location: " + location);
System.out.println("6. Amount Owed: " + amountOwed);
System.out.println("7. After all attributes have been entered, select 7 to create the new supplier:");
System.out.println();
System.out.println("Select a number to add to supplier information: ");
input = scan.nextInt();
scan.nextLine();
updateAttributes(input, "a");
} while(input != 0);
rs.close();
}
/**
* Method edits a supplier information to database by calling the editSupplier in the SupplierSQl file
*/
public static void editSupplier() throws ParseException, ClassNotFoundException, SQLException{
int input = 0;
do{
System.out.println("In order to go back to Supplier Menu, enter 0");
System.out.println("Please enter a supplier ID to edit a supplier");
input = scan.nextInt();
scan.nextLine();
System.out.println();
//SupplierID must be a number greater than 0 so that the application can search for that supplier.
if(input > 0){
rs = SupplierSQL.viewSupplier(input);
//Checks if viewSupplier was able to find an existing Supplier
if(!rs.next()){
System.out.println("Supplier does not exist");
} else{
//The supplier exist and all the attributes of that supplier are being stored into variables so
//that they can be displayed to the user. In order for them to choose which attribute to edit.
supplierID = rs.getInt("supplierID");
name = rs.getString("name");
phone = rs.getString("phone");
email = rs.getString("email");
location =rs.getString("location");
amountOwed = rs.getDouble("amountOwed");
do{
//Attributes are displayed to the user and they must choose which one to edit.
//User will be reprompted these options until they decide to go back to Supplier Menu
System.out.println("0. Go back to Supplier Menu: ");
System.out.println("1. Supplier ID: " + supplierID);
System.out.println("2. Supplier Name: " + name);
System.out.println("3. Supplier Phone: " + phone);
System.out.println("4. Email: " + email);
System.out.println("5. Location: " + location);
System.out.println("6. Amount Owed: " + amountOwed);
System.out.println("7. After all attributes have been entered, select 7 to edit supplier:");
System.out.println();
System.out.println("Select a number to edit supplier information: ");
input = scan.nextInt();
scan.nextLine();
updateAttributes(input, "e");
} while(input != 0);
}
}
}while(input != 0);
}
/**
* Method deletes a supplier information in the database by calling the deleteSupplier in the SupplierSQl file
*/
public static void deleteSupplier(){
do {
System.out.println("In order to go back to Supplier Menu, enter 0");
System.out.println("Please enter a supplier ID to delete a supplier");
System.out.println();
System.out.println("Supplier ID: ");
input = scan.nextInt();
if(input > 0){
SupplierSQL.deleteSupplier(input);
} else if(input < 0){
System.out.println("Invalid input");
} else if (input == 0){
System.out.println("Going back to Supplier Menu");
return;
}
} while(input != 0);
}
/**
* Method is used in all the methods above to print out the information of a supplier. This method takes in
* a ResultSet and then prints out all the attributes each supplier of the ResultSet.
* @param rs
* @throws SQLException
*/
public static void printSupplier(ResultSet rs) throws SQLException{
if(!rs.next()){
System.out.println("There is no supplier with this supplierID");
} else{
do{
supplierID = rs.getInt("supplierID");
name = rs.getString("name");
phone = rs.getString("phone");
email = rs.getString("email");
location = rs.getString("location");
amountOwed = rs.getDouble("amountOwed");
System.out.println("Supplier ID: " + supplierID + ", name: " + name + ", phone: " + phone + ", email: "
+ email + ", location: " + location + ", amount owed: " + amountOwed);
} while(rs.next());
}
}
/**
* This method is used to reset all the variables to "", 0, or false
*/
public static void resetAttributes(){
supplierID = 0;
name = "";
phone = "";
email = "";
location = "";
amountOwed = 0.0;
}
/**
* From option that the user selects, this switch statement will prompt the user for the new value for
* specific attribute.
* Case 7 will take all the updated attributes and update the supplier in the query. Afterwards, it will
* reset all the variable fields to "" , 0, or false.
* @param input
* @param s
*/
public static void updateAttributes(int input, String s) throws ParseException, ClassNotFoundException, SQLException{
switch(input){
case 0:
System.out.println("Going back to Supplier Menu");
System.out.println();
resetAttributes();
return;
case 1:
System.out.println("Enter Supplier ID:");
supplierID = scan.nextInt();
break;
case 2:
System.out.println("Enter Supplier Name:");
name = scan.nextLine();
break;
case 3:
System.out.println("Enter Phone Number:");
phone = scan.nextLine();
break;
case 4:
System.out.println("Enter Email:");
email = scan.nextLine();
break;
case 5:
System.out.println("Enter Location:");
location = scan.nextLine();
break;
case 6:
System.out.println("Enter Amount Owed:");
amountOwed = scan.nextDouble();
break;
case 7:
try{
if(s.equals("e")){
SupplierSQL.editSupplier(supplierID, name, phone, email, location, amountOwed);
}
else if(s.equals("a")){
SupplierSQL.addSupplier(supplierID, name, phone, email, location, amountOwed);
}
} catch(SQLException e){
System.out.println("SQL Exception");
e.getStackTrace();
break;
}
rs = SupplierSQL.viewSupplier(supplierID);
printSupplier(rs);
resetAttributes();
input = -1;
return;
default:
System.out.println("Invalid input");
}
}
/**
* Method creates a bill for a single supplier
*/
public static void createSupplierBill() throws ClassNotFoundException, SQLException, ParseException {
do{
System.out.println("In order to go back to Billing Menu, enter 0");
System.out.println("Please enter a Supplier ID to generate a Supplier bill");
System.out.println();
System.out.println("Supplier ID: ");
input = scan.nextInt();
scan.nextLine();
if(input > 0){
rs = SupplierSQL.viewSupplier(input);
if(!rs.next()){
System.out.println("There is no supplier with this supplierID");
} else {
//rs.next();
supplierID = rs.getInt("supplierID");
name = rs.getString("name");
phone = rs.getString("phone");
email = rs.getString("email");
location = rs.getString("location");
amountOwed = rs.getDouble("amountOwed");
System.out.println("Supplier Bill For " + name);
System.out.println("Total Amount Owed $" + amountOwed);
System.out.println("Send Bill to " + location);
System.out.println("Supplier Contact Information ");
System.out.println("Phone " + phone);
System.out.println("Email " + email);
System.out.println();
rs.close();
}
} else if(input < 0){
System.out.println("Invalid input");
} else if(input == 0){
System.out.println("Going back to Billing Menu");
return;
}
} while(input != 0);
}
}