-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCustomers.java
More file actions
131 lines (124 loc) · 4.87 KB
/
Copy pathCustomers.java
File metadata and controls
131 lines (124 loc) · 4.87 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
import java.util.Scanner;
import java.sql.*;
public class Customers {
int a[] = new int[30];
int i;
// step1 load the driver class
// step2 create the connection object
Connection con = null;
// step3 create the statement object
Statement stmt = null;
PreparedStatement ps = null;
ResultSet rs;
Scanner sc = new Scanner(System.in);
String name, s;
int pid, Qty, orderno = 9001, custno = 000;
char ch = 'y';
int price = 0, Tot_Price = 0, exist_tot = 0,new_tot;
Customers() {
try {
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db?characterEncoding=latin1&useConfigs=maxPerformance", "scott",
"tiger");
stmt = con.createStatement();
// step1 load the driver class
Class.forName("com.mysql.jdbc.Driver");
// step2 create the connection object
} catch (Exception e) {
System.out.println(e);
}
// step3 create the statement object
}
public void Order(int flag) {
try {
Tot_Price = 0;
if (flag == 0) {
s = "select cid FROM Customers";
rs = stmt.executeQuery(s);
while (rs.next())
custno = rs.getInt(1);
custno++;
System.out.println("Enter your name");
String cname = sc.nextLine();
System.out.println("Enter your Phone Number");
long no = sc.nextLong();
System.out.println("Enter your Locality");
String loc = sc.nextLine();
loc = sc.nextLine();
s = "insert into customers values(" + custno + ",'" + cname + "'," + no + ",'"
+ loc + "',0)";
ps = con.prepareStatement(s);
ps.execute();
exist_tot = 0;
} else {
System.out.println("Enter your name");
String cname = sc.nextLine();
s = "select cid,total FROM Customers where cname = '" + cname+"'";
rs = stmt.executeQuery(s);
rs.next();
custno = rs.getInt(1);
exist_tot = rs.getInt(2);
}
s = "select oid FROM Orders";
rs = stmt.executeQuery(s);
while (rs.next())
orderno = rs.getInt(1);
orderno++;
System.out.println("Product list:\n");
s = "select product, price, qty FROM Stockings";
rs = stmt.executeQuery(s);
System.out.println("Product name\tPrice\tQuantity\n--------------------------");
while (rs.next())
System.out.println(rs.getString(1) + "\t" + rs.getInt(2) + "\t" + rs.getInt(3));
for (i = 0; i < 30; i++)
a[i] = 0;
i = 0;
do {
System.out.println("Enter the name of the product: ");
name = sc.nextLine();
System.out.println("Quantity to be bought: ");
Qty = sc.nextInt();
s = "select qty,price,pid from Stockings where product = '" + name + "'";
rs = stmt.executeQuery(s);
rs.next();
int Quantity = rs.getInt(1);
pid = rs.getInt(3);
a[i] = pid;
a[i + 1] = Qty;
a[i + 2] = Quantity - a[i + 1];
i += 3;
if (i > 29) {
System.out.println("Exceeded cloud storage");
break;
}
if (Qty > Quantity) {
System.out.println("Exceeded quantity in stock!");
break;
}
price = rs.getInt(2);
Tot_Price += price * Qty;
System.out.println("Total price: " + Tot_Price);
System.out.println("Do you want to order more products?");
ch = sc.next().charAt(0);
name = sc.nextLine();
} while (ch == 'y');
for (int j = 0; j < i; j += 3) {
s = "insert into orders values(" + orderno + "," + a[j] + "," + custno + ","
+ a[j + 1] + ",SYSDATE())";
ps = con.prepareStatement(s);
ps.execute();
s = "update stockings set qty = " + a[j + 2] + " WHERE pid = " + a[j];
ps = con.prepareStatement(s);
ps.execute();
}
new_tot = Tot_Price+exist_tot;
s = "update customers set total = " + new_tot + " WHERE cid = " + custno;
ps = con.prepareStatement(s);
ps.execute();
orderno++;
// Set the date of closing as sysdate + random number
} catch (Exception e) {
System.out.println(e);
}
}
}