Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.13 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ItemEvent;
  7. import java.awt.event.ItemListener;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. import javax.swing.ButtonGroup;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JLabel;
  20. import javax.swing.JOptionPane;
  21. import javax.swing.JPanel;
  22. import javax.swing.JRadioButton;
  23.  
  24. public class BookRoom extends JFrame{
  25. //for database storing values
  26. static String room="";
  27. static int roomd=123;
  28. static String bck;
  29.  
  30. String label2;
  31. BookRoom (){
  32.  
  33. JLabel headerLabel;
  34. JLabel statusLabel;
  35. JPanel pan1=new JPanel(new GridLayout(4,4));
  36. JPanel pan=new JPanel(new GridLayout(9,9));
  37. pan.setBounds(0,400,1300,250);
  38. pan1.setBounds(20,160,600,150);
  39. headerLabel = new JLabel("", JLabel.CENTER);
  40. statusLabel = new JLabel("",JLabel.CENTER);
  41.  
  42.  
  43. //radio button for Seater selection
  44. final JRadioButton oneseater = new JRadioButton("ONE SEATER");
  45. final JRadioButton twoseater = new JRadioButton("TWO SEATER");
  46. final JRadioButton threeseater = new JRadioButton("THREE SEATER");
  47. final JRadioButton fourseater = new JRadioButton("FOUR SEATER");
  48. ButtonGroup bG = new ButtonGroup();
  49. bG.add(oneseater);
  50. bG.add(twoseater);
  51. bG.add(threeseater);
  52. bG.add(fourseater);
  53.  
  54. //status label
  55. statusLabel.setBounds(620, 60, 500, 200);
  56. statusLabel.setText("<html><span style='font-size:25px;color:BLUE'>"+"CHOOSE ROOM AND BLOCK"+"</span></html>");
  57. //block selection
  58. final JRadioButton A = new JRadioButton("Block A");
  59. final JRadioButton B = new JRadioButton("Block B");
  60.  
  61. ButtonGroup r1 = new ButtonGroup();
  62. r1.add(A);
  63. r1.add(B);
  64.  
  65. headerLabel.setText("<html><span style='font-size:30px;color:RED'>"+"WELCOME TO HOSTEL BOOKING SERVICE"+"</span></html>");
  66. headerLabel.setBounds(170, 10, 900, 80);
  67.  
  68. JLabel regnol=new JLabel("<html><span style='font-size:14px'>"+"Reg no : "+"</span></html>");
  69. regnol.setFont(new Font("SansSerif", Font.BOLD, 16));
  70. regnol.setBounds(0,100, 100, 30);
  71.  
  72.  
  73. JLabel uregnol=new JLabel("<html><span style='font-size:14px'>"+StudentInfo.reg+"</span></html>");
  74. uregnol.setFont(new Font("SansSerif", Font.BOLD, 16));
  75. uregnol.setBounds(100,100, 200, 30);
  76.  
  77.  
  78. JLabel name = new JLabel("<html><span style='font-size:14px'>"+"Name : "+"</span></html>");
  79. name.setBounds(350, 100, 100, 30);
  80.  
  81. JLabel uname = new JLabel("<html><span style='font-size:14px'>"+StudentInfo.name1+"</span></html>");
  82. uname.setBounds(450, 100, 200, 30);
  83.  
  84. JLabel Room = new JLabel("<html><span style='font-size:25px;color:BLUE'>"+"CHOOSE ROOM:"+"</span></html>");
  85. Room.setBounds(20, 350, 300, 30);
  86.  
  87. //for submit button
  88. JButton jb1 = new JButton("<html><span style='font-size:15px;color:GREEN'>"+"BOOK NOW"+"</span></html>");
  89. jb1.setBounds(520, 350, 200, 40);
  90. jb1.setBackground(Color.white);
  91. JButton jb2 = new JButton("<html><span style='font-size:15px;color:RED'>"+"CHECK AVAILIBLITY"+"</span></html>");
  92. jb2.setBounds(720, 350, 400, 40);
  93. jb2.setBackground(Color.white);
  94.  
  95.  
  96.  
  97. add(jb2);
  98. add(jb1);
  99. add(Room);
  100. add(uname);
  101. add(uregnol);
  102. add(name);
  103. add(regnol);
  104. add(pan);
  105. jb1.addActionListener(new ActionListener(){
  106.  
  107. public void actionPerformed(ActionEvent actionEvent) {
  108. {
  109. int flag=0,flag1=0;
  110. if( ( !(A.isSelected() ) && ( !(B.isSelected() ) ) ) )
  111. {
  112. flag=1;flag1=1;
  113. JOptionPane.showMessageDialog(null,"PLEASE SELECT A BLOCK");
  114. }
  115. if( (!(oneseater.isSelected() ) && ( !(twoseater.isSelected()))&& (!(threeseater.isSelected() ) )&& ( !(fourseater.isSelected()) ) ))
  116. {
  117. flag=1;flag1=1;
  118. JOptionPane.showMessageDialog(null,"PLEASE GIVE A PREFRENCE ABOUT THE ROOM ex.ONE SEATER");
  119. }
  120.  
  121. if(flag==0&&flag1==0){
  122. int nreg=Integer.parseInt(StudentInfo.reg);
  123. try{
  124.  
  125. Class.forName("com.mysql.jdbc.Driver");
  126. System.out.println("Driver Loaded");
  127. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/nj", "root", "123456");
  128. System.out.println("Connection made");
  129.  
  130. PreparedStatement pstat = con.prepareStatement("insert into bhh1 values(?,?,?,?)");
  131.  
  132.  
  133.  
  134. System.out.println(BookRoom.roomd);
  135. pstat.setInt(1,nreg);
  136. pstat.setString(2,StudentInfo.name1);
  137. pstat.setInt(3,BookRoom.roomd);
  138. pstat.setString(4,A.isSelected()?"A":"B");
  139. pstat.executeUpdate();
  140. JOptionPane.showMessageDialog(null,"YOU HAVE BOOKED ROOM NO:"+room);
  141. int aaa=1;
  142. con.close();
  143. if(aaa==1){
  144. new EndingPage();
  145. dispose();
  146. }
  147. }
  148.  
  149. catch(SQLException e){
  150. JOptionPane.showMessageDialog(null,room+":IS BOOKED PLEASE SELECT OTHER ROOM") ;
  151. }
  152. catch(Exception e){
  153. e.printStackTrace();
  154. }
  155.  
  156.  
  157.  
  158. }
  159. }
  160.  
  161.  
  162. }
  163. });
  164. //start
  165.  
  166. jb2.addActionListener(new ActionListener(){
  167.  
  168. public void actionPerformed(ActionEvent actionEvent) {
  169. {
  170. int flag=0,flag1=0;
  171. bck=A.isSelected()?" A":" B";
  172. if( ( !(A.isSelected() ) && ( !(B.isSelected() ) ) ) )
  173. {
  174. flag=1;flag1=1;
  175. JOptionPane.showMessageDialog(null,"PLEASE SELECT A BLOCK");
  176. }
  177. if( (!(oneseater.isSelected() ) && ( !(twoseater.isSelected()))&& (!(threeseater.isSelected() ) )&& ( !(fourseater.isSelected()) ) ))
  178. {
  179. flag=1;flag1=1;
  180. JOptionPane.showMessageDialog(null,"PLEASE GIVE A PREFRENCE ABOUT THE ROOM ex.ONE SEATER");
  181. }
  182. if(BookRoom.roomd=='\0'){
  183. flag=1;flag1=1;
  184. JOptionPane.showMessageDialog(null,"PLEASE SELECT THE ROOM AND BLOCK");
  185.  
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. if(flag==0&&flag1==0){
  193. int nreg=Integer.parseInt(StudentInfo.reg);
  194. try{
  195.  
  196. Class.forName("com.mysql.jdbc.Driver");
  197. System.out.println("Driver Loaded");
  198. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/nj", "root", "123456");
  199. System.out.println("Connection made");
  200.  
  201. Statement stat1 = con.createStatement();
  202.  
  203.  
  204.  
  205. ResultSet rs = stat1.executeQuery("select *from bhh1 where room=\'"+BookRoom.roomd+"\'"+" and block=\'"+(A.isSelected()?"A":"B")+"\'");
  206. if( !rs.next()){
  207. System.out.println("Room is avaliabel");
  208. JOptionPane.showMessageDialog(null,"ROOM is FREE SMASH BOOK NOW");
  209.  
  210. }
  211. else{
  212. String spp="MR."+rs.getString("name")+" ";
  213. JOptionPane.showMessageDialog(null,spp+"IS ALREADY BOOKED THIS ROOM");
  214.  
  215. }
  216.  
  217. // JOptionPane.showMessageDialog(null,"ROOM is FREE SMASH BOOK NOW");
  218.  
  219. }
  220. catch(SQLException e){
  221. e.printStackTrace();
  222. //JOptionPane.showMessageDialog(null,room+" IS BOOKED PLEASE SELECT OTHER ROOM") ;
  223.  
  224. }
  225. catch(Exception e){
  226. e.printStackTrace();
  227. }
  228.  
  229.  
  230.  
  231. }
  232. }
  233.  
  234.  
  235. }
  236. });
  237.  
  238.  
  239. JButton a[]=new JButton[120];
  240. int k=0;
  241.  
  242. ActionListener actionListener = new ActionListener()
  243. {
  244. public void actionPerformed(ActionEvent actionEvent) {
  245.  
  246. JButton button = (JButton)actionEvent.getSource();
  247.  
  248. button.setForeground(Color.RED);
  249. label2 = button.getText();
  250. room=label2.substring(5);
  251. roomd=Integer.parseInt((button.getText()).substring(6, 9));
  252. String v=String.valueOf(room);
  253.  
  254. JOptionPane.showMessageDialog(null,"ROOM SELECTED:"+room);
  255. statusLabel.setText("<html><span style='font-size:25px;color:ORANGE'>"+"ROOM SELECTED:"+room+"</span></html>");
  256. }
  257. };
  258.  
  259.  
  260. for(int i=1;i<10;i++){
  261. for(int j=1;j<=12;j++){
  262. int t=i*100+j;
  263. a[k]=new JButton("ROOM: "+t);
  264. a[k].setForeground(Color.BLUE);
  265. pan.add(a[k]);
  266. a[k].addActionListener(actionListener);
  267. k++;
  268. }
  269. }
  270.  
  271.  
  272. ItemListener itemListener = new ItemListener()
  273. {
  274. public void itemStateChanged(ItemEvent e) {
  275.  
  276. if(oneseater.isSelected())
  277. statusLabel.setText("<html><span style='font-size:25px;color:ORANGE'>"+"SELECT ROOM NO:FROM 101 TO 212 for 1-SEATER"+"</span></html>");
  278. if(twoseater.isSelected())
  279. statusLabel.setText("<html><span style='font-size:25px;color:GREEN'>"+"SELECT ROOM NO:FROM 301 TO 412 for 2-SEATER"+"</span></html>");
  280. if(threeseater.isSelected())
  281. statusLabel.setText("<html><span style='font-size:25px;color:PINK'>"+"SELECT ROOM NO:FROM 501 TO 612 for 3-SEATER"+"</span></html>");
  282. if(fourseater.isSelected())
  283. statusLabel.setText("<html><span style='font-size:25px;color:BLUE'>"+"SELECT ROOM NO:FROM 701 TO 912 for 4-SEATER"+"</span></html>");
  284. }
  285. };
  286.  
  287. oneseater.addItemListener(itemListener);
  288. twoseater.addItemListener(itemListener);
  289. threeseater.addItemListener(itemListener);
  290. fourseater.addItemListener(itemListener);
  291. pan1.add(oneseater);
  292. pan1.add(twoseater);
  293. pan1.add(threeseater);
  294. pan1.add(fourseater);
  295. pan1.add(A);
  296. pan1.add(B);
  297. add(headerLabel);
  298. add(statusLabel);
  299. //f1.setBounds(x, y, width, height)
  300.  
  301. add(pan1);
  302. setSize(1600,1600);
  303. setLayout(null);
  304. add(statusLabel);
  305. setVisible(true);
  306. }
  307. /*
  308.  
  309. public static void main(String afff[]){
  310. new BookRoom();
  311. }
  312. */
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement