-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlloc.java
210 lines (186 loc) · 7.22 KB
/
Alloc.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
/*
*
* Project Name: SMART BUS MANAGEMENT
* Author List: SIDDHARTH SHRIVASTAVA, RAJESHWARI KAUSHAL
* Filename: Alloc.java
* Functions: init(),processRequest()
* Global Variables:
*
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import connection.ConnectionDB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
/**
*
* @author SIDDHARTH
*/
public class Alloc extends HttpServlet {
Connection con=null;
@Override
public void init()
{
try{
con =ConnectionDB.giveConnection();
//JOptionPane.showMessageDialog(null,"1");
// System.out.println("1");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,ex);
}
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
int data_cnt=0;
res.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = res.getWriter()) {
/* TODO output your page here. You may use following sample code. */
try{
Statement st= con.createStatement();
// System.out.println("1");
ResultSet rs=st.executeQuery("Select bus_no,bus_seats,avail_seats from bus_sched where avail_seats !=0");
//System.out.println("2");
ArrayList <Integer> avail= new ArrayList<>();
Map<Integer, Integer> bus = new HashMap<Integer, Integer>();
while(rs.next())
{
avail.add(rs.getInt(3));
// System.out.println("3");
data_cnt++;
bus.put(rs.getInt(1), rs.getInt(2));
}
if(data_cnt==0){
JOptionPane.showMessageDialog(null,"No Bus data Found");
res.sendRedirect("alloc_dealloc.jsp");
}
JOptionPane.showMessageDialog(null,avail);
//System.out.println("4");
rs=st.executeQuery("Select count(challan),route from reg_stud where book=1 group by route");
//System.out.println("5");
int scnt,min,dif,bs;
while(rs.next())
{
scnt=Integer.parseInt(rs.getString(1));
min=50;
dif=0;
bs=0;
// System.out.println("6");
data_cnt++;
int i=1;
//System.out.println("7");
for(;i<=4;i++)
{
if(scnt>0)
{
if(scnt<avail.get(i-1)+2)
{
// System.out.println("9");
// System.out.println("99");
dif=avail.get(i-1)-scnt+2;
// System.out.println("8");
if(min>dif&&dif>=0)
{
// System.out.println("10");
min=dif;
bs=i;
}
}
}
}
avail.remove(bs-1);
avail.add(bs-1, bus.get(bs)-scnt);
//System.out.println("16");
Statement st1=con.createStatement();
st1.executeUpdate("update bus_sched set bus_route='"+rs.getString(2)+"'where bus_no="+bs);
//System.out.println("17");
st1.executeUpdate("update reg_stud set bus='"+bs+"'where book=1 and route='"+rs.getString(2)+"'");
//System.out.println("18");
st1.executeUpdate("update bus_sched set avail_seats="+avail.get(bs-1)+" where bus_no="+bs);
// System.out.println("19");
}
if(data_cnt==0){
JOptionPane.showMessageDialog(null,"No Bus data Found");
res.sendRedirect("alloc_dealloc.jsp");
}
int i=0;
// System.out.println("20");
Statement st1=con.createStatement();
Statement st2=con.createStatement();
ResultSet rs2=st1.executeQuery("select bus,enroll,route from reg_stud where book=1");
//System.out.println("21");
while(rs2.next())
{
i++;
// System.out.println("22");
st2.executeUpdate("insert into bus_det values("+rs2.getInt(1)+",'"+rs2.getString(2)+"','"+rs2.getString(3)+"')");
JOptionPane.showMessageDialog(null," "+i);
// System.out.println("23");
}
res.sendRedirect("alloc_dealloc.jsp");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"alloc"+ex);
res.sendRedirect("alloc_dealloc.jsp");
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}