-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryTransaction.java
More file actions
271 lines (244 loc) · 5.26 KB
/
InventoryTransaction.java
File metadata and controls
271 lines (244 loc) · 5.26 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class InventoryTransaction extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4,l5,l6;
JTextField t1,t3,t2;
JButton b1,b2,b3;
JPanel p1,p2,p3;
JList<String> lt1;
JComboBox<Object> c1;
Container c;
ButtonGroup bg;
JRadioButton rb1,rb2;
Connection cnn=null;
PreparedStatement ps;
int item_code;
String cnstring,sqli,sqlu,sql;
int qoh; String type,dept;
void connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
cnstring="jdbc:mysql://localhost/invent";
cnn=DriverManager.getConnection(cnstring,"jav160","mice");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in connection"+e.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
void listControl()
{
try
{
connect();
sql="select item_code from invent_mast";
ps=cnn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
//c1.removeAllItems();
while(rs.next())
{
c1.addItem(rs.getObject(1));
}
rs.close();
ps.close();
cnn.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in loading values to the combo box"+e.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String args[])
{
InventoryTransaction a1=new InventoryTransaction();
}
public InventoryTransaction()
{
setTitle("Data Entry Inventory Transaction");
c=getContentPane();
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
p1=new JPanel();
p1.setLayout(new GridLayout(6,2,5,5));
l1=new JLabel("Item Code");
l2=new JLabel("Description");
l3=new JLabel("Transaction Type");
l4=new JLabel("Department Code ");
l5=new JLabel("Transaction Quantity");
l6=new JLabel("Transaction Date");
c1=new JComboBox<Object>();
listControl();
c1.addActionListener(this);
t1=new JTextField(20);
t1.setEditable(false);
p2=new JPanel();
p2.setLayout(new GridLayout(2,1,5,5));
bg=new ButtonGroup();
rb1=new JRadioButton("Issue");
rb2=new JRadioButton("Receipt");
bg.add(rb1); bg.add(rb2);
rb1.addActionListener(this);
rb2.addActionListener(this);
p2.add(rb1);
p2.add(rb2);
lt1=new JList<String>(new String[]{"X1","X2","Y1","Y2","Z1","Z2"});
t2=new JTextField(20);
t2.addActionListener(this);
t3=new JTextField(20);
p1.add(l1);
p1.add(c1);
p1.add(l2);
p1.add(t1);
p1.add(l3);
p1.add(p2);
p1.add(l4);
p1.add(new JScrollPane(lt1));
p1.add(l5);
p1.add(t2);
p1.add(l6);
p1.add(t3);
c.add(p1);
p3=new JPanel();
p3.setLayout(new GridLayout(1,3,5,5));
b1=new JButton("Insert");
b2=new JButton("Clear");
b3=new JButton("Close");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
p3.add(b1);
p3.add(b2);
p3.add(b3);
c.add(p3);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
insert();
if(e.getSource()==b2)
clear();
if(e.getSource()==b3)
close();
if(e.getSource()==c1)
checkItem();
if(e.getSource()==rb1)
checkIssue();
if(e.getSource()==rb2)
checkReceipt();
if(e.getSource()==t2)
checkQty();
}
void checkQty()
{
int q=Integer.parseInt(t2.getText());
if(q<=0)
{
JOptionPane.showMessageDialog(null,"Transaction quantity must be greater than zero","Message",JOptionPane.INFORMATION_MESSAGE);
t2.setText("");
t2.requestFocus();
}
else if(type.equals("I") && q>qoh)
{
JOptionPane.showMessageDialog(null,"Transaction quantity must not be greater than "+qoh,"Message",JOptionPane.INFORMATION_MESSAGE);
t2.setText("");
t2.requestFocus();
}
else
t3.requestFocus();
}
void checkIssue()
{
type="I";
lt1.setEnabled(true);
}
void checkReceipt()
{
type="R";
lt1.setEnabled(false);
}
void checkItem()
{
try
{
int item=Integer.parseInt(c1.getSelectedItem().toString());
connect();
sql="select descr,qty_onhand from invent_mast where item_code=?";
ps=cnn.prepareStatement(sql);
ps.setInt(1,item);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
t1.setText(rs.getString(1));
qoh=rs.getInt(2);
}
rs.close();
ps.close();
cnn.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in selecting values from the combo box"+e.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
}
}
void insert()
{
try
{
connect();
int item=Integer.parseInt(c1.getSelectedItem().toString());
int qty=Integer.parseInt(t2.getText());
String dt=t3.getText();
if(type.equals("I"))
{
dept=lt1.getSelectedValue().toString();
qoh=qoh-qty;
}
else
{
dept="";
qoh=qoh+qty;
}
sqli="insert into invent_transact (ITEM_CODE,TRAN_TYPE,DEPT_CODE,TRAN_QTY,TRAN_DATE)Values(?,?,?,?,?)";
ps=cnn.prepareStatement(sqli);
ps.setInt(1,item);
ps.setString(2,type);
ps.setString(3,dept);
ps.setInt(4,qty);
ps.setString(5,dt);
int tot=ps.executeUpdate();
JOptionPane.showMessageDialog(null,"Row inserted to Transaction table","Message",JOptionPane.INFORMATION_MESSAGE);
sqlu="update invent_mast set qty_onhand=? where item_code=?";
ps=cnn.prepareStatement(sqlu);
ps.setInt(1,qoh);
ps.setInt(2,item);
tot=ps.executeUpdate();
JOptionPane.showMessageDialog(null,"Row updated to Master table","Message",JOptionPane.INFORMATION_MESSAGE);
ps.close();
cnn.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Error in inserting or updating to the Transaction table "+e.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
}
}
void clear()
{
t1.setText("");
lt1.clearSelection();
bg.clearSelection();
t2.setText("");
t3.setText("");
}
void close()
{
setVisible(false);
}
}