/
BusInfoD.java
202 lines (180 loc) · 5.17 KB
/
BusInfoD.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
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class BusInfoD extends JFrame implements ActionListener
{
JLabel busid,model,price,quentity,route;
JTextField busidText,modelText,priceText,routeText,quentityText;
JButton logout,back,refresh,load,borrow;
JPanel panel;
String UserId;
public BusInfoD(String UserId)
{
super(" Bus Information ");
this.UserId=UserId;
this.setSize(800, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel =new JPanel();
panel.setLayout(null);
panel.setBackground(Color.white);
busid=new JLabel(" ID : ");
busid.setBounds(100,130,100,50);
panel.add(busid);
busidText=new JTextField();
busidText.setBounds(230,130,200,50);
panel.add(busidText);
load=new JButton("Search");
load.setBounds(450,130,200,50);
load.setBackground(Color.yellow);
load.addActionListener(this);
panel.add(load);
borrow=new JButton("Borrow");
borrow.setBounds(500,220,200,50);
borrow.setBackground(Color.green);
borrow.addActionListener(this);
borrow.setEnabled(false);
panel.add(borrow);
model=new JLabel(" MODEL : ");
model.setBounds(100,190,100,50);
panel.add(model);
modelText=new JTextField();
modelText.setBounds(230,190,200,50);
panel.add(modelText);
price=new JLabel("Price :");
price.setBounds(100,250,100,50);
panel.add(price);
priceText=new JTextField();
priceText.setBounds(280,250,150,50);
panel.add(priceText);
quentity=new JLabel("QUANTITY :");
quentity.setBounds(100,310,100,50);
panel.add(quentity);
quentityText = new JTextField();
quentityText.setBounds(230,310,200,50);
panel.add(quentityText);
route=new JLabel("ROUTE :");
route.setBounds(100,370,100,50);
panel.add(route);
routeText=new JTextField();
routeText.setBounds(230,370,200,50);
panel.add(routeText);
logout=new JButton("Log Out");
logout.setBounds(500,10,250,50);
logout.setBackground(Color.red);
logout.addActionListener(this);
panel.add(logout);
back=new JButton("Back");
back.setBounds(500,380,250,50);
back.setBackground(Color.blue);
back.setForeground(Color.white);
back.addActionListener(this);
panel.add(back);
refresh = new JButton("Refresh");
refresh.setBounds(120,10,250,50);
refresh.setBackground(Color.orange);
refresh.addActionListener(this);
panel.add(refresh);
this.add(panel);
}
public void actionPerformed(ActionEvent ae)
{
String ac=ae.getActionCommand();
if(ac.equals(logout.getText()))
{
LogIn ln=new LogIn();
ln.setVisible(true);
this.setVisible(false);
}
if(ac.equals(refresh.getText()))
{
busidText.setEnabled(true);
busidText.setText("");
modelText.setText("");
priceText.setText("");
quentityText.setText("");
routeText.setText("");
}
else if(ac.equals(back.getText()))
{
DriverHome ed=new DriverHome(UserId);
ed.setVisible(true);
this.setVisible(false);
}
else if(ac.equals(load.getText()))
{
busSearch();
}
else if(ac.equals(borrow.getText()))
{
borrowBus();
}
}
public void busSearch()
{
String search = busidText.getText();
String query = "SELECT `Model`,`Price`,`Quantity`,`Route` FROM `bus` WHERE `BusId`='"+search+"';";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
Statement st= con.createStatement();
ResultSet rs= st.executeQuery(query);
boolean flag=false;
while(rs.next())
{
modelText.setText(rs.getString("Model"));
priceText.setText(rs.getString("Price"));
quentityText.setText(rs.getString("Quantity"));
routeText.setText(rs.getString("Route"));
flag=true;
}
if(Integer.parseInt(quentityText.getText())>0)
{
borrow.setEnabled(true);
}
if(!flag)
{
modelText.setText("");
priceText.setText("");
routeText.setText("");
quentityText.setText("");
JOptionPane.showMessageDialog(this,"Invalid ID");
}
rs.close();
st.close();
con.close();
}
catch(Exception ex)
{
System.out.println("Exception : " +ex.getMessage());
}
}
public void borrowBus()
{
String newId= busidText.getText();
int newquentity=Integer.parseInt(quentityText.getText());
System.out.println(newquentity);
newquentity=newquentity-1;
String query = "UPDATE bus SET Quantity = '"+newquentity+"'WHERE BusId='"+newId+"'";
System.out.println(newquentity);
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
Statement st=con.createStatement();
st.executeUpdate(query);
st.close();
con.close();
JOptionPane.showMessageDialog(this, "Borrow Done");
borrow.setEnabled(false);
quentityText.setText(""+newquentity);
}
catch(Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(this, "Update Fail");
}
}
}