-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockManager.java
265 lines (240 loc) · 8.01 KB
/
StockManager.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
package finalExam;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
class myTextArea extends JTextArea{
myTextArea(){
Font font = new Font("Verdana", Font.BOLD, 15);
setFont(font);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
setColumns(20);
}
public void clear() {
setText("");
}
public String get() {
return getText().trim();
}
}
class myLabel extends JLabel{
myLabel(String name){
super(name);
Font font = new Font("Verdana", Font.BOLD, 15);
setFont(font);
}
}
class availabilityPanel extends JOptionPane{
availabilityPanel(String name, int n){
if(n <= 0) n = 0;
JFrame f = new JFrame();
String message;
if(name.equals("")) {
message = "Enter product name";
}
else {
message = name + " has " + n + " units in stock";
}
showMessageDialog(f,message);
}
}
public class StockManager extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
StockManager(){
super("Stock Manager");
try(Connection con = DriverManager.getConnection("jdbc:derby:stocks;create=true")){
Statement st = con.createStatement();
st.executeUpdate("create table stocks (name varchar(50), quantity int)");
}
catch(Exception ex1) {
System.out.println(ex1);
System.out.println("Table exists");
}
JPanel availabilityPanel = new JPanel();
JButton availabilityButton = new JButton("Check");
JLabel availabityLabel = new myLabel("Check Availability");
JLabel availabityLabelName = new JLabel("Enter product name");
myTextArea availabilityProduct = new myTextArea();
availabilityProduct.setColumns(20);
availabilityPanel.add(availabityLabel);
availabilityPanel.add(new JLabel(" "));
availabilityPanel.add(availabityLabelName);
availabilityPanel.add(availabilityProduct);
availabilityPanel.add(new JLabel(" "));
availabilityPanel.add(availabilityButton);
availabilityPanel.setLayout(new GridLayout(6,1));
availabilityButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String stock = availabilityProduct.getText().trim();
new availabilityPanel(stock, isAvailable(stock));
availabilityProduct.clear();
}
});
JPanel updatePanel = new JPanel();
JLabel updateLabel = new myLabel("Update Stocks ");
JLabel updateLabelName = new JLabel("Enter stock name ");
JLabel updateLabelQuantity = new JLabel("Enter quantity ");
myTextArea updateProduct = new myTextArea();
myTextArea updateQuantity = new myTextArea();
JButton updateButton = new JButton("Update Stocks ");
updatePanel.add(updateLabel);
updatePanel.add(updateLabelName);
updatePanel.add(updateProduct);
updatePanel.add(updateLabelQuantity);
updatePanel.add(updateQuantity);
updatePanel.add(updateButton);
updatePanel.setLayout(new GridLayout(6,1));
JFrame frame = this;
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String productName = updateProduct.get();
String quantity = updateQuantity.get();
System.out.println(quantity);
int q = Integer.parseInt(quantity);
insertProduct(productName, q);
updateProduct.clear();
updateQuantity.clear();
new StockManager();
frame.setVisible(false);
}
});
JPanel headerPanel = new JPanel();
headerPanel.add(availabilityPanel);
headerPanel.add(updatePanel);
headerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 20));
add(headerPanel);
JPanel bodyPanel = new JPanel();
JButton orderButton = new JButton("Order");
myTextArea orderProduct = new myTextArea();
JLabel orderProductLabel = new myLabel("Order Product");
myTextArea orderQuantity = new myTextArea();
JPanel tempPanel = new JPanel();
tempPanel.add(orderProductLabel);
tempPanel.add(new JLabel("Enter name of product"));
tempPanel.add(orderProduct);
tempPanel.add(new JLabel("Enter quantity of product"));
tempPanel.add(orderQuantity);
tempPanel.add(orderButton);
tempPanel.setLayout(new GridLayout(6,1));
bodyPanel.add(tempPanel);
headerPanel.add(bodyPanel);
orderButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String product = orderProduct.get();
orderProduct.clear();
String quantity = orderQuantity.get();
orderQuantity.clear();
orderProduct(product, Integer.parseInt(quantity));
new StockManager();
frame.setVisible(false);
}
});
JTable table;
ResultSet rs = null;
try {
rs = getProducts();
ResultSetMetaData metadata = rs.getMetaData();
int numberOfColumns = metadata.getColumnCount();
int numberOfRows = 30;
Object[][] resultSet = new Object[numberOfRows][numberOfColumns];
int row = 0;
while (rs.next()) {
for (int i = 0; i < numberOfColumns; i++) {
resultSet[row][i] = rs.getObject(i+1);
}
row++;
}
String[] heading = new String[] {"Product", "Quantity in stock"};
table = new JTable(resultSet, heading);
JScrollPane jsp = new JScrollPane(table);
add(jsp);
}
catch(Exception ex) {
System.out.println(ex);
}
setLayout(new GridLayout(2,2));
setSize(1100, 650);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static int isAvailable(String stockName) {
try {
int n = 0;
Connection con = DriverManager.getConnection("jdbc:derby:stocks;create=true");
PreparedStatement ps = con.prepareStatement("Select quantity from stocks where name = ?");
ps.setString(1, stockName);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
n = rs.getInt(1);
}
con.close();
return n;
}
catch(Exception ex) {
System.out.println("error in available fun");
return -1;
}
}
public static void orderProduct(String product, int n) {
try {
int stock = isAvailable(product);
if(stock == -1 || stock == 0) {
new availabilityPanel(product, 0);
}
else {
Connection con = DriverManager.getConnection("jdbc:derby:stocks;create=true");
PreparedStatement ps = con.prepareStatement("update stocks set quantity = ? where name = ?");
ps.setString(2, product);
ps.setInt(1, stock - n);
ps.executeUpdate();
con.close();
}
}
catch(Exception ex) {
System.out.println(ex);
}
}
public static void insertProduct(String product, int stock){
try {
int n = isAvailable(product);
if(n == -1 || n == 0) {
Connection con = DriverManager.getConnection("jdbc:derby:stocks;create=true");
PreparedStatement ps = con.prepareStatement("insert into stocks values (?, ?)");
ps.setString(1, product);
ps.setInt(2, stock);
ps.executeUpdate();
con.close();
}
else {
Connection con = DriverManager.getConnection("jdbc:derby:stocks;create=true");
PreparedStatement ps = con.prepareStatement("update stocks set quantity = ? where name = ?");
ps.setString(2, product);
ps.setInt(1, stock + n);
ps.executeUpdate();
con.close();
}
}
catch(Exception ex) {
System.out.println(ex);
}
}
public static ResultSet getProducts() throws SQLException{
Connection con = DriverManager.getConnection("jdbc:derby:stocks;create=true");
//PreparedStatement ps = con.prepareStatement("select * from stocks where quantity <= 500"); // <= denotes 'lesser than'
PreparedStatement ps = con.prepareStatement("select * from stocks order by name");
ResultSet rs = ps.executeQuery();
return rs;
}
public static void removeEmptyProduct() throws SQLException{
Connection con = DriverManager.getConnection("jdbc:derby:stocks;create=true");
Statement ps = con.createStatement();
ps.executeQuery("delete from stocks where name = '' or name is null");
}
public static void main(String args[]) {
new StockManager();
}
}