Friday, October 8, 2010

THE HOTEL BOOKING INTERFACE PROGRAM (JAVA CODE)- By Samson C Mkandawire

  /***********************************************************************************************************
   ***********************************************************************************************************

       A HOTEL BOOKING INTERFACE PROGRAM: THE PROGRAM PRODUCES A             CUSTOMIZED INTERFACE FOR  MPINGWE HOTEL SERVICES.
    
                  CODE WRITTEN BY: SAMSON C. MKANDAWIRE (MSE/07/ME/050).
   
                   DEPARTMENT OF MATHEMATICS AND STATISTICS.
   
    ********************************************************************************************************
    ********************************************************************************************************/



  /******************************************************************************
              CLASS IMPORTING STATEMENTS
  ******************************************************************************/

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;


   /********************************************************************************
                 THE MAIN CLASS
  *********************************************************************************/
         
   public class ProjectSam extends JFrame implements ActionListener{
   
       static ProjectSam myProj;
   
       JComboBox roomType,numDaysBooked,titleBox;
   
       JButton confirmButton, cancelButton, exitButton;
   
       JRadioButton cheque,cash, creditCard;
   
      JTextField  nameField, addressField, phoneField,nationalityField,eNameField, eNumberField, dObField,
           checkInField,checkOutField,amountField;
   
      JLabel name, phone, address,modeOfPay1,title,phoneNumber,nationality,
       emergNumber,emergName, rClass,nDaysBkd;
 
   public static String Title1, NumDaysBkd, RoomTyp1, modeOfPay;;
   
   /******************************************************************************************
        CONSTRUCTOR FOR THE PROGRAM
    ******************************************************************************************/   

    public ProjectSam(){
       
        JPanel mainPanel= new JPanel();
     
       mainPanel.setLayout(new GridBagLayout());
        JButton confirm,cancel,exit;    
         
        JLabel nameLabel = new JLabel("MPINGWE HOTEL SERVICES-BOOKING SECTION");
        JLabel modeOfPay1 = new JLabel("Mode of Payment:");
        JLabel nameLabel1 = new JLabel("The Home You can Trust!..");
        JLabel details= new JLabel("CLIENTS INFORMATION");
       
         title = new JLabel ("Title:");
         name = new JLabel("Name:");
         address = new JLabel("Address:");
         phoneNumber = new JLabel("Phone #:");
        nationality =new JLabel ("Nationality:");
         emergName = new JLabel ("Emergency C. Name:");
         emergNumber =new  JLabel ("Emergency C. #:");
         rClass  = new JLabel("Room Class:");
        nDaysBkd  = new JLabel ("Number of Days Booked");
       
       
        JLabel dateBooked = new JLabel ("Date of Booking:");
        JLabel checkIn = new JLabel ("Date of CheckingIN:");
        JLabel checkOut = new JLabel ("Date of CheckOut:");
        JLabel amount = new JLabel ("Booking Fees (MK):");
       

       Box modeOfPay = Box.createHorizontalBox();         
      cash = new JRadioButton("Cash");
        cash. addActionListener(new ModeOfPayListener());
        cheque = new JRadioButton("Cheque");
        cheque. addActionListener(new ModeOfPayListener());
        creditCard = new JRadioButton("Credit Card");
        creditCard. addActionListener(new ModeOfPayListener());
      ButtonGroup modeOfPayGroup = new ButtonGroup();
        modeOfPayGroup.add(cash);
        modeOfPayGroup.add(cheque);
        modeOfPayGroup.add(creditCard);
         modeOfPay.add(cash);
        modeOfPay.add(cheque);
        modeOfPay.add(creditCard);
         modeOfPay.setBorder(BorderFactory.createTitledBorder("MODE OF PAYMENT"));
        addItem(mainPanel, modeOfPay, 0, 20, 2, 1, GridBagConstraints.CENTER);

       JLabel bookingDetails = new JLabel("BOOKING DETAILS");
   
        String[] rclass= {"Single", "Double", "Suite"};
   
        roomType = new JComboBox(rclass);
        roomType.setSelectedIndex(1);
       addItem(mainPanel,roomType, 1, 14, 1, 1, GridBagConstraints.WEST);
        String[] duration= {"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",
                 "2 Months","3 Months","4 Months", "5 Months"};
       
        String[]titlebox1 = {"Prof","Dr","Rev","Mr","Mrs","Miss"};
     
          titleBox = new JComboBox(titlebox1);
        addItem(mainPanel,titleBox,1,3,1,1,GridBagConstraints.WEST);
       
        numDaysBooked = new JComboBox(duration);
        numDaysBooked.setSelectedIndex(2);
    
     
       addItem(mainPanel,numDaysBooked, 1, 13, 1, 1, GridBagConstraints.WEST);
   
        nameField = new JTextField(15);
        addressField= new JTextField(15);
        phoneField = new JTextField(15);
        nationalityField = new JTextField(15);
        eNameField = new JTextField (15);
        eNumberField = new JTextField (15);
        dObField = new JTextField (15);
        checkInField = new JTextField (15);
        checkOutField = new JTextField (15);
        amountField = new JTextField (15);
 
   /************************************************************************               
            ADDING THE LABELS ON THE MAIN PANEL
    ** **********************************************************************/
   
        addItem(mainPanel, nameLabel, 0, 0, 4, 1, GridBagConstraints.CENTER);
        nameLabel.setFont(new Font("Bradley Hand ITC", Font.BOLD, 24));
       
      addItem(mainPanel, nameLabel1, 0, 1, 4, 1, GridBagConstraints.CENTER);
        nameLabel1.setFont(new Font("Times New Roman", Font.ITALIC, 18));
       
        addItem(mainPanel, details, 0, 2, 2, 1, GridBagConstraints.CENTER);
                
        addItem(mainPanel, title, 0, 3, 1, 1, GridBagConstraints.EAST);
      title.setFont(new Font("Times New Roman", Font.BOLD, 18));

       addItem(mainPanel, name, 0, 4, 1, 1, GridBagConstraints.EAST);
         name.setFont(new Font("Times New Roman", Font.BOLD, 18));
   
        addItem(mainPanel, address, 0, 5, 1, 1, GridBagConstraints.EAST);
       address.setFont(new Font("Times New Roman", Font.BOLD, 18));

        addItem(mainPanel, phoneNumber, 0, 6, 1, 1, GridBagConstraints.EAST);
       phoneNumber.setFont(new Font("Times New Roman", Font.BOLD, 18));

   
       addItem(mainPanel, nationality,0,7,1,1,GridBagConstraints.EAST);
         nationality.setFont(new Font("Times New Roman", Font.BOLD, 18));

        addItem(mainPanel, emergName,0,8,1,1,GridBagConstraints.EAST);
       emergName.setFont(new Font("Times New Roman", Font.BOLD, 18));

   
        addItem(mainPanel, emergNumber,0,9,1,1,GridBagConstraints.EAST);
       emergNumber.setFont(new Font("Times New Roman", Font.BOLD, 18));
           
        addItem(mainPanel, rClass,0,14,1,1,GridBagConstraints.EAST);
      rClass.setFont(new Font("Times New Roman", Font.BOLD, 18));

        addItem(mainPanel, nDaysBkd,0,13,1,1,GridBagConstraints.EAST);
      nDaysBkd.setFont(new Font("Times New Roman", Font.BOLD, 18));

        addItem(mainPanel, dateBooked,0,16,1,1,GridBagConstraints.EAST);
        dateBooked.setFont(new Font("Times New Roman", Font.BOLD, 18));

   
        addItem(mainPanel, checkIn,0,17,1,1,GridBagConstraints.EAST);
        checkIn.setFont(new Font("Times New Roman", Font.BOLD, 18));

        addItem(mainPanel, checkOut,0,18,1,1,GridBagConstraints.EAST);
       checkOut.setFont(new Font("Times New Roman", Font.BOLD, 18));
   
        addItem(mainPanel, amount,0,19,1,1,GridBagConstraints.EAST);
        amount.setFont(new Font("Times New Roman", Font.BOLD, 18));

   /**************************************************************************
     ADDING TEXT FIELDS ON THE MAIN PANEL
    **************************************************************************/
     
        addItem(mainPanel, nameField, 1, 4, 2, 1, GridBagConstraints.WEST);
        addItem(mainPanel, addressField, 1, 5, 2, 1, GridBagConstraints.WEST);
       
        addItem(mainPanel, phoneField,1, 6, 1, 1, GridBagConstraints.WEST);
       addItem(mainPanel, nationalityField, 1, 7, 1, 1, GridBagConstraints.WEST);

        addItem(mainPanel, eNameField, 1, 8, 1, 1, GridBagConstraints.WEST);
        addItem(mainPanel, eNumberField, 1, 9, 1, 1, GridBagConstraints.WEST);
           
       addItem(mainPanel, dObField, 1, 16, 1, 1, GridBagConstraints.WEST);
   
       addItem(mainPanel, checkInField , 1, 17, 1, 1, GridBagConstraints.WEST);
       
        addItem(mainPanel, checkOutField , 1, 18, 1, 1, GridBagConstraints.WEST);
       
        addItem(mainPanel, amountField , 1, 19, 1, 1, GridBagConstraints.WEST);


        addItem(mainPanel, bookingDetails, 0, 12, 3, 1, GridBagConstraints.CENTER);
                
   
    /***********************************************************************
              ADDING BUTTONS (CONFIRM, CANCEL, AND EXIT)
    ************************************************************************/
       
        Box buttonBox = Box.createHorizontalBox();      
      confirmButton = new JButton("Confirm");
      cancelButton = new JButton("Cancel");
        exitButton = new JButton("Exit");
       
      buttonBox.add(confirmButton);
        confirmButton.addActionListener(new ConfirmListener () );     
      buttonBox.add(Box.createHorizontalStrut(25));
       buttonBox.add(cancelButton);
        cancelButton.addActionListener(this);
        buttonBox.add(Box.createHorizontalStrut(25));
       buttonBox.add(exitButton);
        exitButton.addActionListener(this);
       addItem(mainPanel, buttonBox, 0, 21, 3, 1, GridBagConstraints.CENTER);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("HOTEL BOOKING SYSTEM");

        add(mainPanel);
        mainPanel.setBackground(Color.blue);
        pack();
       
       setVisible(true);
        setLocationRelativeTo(null);
       }
       
   /* **********************************************************
             THE MAIN FUNCTION
                   
    ************************************************************/

    public static void main(String[] args){
          myProj =new ProjectSam();        
          }

    public void actionPerformed(ActionEvent evnt){
        if(evnt.getSource()==exitButton){
            System.exit(0);
        }
        else if (evnt.getSource()==cancelButton){
            cancel();
         }       
     }

   /**************************************************************
        DEFINING THE CANCEL METHOD
   ***************************************************************/

    public void cancel() {
        nameField.setText("");
        addressField.setText("");
        phoneField.setText("");
        nationalityField.setText("");
        eNameField.setText("");
        eNumberField.setText("");
        dObField.setText("");
        checkInField.setText("");
        checkOutField.setText("");
        amountField.setText("");
    }
   
  /***************************************************************************
    THE CONFIRMLISTENER CLASS THAT PRODUCE THE SECOND WINDOW FOR DISPLAY   
  ****************************************************************************/
 
   public class ConfirmListener  implements ActionListener{
         public void actionPerformed(ActionEvent evt){
           
                                    
                Title1 = (String)titleBox.getSelectedItem();
                NumDaysBkd = (String)numDaysBooked.getSelectedItem();
                RoomTyp1 = (String)roomType.getSelectedItem();
               
           
    /*********************************************************************   
                ADDING LABELS ON THE SECOND PANEL
    **********************************************************************/
               
                JFrame aFrame = new JFrame("Client's Details");
            JPanel myPanel = new JPanel(new GridBagLayout());
               
                JLabel titleLabel = new JLabel("MPINGWE HOTEL SERVICES-BOOKING SECTION ");
           
                addItem(myPanel, titleLabel, 0, 0, 4, 1,
            GridBagConstraints.CENTER);
                   
                titleLabel.setFont(new Font("Bradley Hand ITC", Font.ITALIC, 20));
       

            JLabel title = new JLabel("CLIENT'S BOOKING DETAILS");
                JButton saveButton, cancel2Button, openButton;
           
            addItem(myPanel, title, 0, 1, 4, 1,
              GridBagConstraints.CENTER);
        
           
            addItem(myPanel,  new JLabel("Title:"), 0,2, 2, 1,
              GridBagConstraints.EAST);
              
            addItem(myPanel,  new JLabel("Name:"), 0, 3, 2, 1,
               GridBagConstraints.EAST);
              
            addItem(myPanel,  new JLabel("Address:"), 0,4, 2, 1,
                GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Phone Number:"), 0,5, 2, 1,
                 GridBagConstraints.EAST);

              
                addItem(myPanel,  new JLabel("Nationality:"), 0, 6,2, 1,
                 GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Emergency Contact Name:"), 0, 7,2, 1,
                GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Emergency Contact Number:"), 0,8, 2, 1,
                GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Number of Days Booked:"), 0,9,2, 1,
                GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Room Class:"), 0, 10, 2, 1,
                GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Date of Booking:"), 0,11, 2, 1,
                GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Date of Checking In:"), 0, 12, 2, 1,
                GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Date of Checking Out:"), 0, 13, 2, 1,
               GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Hotel Fees :"), 0, 14, 2, 1,
                GridBagConstraints.EAST);
                   
                addItem(myPanel,  new JLabel("Mode of Payment :"), 0, 15, 2, 1,
                GridBagConstraints.EAST);

     /**********************************************************************************
            
            ADDING FIELD AND SELECTED ITEMS FROM COMBO BOXES ON THE SECOND PANEL
    **********************************************************************************/   
               
                addItem(myPanel,  new JLabel(Title1), 2, 2, 2, 1,
                 GridBagConstraints.WEST);

            addItem(myPanel,  new JLabel(nameField.getText()), 2, 3, 2, 1,
               GridBagConstraints.WEST);
              
            addItem(myPanel,  new JLabel(addressField.getText()), 2, 4, 2, 1,
               GridBagConstraints.WEST);
              
            addItem(myPanel,  new JLabel(phoneField.getText()), 2,5, 2, 1,
               GridBagConstraints.WEST);
                   
              
            addItem(myPanel,  new JLabel(nationalityField.getText()), 2, 6, 2, 1,
               GridBagConstraints.WEST);
              
           addItem(myPanel,  new JLabel(eNameField.getText()), 2, 7, 2, 1,
              GridBagConstraints.WEST);
        
              
           addItem(myPanel,  new JLabel(eNumberField.getText()), 2,8,2, 1,
               GridBagConstraints.WEST);
        
           
                 
           addItem(myPanel,  new JLabel(NumDaysBkd), 2, 9, 2, 1,
               GridBagConstraints.WEST);


             addItem(myPanel,  new JLabel(RoomTyp1),2,10,2, 1,
               GridBagConstraints.WEST);

        
          addItem(myPanel,  new JLabel(dObField.getText()), 2,11,2, 1,
             GridBagConstraints.WEST);
                   
             addItem(myPanel,  new JLabel(checkInField.getText()), 2,12, 2, 1,
               GridBagConstraints.WEST);
                   
            addItem(myPanel,  new JLabel(checkOutField.getText()), 2, 13, 2, 1,
               GridBagConstraints.WEST);
                   
            addItem(myPanel,  new JLabel(amountField.getText()), 2, 14,2, 1,
               GridBagConstraints.WEST);
                   
            addItem(myPanel,  new JLabel(modeOfPay), 2, 15,2, 1,
               GridBagConstraints.WEST);
  
   
     /*****************************************************************************            
           
            ADDING BUTTONS (Save, Open, Cancel) ON THE SECOND PANEL
    *******************************************************************************/       
           
                   
            Box buttonBox = Box.createHorizontalBox();      
         saveButton = new JButton("Save");
           openButton= new JButton ("Open");
         cancel2Button = new JButton("Cancel");
         buttonBox.add(saveButton);
         saveButton.addActionListener( new SaveListener());
            buttonBox.add(Box.createHorizontalStrut(20));
            buttonBox.add(openButton);
            openButton.addActionListener( new OpenListener());

         buttonBox.add(Box.createHorizontalStrut(20));
         buttonBox.add(cancel2Button);
            addItem(myPanel, buttonBox, 1, 16, 3, 1,
            GridBagConstraints.CENTER);
               
            aFrame.setSize(500,700);
            aFrame.setContentPane(myPanel);
           
         setDefaultCloseOperation(aFrame.EXIT_ON_CLOSE);
            aFrame.setVisible(true);
       
            }
        }

       
 /**********************************************************************       
    CLASSES THAT IMPLEMENTS ACTION LISTENERS
************************************************************************/
       
    class SaveListener implements ActionListener{
          public void actionPerformed(ActionEvent evnt){
            JFileChooser fileChooser = new JFileChooser();
            int result = fileChooser.showSaveDialog(myProj);
            if(result==JFileChooser.APPROVE_OPTION){
                  File outFile = null;
               outFile=fileChooser.getSelectedFile();
                if (outFile.exists()){
                    JOptionPane.showMessageDialog(null, "File already exists");
                }   
                else
                    try{
                        FileOutputStream fileStream = new     FileOutputStream(outFile);
                        DataOutputStream dataStream = new      DataOutputStream(fileStream);
                        fileStream.close();
                       }
                 
                      catch(IOException e){
                               JOptionPane.showMessageDialog(null, "Error in Opening");
                 }   
                }
                else if(result==JFileChooser.ERROR_OPTION){
                        JOptionPane.showMessageDialog(null, "ERROR OCCURS IN SAVING");
                }
            }
        }
   

         public class OpenListener implements ActionListener{
            public void actionPerformed(ActionEvent evnt){                                         
               JFileChooser fileChooser = new JFileChooser();
                  int result = fileChooser.showOpenDialog(myProj);
                     if(result==JFileChooser.APPROVE_OPTION){              
                           File inFile = null;
                            inFile=fileChooser.getSelectedFile();
               try{
                    FileInputStream fileStream = new    FileInputStream(inFile);
                    DataInputStream dataStream = new    DataInputStream(fileStream);
                   String displayText = dataStream.readUTF();
           
                    fileStream.close();
                }
                catch(IOException ioExcept){
                     JOptionPane.showMessageDialog(null, "File Not Found");
                }   
            }
        }
        }                
   
   
     public class  ModeOfPayListener  implements ActionListener{
          public void actionPerformed(ActionEvent evt){
            Object source = evt.getSource();
            if (source==cash)   modeOfPay ="Cash";
            else if(source==cheque) modeOfPay ="Cheque";
            else modeOfPay = "Credit Card";
             }
          }
   
   
     public void addItem(JPanel p, JComponent c,int x, int y, int width, int height, int align){
        GridBagConstraints gridBag = new GridBagConstraints();
          gridBag.gridx = x;
          gridBag.gridy = y;
          gridBag.gridwidth = width;
          gridBag.gridheight = height;
          gridBag.weightx = 100.0;
          gridBag.weighty = 100.0;
          gridBag.insets = new Insets(8, 8, 5, 5);
          gridBag.anchor = align;
          gridBag.fill = GridBagConstraints.NONE;
          p.add(c, gridBag);

     }
    }

 /*****************************************************************************************
   *************************************************************************************           
                 END OF CODE
    *************************************************************************************
 ******************************************************************************************/              

No comments:

Post a Comment