Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.util.Scanner;
- import java.awt.*;
- import java.io.*;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JTextField;
- import javax.swing.JPasswordField;
- public class TicketBooking extends JFrame{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- //private JFrame frame;
- private JPanel contentPane;
- private JTextField textFieldusr;
- private JPasswordField passwordField;
- private JLabel lblPassword;
- private JLabel lblUsername;
- private JLabel lblWelcomeToBus;
- private JButton btnAdmin;
- private JButton btnUser;
- public static DataOutputStream dos = null;
- public static ObjectOutputStream oos = null;
- public static DataInputStream dis = null;
- public static ObjectInputStream ois = null;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- TicketBooking frame = new TicketBooking();
- frame.setVisible(true);
- Socket s = new Socket("localhost",3000);
- System.out.println("nCONNECTED TO SERVERn");
- //DataInputStream dis = new DataInputStream(s.getInputStream());
- TicketBooking.dos = new DataOutputStream(s.getOutputStream());
- TicketBooking.oos = new ObjectOutputStream(s.getOutputStream());
- TicketBooking.dis = new DataInputStream(s.getInputStream());
- TicketBooking.ois = new ObjectInputStream(s.getInputStream()); //This statement is causing the infinite loop
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public TicketBooking() {
- super("Bus Ticket Booking System");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(200,200,450,450);
- contentPane = new JPanel();
- contentPane.setForeground(Color.BLACK);
- contentPane.setBackground(Color.DARK_GRAY);
- //frame.setBounds(200, 200, 450, 450);
- contentPane.setBorder(new EmptyBorder(5,5,5,5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- lblWelcomeToBus = new JLabel("Welcome to Bus Ticket Booking System");
- lblWelcomeToBus.setFont(new Font("Dialog", Font.BOLD, 18));
- lblWelcomeToBus.setForeground(Color.WHITE);
- lblWelcomeToBus.setBounds(12, 37, 415, 39);
- contentPane.add(lblWelcomeToBus);
- btnAdmin = new JButton("Admin");
- btnAdmin.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- //JOptionPane.showMessageDialog(null,"Admin logged in");
- if(textFieldusr.getText().equals("Admin") && new String(passwordField.getPassword()).equals("1234")) {
- Adminframe obj = new Adminframe();
- obj.setVisible(true);
- try {
- TicketBooking.dos.writeInt(1);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- else {
- JOptionPane.showMessageDialog(null,"Wrong username or password");
- }
- }
- });
- btnAdmin.setBounds(57, 337, 117, 25);
- contentPane.add(btnAdmin);
- btnUser = new JButton("User");
- btnUser.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- //JOptionPane.showMessageDialog(null,"User logged in");
- if(textFieldusr.getText().equals("User") && new String(passwordField.getPassword()).equals("1234")) {
- UserFrame obj = new UserFrame();
- obj.setVisible(true);
- try {
- TicketBooking.dos.writeInt(2);
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- else {
- JOptionPane.showMessageDialog(null,"Wrong username or password");
- }
- }
- });
- btnUser.setBounds(274, 337, 117, 25);
- contentPane.add(btnUser);
- lblUsername = new JLabel("Username");
- lblUsername.setFont(new Font("Dialog", Font.BOLD, 15));
- lblUsername.setForeground(Color.WHITE);
- lblUsername.setBounds(57, 157, 105, 30);
- contentPane.add(lblUsername);
- textFieldusr = new JTextField();
- textFieldusr.setBounds(260, 158, 131, 30);
- contentPane.add(textFieldusr);
- textFieldusr.setColumns(10);
- lblPassword = new JLabel("Password");
- lblPassword.setFont(new Font("Dialog", Font.BOLD, 15));
- lblPassword.setForeground(Color.WHITE);
- lblPassword.setBounds(57, 232, 105, 30);
- contentPane.add(lblPassword);
- passwordField = new JPasswordField();
- passwordField.setBounds(260, 233, 131, 30);
- contentPane.add(passwordField);
- }
- }
- import java.net.*;
- import java.io.*;
- class BusTicketSystem {
- public static void main(String[] args) throws IOException
- {
- //Booking bk = new Booking();
- //bk.getdata();
- MySQLJDBC mysqljdbc = new MySQLJDBC();
- //mysqljdbc.read();
- ServerSocket ss = new ServerSocket(3000);
- // running infinite loop for getting
- // client request
- while (true)
- {
- Socket s = null;
- try
- {
- // socket object to receive incoming client requests
- s = ss.accept();
- System.out.println("A new client is connected : " + s);
- // obtaining input and out streams
- ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
- DataInputStream dis = new DataInputStream(s.getInputStream());
- //DataOutputStream dos = new DataOutputStream(s.getOutputStream());
- System.out.println("Assigning new thread for this client");
- // create a new thread object
- Thread t = new ClientHandler(s,dis,ois);
- // Invoking the start() method
- t.start();
- }
- catch (Exception e){
- s.close();
- e.printStackTrace();
- }
- }
- }
- }
- class ClientHandler extends Thread {
- final ObjectInputStream ois;
- final DataInputStream dis;
- //final DataOutputStream dos;
- final Socket s;
- // Constructor
- public ClientHandler(Socket s, DataInputStream dis, ObjectInputStream ois)
- {
- this.s = s;
- this.dis = dis;
- this.ois = ois;
- //this.dos = dos;
- }
- @Override
- public void run() {
- Booking bk = new Booking();
- try {
- //Scanner sc = new Scanner(System.in);
- //String temp;
- System.out.println("Customer connected");
- while(true) {
- System.out.println("In while");
- int ch = dis.readInt();
- //dos.writeUTF("Press 1 to login as admin");
- //dos.writeUTF("Press 2 to login as usernEnter");
- //dos.writeUTF("Press 3 to exit n Enter");
- //temp = dis.readUTF();
- //int ch = Integer.parseInt(temp);
- if(ch == 1) {
- //dos.writeUTF("Press 1 to add bus");
- //dos.writeUTF("Press 2 to cancel bus");
- //dos.writeUTF("Press 3 to see buses");
- //dos.writeUTF("Press 4 to check status of particular bus nEnter");
- int choice = dis.readInt();
- //int choice = Integer.parseInt(temp);
- switch(choice) {
- case 1: bk.addbus(ois);
- break;
- case 2: bk.cancelbus(dis);
- break;
- /* case 3: bk.showbuses();
- break;
- case 4: bk.checkbus(ois);
- break;*/
- }
- }
- /*else if(ch == 2) {
- //dos.writeUTF("Press 1 to see buses");
- //dos.writeUTF("Press 2 to book ticket");
- //dos.writeUTF("Press 3 to cancel ticket nEnter");
- //temp = dis.readUTF();
- //int choice = Integer.parseInt(temp);
- switch(choice) {
- case 1: bk.showbuses(dos);
- break;
- case 2: bk.bookticket(dis,dos);
- break;
- case 3: bk.cancelticket(dis,dos);
- break;
- }
- }*/
- /*else if(ch==3) {
- try {
- bk.updatedata();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- dos.writeUTF("Data saved. Exit successful");
- this.dis.close();
- this.dos.close();
- System.exit(0);
- }*/
- //dos.writeUTF("Do you want to continue? Enter");
- String c = dis.readUTF();
- if(!c.equals("y")) {
- //bk.updatedata();
- break;
- }
- }
- this.dis.close();
- //this.dos.close();
- }
- catch(IOException i) {
- System.out.println(i);
- }
- }
- }
- TicketBooking.dos = new DataOutputStream(s.getOutputStream());
- TicketBooking.oos = new ObjectOutputStream(dos);
- TicketBooking.dis = new DataInputStream(s.getInputStream());
- TicketBooking.ois = new ObjectInputStream(dis);
Add Comment
Please, Sign In to add comment