Guest User

Untitled

a guest
Sep 11th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. import java.net.*;
  2. import java.util.Scanner;
  3. import java.awt.*;
  4. import java.io.*;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JButton;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14. import javax.swing.JTextField;
  15. import javax.swing.JPasswordField;
  16.  
  17. public class TicketBooking extends JFrame{
  18.  
  19. /**
  20. *
  21. */
  22. private static final long serialVersionUID = 1L;
  23. //private JFrame frame;
  24. private JPanel contentPane;
  25. private JTextField textFieldusr;
  26. private JPasswordField passwordField;
  27. private JLabel lblPassword;
  28. private JLabel lblUsername;
  29. private JLabel lblWelcomeToBus;
  30. private JButton btnAdmin;
  31. private JButton btnUser;
  32. public static DataOutputStream dos = null;
  33. public static ObjectOutputStream oos = null;
  34. public static DataInputStream dis = null;
  35. public static ObjectInputStream ois = null;
  36.  
  37. /**
  38. * Launch the application.
  39. */
  40. public static void main(String[] args) {
  41. EventQueue.invokeLater(new Runnable() {
  42. public void run() {
  43. try {
  44.  
  45. TicketBooking frame = new TicketBooking();
  46. frame.setVisible(true);
  47.  
  48. Socket s = new Socket("localhost",3000);
  49.  
  50. System.out.println("nCONNECTED TO SERVERn");
  51.  
  52. //DataInputStream dis = new DataInputStream(s.getInputStream());
  53. TicketBooking.dos = new DataOutputStream(s.getOutputStream());
  54.  
  55. TicketBooking.oos = new ObjectOutputStream(s.getOutputStream());
  56.  
  57. TicketBooking.dis = new DataInputStream(s.getInputStream());
  58.  
  59. TicketBooking.ois = new ObjectInputStream(s.getInputStream()); //This statement is causing the infinite loop
  60.  
  61.  
  62.  
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. });
  68. }
  69.  
  70. /**
  71. * Create the application.
  72. */
  73. public TicketBooking() {
  74. super("Bus Ticket Booking System");
  75. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76.  
  77. setBounds(200,200,450,450);
  78. contentPane = new JPanel();
  79. contentPane.setForeground(Color.BLACK);
  80. contentPane.setBackground(Color.DARK_GRAY);
  81. //frame.setBounds(200, 200, 450, 450);
  82. contentPane.setBorder(new EmptyBorder(5,5,5,5));
  83. setContentPane(contentPane);
  84. contentPane.setLayout(null);
  85.  
  86. lblWelcomeToBus = new JLabel("Welcome to Bus Ticket Booking System");
  87. lblWelcomeToBus.setFont(new Font("Dialog", Font.BOLD, 18));
  88. lblWelcomeToBus.setForeground(Color.WHITE);
  89. lblWelcomeToBus.setBounds(12, 37, 415, 39);
  90. contentPane.add(lblWelcomeToBus);
  91.  
  92. btnAdmin = new JButton("Admin");
  93. btnAdmin.addActionListener(new ActionListener() {
  94. public void actionPerformed(ActionEvent arg0) {
  95. //JOptionPane.showMessageDialog(null,"Admin logged in");
  96. if(textFieldusr.getText().equals("Admin") && new String(passwordField.getPassword()).equals("1234")) {
  97. Adminframe obj = new Adminframe();
  98. obj.setVisible(true);
  99. try {
  100. TicketBooking.dos.writeInt(1);
  101. } catch (IOException e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105. }
  106. else {
  107. JOptionPane.showMessageDialog(null,"Wrong username or password");
  108. }
  109. }
  110. });
  111. btnAdmin.setBounds(57, 337, 117, 25);
  112. contentPane.add(btnAdmin);
  113.  
  114. btnUser = new JButton("User");
  115. btnUser.addActionListener(new ActionListener() {
  116. public void actionPerformed(ActionEvent e) {
  117. //JOptionPane.showMessageDialog(null,"User logged in");
  118. if(textFieldusr.getText().equals("User") && new String(passwordField.getPassword()).equals("1234")) {
  119. UserFrame obj = new UserFrame();
  120. obj.setVisible(true);
  121. try {
  122. TicketBooking.dos.writeInt(2);
  123. } catch (IOException e1) {
  124. // TODO Auto-generated catch block
  125. e1.printStackTrace();
  126. }
  127. }
  128. else {
  129. JOptionPane.showMessageDialog(null,"Wrong username or password");
  130. }
  131. }
  132. });
  133. btnUser.setBounds(274, 337, 117, 25);
  134. contentPane.add(btnUser);
  135.  
  136. lblUsername = new JLabel("Username");
  137. lblUsername.setFont(new Font("Dialog", Font.BOLD, 15));
  138. lblUsername.setForeground(Color.WHITE);
  139. lblUsername.setBounds(57, 157, 105, 30);
  140. contentPane.add(lblUsername);
  141.  
  142. textFieldusr = new JTextField();
  143. textFieldusr.setBounds(260, 158, 131, 30);
  144. contentPane.add(textFieldusr);
  145. textFieldusr.setColumns(10);
  146.  
  147. lblPassword = new JLabel("Password");
  148. lblPassword.setFont(new Font("Dialog", Font.BOLD, 15));
  149. lblPassword.setForeground(Color.WHITE);
  150. lblPassword.setBounds(57, 232, 105, 30);
  151. contentPane.add(lblPassword);
  152.  
  153. passwordField = new JPasswordField();
  154. passwordField.setBounds(260, 233, 131, 30);
  155. contentPane.add(passwordField);
  156. }
  157. }
  158.  
  159. import java.net.*;
  160. import java.io.*;
  161.  
  162. class BusTicketSystem {
  163. public static void main(String[] args) throws IOException
  164. {
  165. //Booking bk = new Booking();
  166.  
  167. //bk.getdata();
  168.  
  169. MySQLJDBC mysqljdbc = new MySQLJDBC();
  170. //mysqljdbc.read();
  171.  
  172. ServerSocket ss = new ServerSocket(3000);
  173.  
  174. // running infinite loop for getting
  175. // client request
  176. while (true)
  177. {
  178. Socket s = null;
  179.  
  180. try
  181. {
  182. // socket object to receive incoming client requests
  183. s = ss.accept();
  184.  
  185. System.out.println("A new client is connected : " + s);
  186.  
  187. // obtaining input and out streams
  188. ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
  189. DataInputStream dis = new DataInputStream(s.getInputStream());
  190. //DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  191.  
  192. System.out.println("Assigning new thread for this client");
  193.  
  194. // create a new thread object
  195. Thread t = new ClientHandler(s,dis,ois);
  196.  
  197. // Invoking the start() method
  198. t.start();
  199. }
  200. catch (Exception e){
  201. s.close();
  202. e.printStackTrace();
  203. }
  204. }
  205. }
  206. }
  207.  
  208. class ClientHandler extends Thread {
  209. final ObjectInputStream ois;
  210. final DataInputStream dis;
  211. //final DataOutputStream dos;
  212.  
  213. final Socket s;
  214. // Constructor
  215. public ClientHandler(Socket s, DataInputStream dis, ObjectInputStream ois)
  216. {
  217. this.s = s;
  218. this.dis = dis;
  219. this.ois = ois;
  220. //this.dos = dos;
  221. }
  222.  
  223.  
  224. @Override
  225. public void run() {
  226.  
  227. Booking bk = new Booking();
  228.  
  229. try {
  230. //Scanner sc = new Scanner(System.in);
  231.  
  232. //String temp;
  233.  
  234. System.out.println("Customer connected");
  235.  
  236. while(true) {
  237. System.out.println("In while");
  238. int ch = dis.readInt();
  239.  
  240. //dos.writeUTF("Press 1 to login as admin");
  241. //dos.writeUTF("Press 2 to login as usernEnter");
  242. //dos.writeUTF("Press 3 to exit n Enter");
  243. //temp = dis.readUTF();
  244. //int ch = Integer.parseInt(temp);
  245. if(ch == 1) {
  246. //dos.writeUTF("Press 1 to add bus");
  247. //dos.writeUTF("Press 2 to cancel bus");
  248. //dos.writeUTF("Press 3 to see buses");
  249. //dos.writeUTF("Press 4 to check status of particular bus nEnter");
  250. int choice = dis.readInt();
  251. //int choice = Integer.parseInt(temp);
  252. switch(choice) {
  253. case 1: bk.addbus(ois);
  254. break;
  255. case 2: bk.cancelbus(dis);
  256. break;
  257. /* case 3: bk.showbuses();
  258. break;
  259. case 4: bk.checkbus(ois);
  260. break;*/
  261. }
  262. }
  263. /*else if(ch == 2) {
  264. //dos.writeUTF("Press 1 to see buses");
  265. //dos.writeUTF("Press 2 to book ticket");
  266. //dos.writeUTF("Press 3 to cancel ticket nEnter");
  267. //temp = dis.readUTF();
  268. //int choice = Integer.parseInt(temp);
  269. switch(choice) {
  270. case 1: bk.showbuses(dos);
  271. break;
  272. case 2: bk.bookticket(dis,dos);
  273. break;
  274. case 3: bk.cancelticket(dis,dos);
  275. break;
  276. }
  277. }*/
  278. /*else if(ch==3) {
  279. try {
  280. bk.updatedata();
  281. }
  282. catch(Exception e) {
  283. e.printStackTrace();
  284. }
  285. dos.writeUTF("Data saved. Exit successful");
  286. this.dis.close();
  287. this.dos.close();
  288. System.exit(0);
  289. }*/
  290. //dos.writeUTF("Do you want to continue? Enter");
  291. String c = dis.readUTF();
  292. if(!c.equals("y")) {
  293. //bk.updatedata();
  294. break;
  295. }
  296. }
  297. this.dis.close();
  298. //this.dos.close();
  299. }
  300. catch(IOException i) {
  301. System.out.println(i);
  302. }
  303. }
  304. }
  305.  
  306. TicketBooking.dos = new DataOutputStream(s.getOutputStream());
  307. TicketBooking.oos = new ObjectOutputStream(dos);
  308. TicketBooking.dis = new DataInputStream(s.getInputStream());
  309. TicketBooking.ois = new ObjectInputStream(dis);
Add Comment
Please, Sign In to add comment