-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConsumer.java
More file actions
320 lines (306 loc) · 13.4 KB
/
Consumer.java
File metadata and controls
320 lines (306 loc) · 13.4 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
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
//package Connection;
import java.sql.*;
import java.util.*;
public class Consumer {
Scanner sc=new Scanner(System.in);
int customer_id=18;int order_id=15;int productcount=0;int cost=0;
int count1=0;int count2=0;int noflag=0;
//customer login function
public int customer_login(Connection con) {
try {
Statement stmt=con.createStatement();
try {
System.out.println("--------------------------------------------------------------");
System.out.println("Enter login id:");
String loginid=sc.next();
System.out.println("Enter Password:");
String password=sc.next();
System.out.println("--------------------------------------------------------------");
ResultSet rs=stmt.executeQuery("select * from customer where login_id='"+loginid+"'"+ "and login_password='"+password+"'");
boolean isnotEmpty = rs.first();
if(isnotEmpty) {
customer_id=rs.getInt(1);
stmt.close();
return 1;//logged in successfully
}
else return 0;// not able to login
}
catch(Exception e) {
System.out.println(e);
}
}
catch(Exception e) {
System.out.println(e);
}
return 0;
}
public int customer_register(Connection con){
System.out.println("***********************************");
System.out.print("Enter your login_id");String loginid=sc.nextLine();System.out.println();
System.out.print("Enter your password");String password=sc.nextLine();System.out.println();
System.out.println("--------------------------------------------------------------");
System.out.print("Enter your first name");String fname=sc.nextLine();System.out.println();
System.out.print("Enter your lastname");String lname=sc.nextLine();System.out.println();
System.out.println("--------------------------------------------------------------");
System.out.print("Enter your address line1");String address1=sc.nextLine();System.out.println();
System.out.print("Enter your address line2");String address2=sc.nextLine();System.out.println();
System.out.println("--------------------------------------------------------------");
System.out.print("Enter your city");String city=sc.nextLine();System.out.println();
System.out.print("Enter your pincode");String pincode=sc.nextLine();System.out.println();
System.out.print("Enter your contact");String contact=sc.nextLine();System.out.println();
System.out.print("Enter your country code");String concode=sc.nextLine();System.out.println();
System.out.print("Enter your creditlimit");String creditlimit=sc.nextLine();System.out.println();
System.out.println("***********************************");
try {
Statement stmt=con.createStatement();
try {
java.util.Date javaDate = new java.util.Date();
long javaTime = javaDate.getTime();
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(javaTime);
String join=sqlTimestamp.toString();
int x = 0;
String query1="INSERT into customer (`login_id`,`login_password`,`first_name`, `last_name`,`address_line1`,`address_line2`,`city`,`pin_code`,`contact`,`country_code`,`credit_limit`,`joined_at`,`points`)";
String query2="VALUES ('"+loginid+"','"+password+"','"+fname+"','"+lname+"','"+address1+"','"+address2+"','"+city+"','"+pincode+"','"+contact+"','"+concode+"','"+creditlimit+"','"+join+"','"+x+"' )";
String final_query=query1+query2;
stmt.executeUpdate(final_query);
stmt.close();
return 1;
}
catch(Exception e) {
System.out.println(e);
return 0;
}
}
catch(Exception e) {
System.out.println(e);
}
return 0;
}
public int showresult(Connection con,int flag) {
try {
Statement stmt=con.createStatement();
// flag==1 for showing all the products
try {
if(flag==1) {
System.out.println("--------------------------------------------------------------");
ResultSet rs=stmt.executeQuery("select product_name,product_price,product_rating from products where quantity_available>0" );
while(rs.next()) {
System.out.println("product "+rs.getString("product_name")+" price "+rs.getString("product_price")+" ratings "+rs.getString("product_rating"));
System.out.println("--------------------------------------------------------------");
}
return 1;
}
//flag==2 for showing products by category
else if(flag==2) {
if(count1==0 || noflag==1) {
sc.nextLine();
noflag=0;
count1++;
}
System.out.println("please enter the category");
String category=sc.nextLine();
System.out.println(category);
ResultSet rs=stmt.executeQuery("select p.product_name,p.product_price,p.product_rating,c.category_name from products as p,categories as c where p.quantity_available>0 and p.category_id=c.category_id and c.category_name='"+category+"'");
while(rs.next()) {
System.out.println("product "+rs.getString("p.product_name")+" price "+rs.getString("p.product_price")+" ratings "+rs.getString("p.product_rating")+" category "+rs.getString("c.category_name"));
System.out.println("--------------------------------------------------------------");
}
stmt.close();
return 1;
}
// flag==3 shows all the categories available
else if(flag==3) {
ResultSet rs=stmt.executeQuery("select category_name from categories");
System.out.println("--------------------------------------------------------------");
while(rs.next()) {
System.out.println(rs.getString("category_name"));
System.out.println("--------------------------------------------------------------");
}
stmt.close();
return 1;
}
// flag==4 allows user to search particular product and add to cart
else if(flag==4) {
System.out.println("Search for product and add to cart");
if(count1==0 || noflag==1) {
String prod1=sc.nextLine();
count1++;
noflag=0;
}
String prod=sc.nextLine();
System.out.println("you entered ==> "+prod);
System.out.println("--------------------------------------------------------------");
ResultSet rs1=stmt.executeQuery("select count(*) from orders");
while(rs1.next()) {
order_id=rs1.getInt(1)+1;// this tracks the order_id for a particular order
}
ResultSet rs=stmt.executeQuery("select product_name,product_price,product_rating,quantity_available,product_id from products where product_name='"+prod+"'");
if(!rs.first()) {
System.out.println("*********Sorry no product with this name try again************");
return 0;
}
int product_price=rs.getInt("product_price");
int product_id=rs.getInt("product_id");
System.out.println("product "+rs.getString("product_name")+" price "+Integer.toString(product_price)+" ratings "+rs.getString("product_rating"));
System.out.println("--------------------------------------------------------------");
System.out.println("want to add this product to cart yes/no");
String input=sc.next();
System.out.println("--------------------------------------------------------------");
if(input.equals("yes")) {
productcount++;
int quantity_available=rs.getInt(4);
System.out.println("quantity, please put less than "+quantity_available);
int k=sc.nextInt();
if(k>quantity_available) {
System.out.println("********please enter less than quantity availbale********");// return if input quantity is less than the available quantity
return 0;
}
int price=product_price*k;
cost+=price;
System.out.println(productcount+" "+cost);
String pr=Integer.toString(price);
stmt.executeUpdate("INSERT INTO order_items VALUES ("+Integer.toString(order_id)+","+Integer.toString(product_id)+","+Integer.toString(k)+","+pr+","+"0"+","+pr+")" );
String qu="UPDATE products SET quantity_available ="+Integer.toString(quantity_available-k)+" WHERE product_name='"+prod+"'";
stmt.executeUpdate(qu);
}
else {
noflag=1;
return 1;
}
ResultSet rs2=stmt.executeQuery("select count(*) from delivery_executive");
int del=1;
if(rs2.next()) {
del=rs2.getInt(1);
}
System.out.println("finish order yes/no");
System.out.println("--------------------------------------------------------------");
String inp1=sc.next();
if(inp1.equals("yes")) {
Random rand = new Random();
int del_id = rand.nextInt(del);
String del_exec=Integer.toString(del_id++);
order_id++;// just to debug variable
System.out.println("Please enter order address===>");
sc.nextLine();
System.out.println("Address_line_1: ");
String address1=sc.nextLine();
System.out.println("Address_line_2: ");
String address2=sc.nextLine();
System.out.println("Pincode: ");
String pincode=sc.nextLine();
System.out.println();
System.out.println("Order added to cart Successfully!");
java.util.Date javaDate = new java.util.Date();
long javaTime = javaDate.getTime();
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(javaTime);
String join=sqlTimestamp.toString();
String orderq="INSERT INTO orders(customer_id,status,order_ship_address_line1,order_ship_address_line2,order_ship_city_pincode,"
+ "order_ship_country,order_delivery_partner,created_at)"
+ " VALUES ("+Integer.toString(customer_id)+",'active','"+address1+"','"+address2+"',"+pincode+",'India',"+del_exec+",'"+join+"')";
stmt.executeUpdate(orderq);
stmt.executeUpdate("INSERT INTO cart VALUES ("+Integer.toString(customer_id)+","+Integer.toString(cost)+","+Integer.toString(productcount)+")");
//System.out.println(productcount+" "+cost);
productcount=0;cost=0;
return 1;
}
else {
noflag=1;
}
return 1;
}
// flag==5 is for checking out the cart
if(flag==5) {
ResultSet rs1=stmt.executeQuery("SELECT points FROM customer where customer_id="+Integer.toString(customer_id));
int points=0;
if(rs1.next()) {
points = rs1.getInt("points");
}
ResultSet rs=stmt.executeQuery("SELECT sum(total_amount),sum(number_of_products) FROM cart where customer_id="+Integer.toString(customer_id));
int totc=0;
int totp=0;
int totc1=0;
if(rs.first()) {
totc=rs.getInt(1);
totp=rs.getInt(2);
if(totc<0) totc=0;
if(totp<0) totp=0;
System.out.println("total cost "+totc+" total products "+ totp);
//applying reward points
System.out.println("You have "+ points + " reward points in your account. Would you like to use them? yes/no?");
String reply = sc.next();
if(reply.equals("yes"))
{
if(points > totc)
{
totc1 = totc - (points/10);
points = points - totc1;
System.out.println("new price :==>"+" "+totc1);
stmt.executeUpdate("Update customer set points=" + points + " where customer_id=" + Integer.toString(customer_id));
}
else
{
totc1 = totc - (points/10);
points = 0;
System.out.println("new price :==>"+" "+totc1);
stmt.executeUpdate("Update customer set points=" + points + " where customer_id=" + Integer.toString(customer_id));
}
}
}
System.out.println("checkout yes/no");
String yn=sc.next();
System.out.println("--------------------------------------------------------------");
if(yn.equals("yes")) {
System.out.println("+++++++order placed successfully++++++++++++");
stmt.executeUpdate("INSERT INTO cart VALUES ("+Integer.toString(customer_id)+","+Integer.toString(-totc)+","+Integer.toString(-totp)+")");
System.out.println(totc + "points have been added to your reward points!");
int totPoints = points + totc1;
stmt.executeUpdate("Update customer set points=" + totPoints + " where customer_id=" + Integer.toString(customer_id));
return 1;
}
else {
noflag=0;
return 1;
}
}
//flag 7 is to submit product rating
if(flag==7) {
String cust_id=Integer.toString(customer_id);
String q="SELECT distinct(p.product_name) from orders as o,order_items as od,products as p where o.customer_id="+cust_id +" and od.order_id=o.order_id and p.product_id=od.product_id";
ResultSet rs=stmt.executeQuery(q);
// shows all previously ordered products
System.out.println("previously ordered items");
while(rs.next()) {
System.out.println("product "+rs.getString("p.product_name"));
}
System.out.println("--------------------------------------------------------------");
System.out.print("submit the name of the products to rate:");
//if(count1==0) {
sc.nextLine();
count1++;
//}
String prod=sc.nextLine();
System.out.println("enter rating between 0 to 5");
int ratings=sc.nextInt();
rs=stmt.executeQuery("SELECT product_id from products where product_name='"+prod+"'");
int p_id=0;
while(rs.next()) {
p_id=rs.getInt(1);
}
String q1="INSERT INTO feedback (customer_id,product_id,product_rating) VALUES ("+cust_id+","+Integer.toString(p_id)+","+Integer.toString(ratings)+")";
stmt.executeUpdate(q1);
stmt.close();
System.out.println("--------------------------------------------------------------");
System.out.println("feedback submitted successfully");
return 1;
}
}
catch(Exception e) {
System.out.println(e);
}
}
catch(Exception e) {
System.out.println(e);
}
return 0;
}
}