-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cart.java
343 lines (310 loc) · 9.86 KB
/
Cart.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
342
343
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.Random;
// Java-->Driver-->Mysql
public class Cart {
static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:XE";
static final String USER = "Nishit";
static final String PASS = "hello123";
public static void fun(Scanner scan, Integer Id1) {
Connection conn = null;
Statement stmt = null;
ResultSet r = null, res = null, tp = null;
try {
//STEP 1: Register JDBC driver
Class.forName(JDBC_DRIVER);
//STEP 2: Open a connection
//System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 3: Execute a query
// System.out.println("Creating statement...");
stmt = conn.createStatement();
//Welcome user!
System.out.println("Welcome to the gift Purchasing Program!");
//Initialize our querying variable
boolean querying = true;
try {
while (querying) {
//Print the menu
System.out.println();
System.out.println("********************************");
System.out.println("Please select a query below!");
//2 available options
System.out.println("1. Check Your Orders");
System.out.println("2. Check Out Cart");
System.out.println("3. Return to dashboard");
System.out.println("********************************");
System.out.println();
System.out.println("Enter Your Choice");
String input = scan.nextLine();
int answer;
try {
answer = Integer.valueOf(input);
} catch (Exception e) {
answer = -1;
}
switch (answer) {
case 1:
try {
String resq = "Select * from usercart where CUSTOMERID=" + Id1;
System.out.println("sfsdgsdg" + resq);
r = stmt.executeQuery(resq);
int cnt = 0;
System.out.println("GIFTID QUANTITY PRICE");
while (r.next())
{
Integer giftID = r.getInt("GIFTID");
Integer quant = r.getInt("QUANTITY");
//String giftq = "Select * from giftitems where ID=" + giftID;
//System.out.println("gifftttttttttttt" + giftq);
Integer price = -1;
// try {
// tp = stmt.executeQuery(giftq);
//// try
//// {
// if (tp.next()) {
// //price = quant * tp.getInt("PRICE");
// }
// //}
//// catch(Exception e)
//// {
//// System.out.println("GIFT ID not retreived");
//// System.out.println(e);
//// }
// }
//
// catch (Exception e) {
// System.out.println("Databse error");
// System.out.println(e);
// }
System.out.println(giftID + " " + quant + " " + price);
cnt++;
}
if (cnt == 0) {
System.out.println("YOUR CART IS EMPTY");
}
} catch (Exception e) {
System.out.println("Databse error1");
e.printStackTrace();
}
r.close();
break;
case 2:
String add;
System.out.println("Enter your address for delivery");
add = scan.nextLine();
System.out.println("Are you sure you want to check out your cart\n Enter 1 to confirm\n Enter 0 to cancel");
String getconfirm;
getconfirm = scan.nextLine();
Integer confirm = -1;
try {
confirm = Integer.parseInt(getconfirm);
} catch (Exception e) {
System.out.println("Invalid Choice");
}
if (confirm == 1) {
try {
String query = "Select * from usercart where CUSTOMERID=" + Id1;
res = stmt.executeQuery(query);
while (res.next()) {
Random rand = new Random();
Integer giftID = res.getInt("GIFTID");
Integer bookingid = rand.nextInt (1000000000) + 1;
String ins = "Insert into bookings values(" + bookingid + "," + Id1 + "," + giftID + "," + "'" + add + "'" + ")";
//System.out.println("insert:" + ins);
//stmt.executeUpdate(ins);
}
//String delf = "Delete from usercart where CUSTOMERID=" + Id1;
//stmt.executeUpdate(delf);
} catch (Exception e) {
System.out.println("Unable to access cart");
System.out.println(e);
e.printStackTrace();
}
}
break;
default:
querying = false;
break;
}
}
}
catch (Exception e) {
System.out.println(e);
}
} catch (SQLException | ClassNotFoundException e) {
System.out.println("sdgsH");
e.printStackTrace();
} finally {
//finally block used to close resources
try {
if (res != null)
res.close();
if (r != null)
r.close();
if (tp != null)
tp.close();
} catch (Exception e) {
System.out.println("error closing" + e);
}
try {
if (stmt != null)
conn.close();
} catch (SQLException se) {
}// do nothing
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
//Inform user of error
System.out.println("Error closing connection to the database!");
}//end finally try
}//end try
System.out.println("Goodbye!");
}
// public void fun2(Scanner scan, Integer Id1)
// {
// Connection conn = null;
// Statement stmt = null;
//
// try{
// //STEP 1: Register JDBC driver
// Class.forName(JDBC_DRIVER);
//
// //STEP 2: Open a connection
// System.out.println("Connecting to a selected database...");
// conn = DriverManager.getConnection(DB_URL, USER, PASS);
// System.out.println("Connected database successfully...");
//
// //STEP 3: Execute a query
// System.out.println("Creating statement...");
// stmt = conn.createStatement();
//
//
// //Welcome user!
// System.out.println("Welcome to the gift Purchasing Program!");
//
// //Initialize our querying variable
// boolean querying = true;
//
// try {
// while(querying)
// {
// //Print the menu
// System.out.println();
// System.out.println("********************************");
// System.out.println("Please select a query below!");
// //1 available option
// System.out.println("1. Confirm Order");
// System.out.println("********************************");
// System.out.println();
//
// System.out.println("Enter Your Choice");
// String input = scan.nextLine();
// int answer;
// try{
// answer=Integer.valueOf(input);
// }
// catch(Exception e)
// {
// answer=-1;
// }
// if(answer == -1 || answer < 1 || answer > 1)
// {
// //User entered incorrect input
// System.out.println("Incorrect Input!");
// System.out.println("Please enter a number 1-1");
// }
//
// Random rand = new Random();
// bookingid = rand.nextInt (100000) + 1; // [1-100000]
//
//
// // cart db-> search(will return array) -> bookings(with id and address)
// // user id, booking id, address,
//
// else if (answer==1)
// {
// System.out.println("Please enter the gift");
// input = scan.nextLine();
// String sql = "SELECT Availablity FROM wm WHERE LOWER(Title) LIKE LOWER('%" + input + "%')";
// ResultSet rs = stmt.executeQuery(sql);
//
// if(rs.next())
// {
// String sql1 = "INSERT INTO wm SET wm.Availablity = ? where wm.Title=?";
// PreparedStatement stmt1 = conn.prepareStatement(sql1);
// stmt1.setInt(1, val);
// stmt1.setString(2, input);
// stmt1.executeUpdate();
// System.out.println("Enter your Address");
// String addr= scan.nextLine();
// String sql2 = "INSERT INTO book Values" + "("+ Id1 + ", " + "'" + input + "', " + "'" + addr + "')";
// stmt.execute(sql2);
//
// System.out.println("Order Confirmed");
//
// }
//
// else
// {
// System.out.println("This gift item does not exist...");
// }
//
// rs.close();
// }
//
//
//
//
// catch(Exception e)
// {
// e.printStackTrace();
// System.out.println("Error! Could Not get result from the Database");
// }
//
// }
//
//
//
//
// }
//
// }
// catch (Exception e)
// {
// System.out.println(e);
// }
// }
// catch(SQLException | ClassNotFoundException e)
// {
// e.printStackTrace();
// }
// finally{
// //finally block used to close resources
// try{
// if(stmt!=null)
// conn.close();
// }catch(SQLException se){
// }// do nothing
// try{
// if(conn!=null)
// conn.close();
// }catch(SQLException se){
// se.printStackTrace();
//
// //Inform user of error
// System.out.println("Error closing connection to the database!");
// }//end finally try
// }//end try
// System.out.println("Goodbye!");
// }
};