Tuesday, October 19, 2010

2D CHESS BOARD AND CHESS PIECES-By Samson C Mkandawire

THE 2D CHESS BOARD AND CHESS PIECES

Introduction
Chess is both an indoor and outdoor game played by two individuals on either a board or a loan. The players of the chess game move chess pieces in order to have a checkmate move which aims at the king piece of the opponent. Currently, electronic chess boards and chess pieces have been developed allowing individuals to play the chess game on a computer against an opponent which can either be the computer itself or a fellow individual. In this outline, a brief description through snapshots of outputs of a program that is designed and coded to produce an electronic two dimension (2D) chess board with chess pieces arranged on it is discussed.
  
The snapshots below have been produced by a Java program. The program has been developed by using the Java Runtime Environment (JRE) which has the Java Development Kit (JDK) as the compiler and the jGrasp as the text editor. The outline below include a snapshot of a 2D chess board with chess pieces arranged on the board ready for a game and the java code that has generated the chess board.

Attributes
After running the program, the code produces a 2D chess board with the chess pieces arranged on it. The chess board is defined with two colours namely black and white. On the other hand , the chess pieces which have just been imported from a file (file path c:\\Chesspieces\\) have not been generated by the java code. The pieces are saved in the PNG file format.

Apart from the chess board and the chess pieces, there is an inscription on the board which describes the chess board as a product of Samson C Mkandawire. This inscription has been produced generated on the drawing canvas by a drawing method from a drawString class.



SNAPSHOTS 
Once the program has been run, a 2D chess board is generated by the code and produced on the desktop. The frame interface where the board is generated can be either minimized or maximized depending on the preference of the individual. On the other hand, the actual board has a fixed dimension of 1000 * 700.

The interface on which the chess board is can be closed by clicking the mouse on the X button on the far right corner of the interface or placing the control key (Ctrl+X). Figure 1.0 below shows the chess board without chess pieces arranged on it.

                                               Table 1.0: 2D Chess board and Chess pieces

   

                                      

           
            The figure 1.0 above shows a 2D chess board without chess pieces arranged on it.

For the game of chess, there are thirty two pieces that are arranged on the above chess board. these pieces are played by two sides (individuals or an inidividual against a computer). Figure 2.0 below shows a 2D chess board with pieces arranged on it.

                               Table 2.0: A 2D Chess board with chess pieces arranged on it

         






EXPERIENCES LEARNT
The above displayed snapshots have been produced by a program coded as part of an assessment task for the final grade for the award of a Bachelor of Sciences in Mathematical Sciences Education. At the same time, the task was given as course work for understanding how graphical user interfaces are produced using java language. As part of course work, they are several experiences learnt from the process of coding the program and at the same time compiling and running the code for the above snapshots to be produced. Some of the experiences learnt include debugging of a java code, use of the draw method, drawing of 2D diagrams on a canvas, importing of images form a file saved on a disk. These experiences are outlined below;

Debugging of a java 
Development of any computer code (program) involves the programmer making a lot of errors. These errors are both compiler and run time errors. It takes a great deal to learn and understand these errors in order to come up with a code which can run and at the same time produce the desired output. With this assignment, debugging of codes that involve the production of graphical user interfaces in two dimensions has been learnt and at the same time mastered such that using this experience, codes which produce 2D images can be produced.

Use of draw method in java
The draw method in java is responsible for generating graphic images in a drawing canvas. These images are both graphic images and the graphic 2D images.

Drawing of 2D graphic images
The chess board is a 2D graphic image generated from a graphic object. The 2D graphic image is a generated by a 2D objected which has been casted.

Importing of graphic images from a file
The chess board has been generated by the cod but the chess pieces are png images which have been saved in a file. These chess pieces have been imported and position and placed on the board for the display.



Conclusion
The major objective of the program was to develop a 2D chess board with chess pieces arranged on it (using Java Programming Language). The out put just display a chess board and the chess pieces arranged on it. There is, however, a need to develop it further so that the pieces can be moved from one position to the other in order to play the game. At the same time, some pieces have been duplicated due to shortage of other pieces hence there is a need to either have uniform pieces or improve the code so that the code ha to generate the pieces.

The Java Code
The code that generated the above snaphot is as below;

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

public class SamChess extends JFrame {
    private JLabel myLabel;
    private JPanel myPanel;
    private DrawingPanel drawPanel;
  static SamChess myChessBoard;
  private int x = 100, y = 60;

    public SamChess() {
 
     super("CHESS BOARD AND CHESS PIECES DEVELOPED BY SAMSON C. MKANDAWIRE");
 
        myPanel = new JPanel();
        myPanel.setLayout(null);
        myPanel.setBackground(Color.GREEN);

        drawPanel= new DrawingPanel();
        drawPanel.setBounds(myPanel.getWidth() / 2, myPanel.getHeight() / 2, 1000, 1000);
        drawPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        myPanel.add(drawPanel, BorderLayout.CENTER);
      setContentPane(myPanel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000,700);
        setLocation(10,10);
   
        setVisible(true);
      
    }

    public static void main(String[] args) {
         myChessBoard = new  SamChess();
           }

  class DrawingPanel extends JPanel {
     private int x = 100, y = 70;   

     protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         Graphics2D g2d = (Graphics2D) g;
         drawingMethod(g2d);
     }

    private void drawingMethod(Graphics2D g2d) {
 
 
     g2d.drawRect(100,75,800,500);
    
     
    g2d.setColor(Color.BLACK);
   
     g2d.setFont(new Font("Bradley Hand ITC", Font.BOLD, 20));
    g2d.drawString("  2D CHESS GAME:A product of Samson C Mkandawire-MSE/07/ME/050",100,50);
  

    g2d.fillRect(200, 75, 100, 65);
   
    ImageIcon myImage3 = new ImageIcon("f:\\Chesspieces\\chess2.png");
    Image picImage3 = myImage3.getImage();
    g2d.drawImage(picImage3,130,75, this);
   
    ImageIcon myImage = new ImageIcon("f:\\Chesspieces\\chess1.png");
    Image picImage = myImage.getImage();
    g2d.drawImage(picImage, 230,75, this);
   
    ImageIcon myImage6 = new ImageIcon("f:\\Chesspieces\\chess3.png");
    Image picImage6 = myImage6.getImage();
    g2d.drawImage(picImage6, 330,75, this);
   
      g2d.fillRect(400, 75, 100, 65);
    ImageIcon myImage8 = new ImageIcon("f:\\Chesspieces\\chess5.png");
    Image picImage8 = myImage8.getImage();
    g2d.drawImage(picImage8, 430,75, this);   
      
   
    ImageIcon myImage9 = new ImageIcon("f:\\Chesspieces\\chess2.png");
    Image picImage9 = myImage9.getImage();
   
    g2d.fillRect(100, 140, 100, 55);
    g2d.fillRect(300, 140, 100, 55);
    g2d.fillRect(500, 140, 100, 55);
      g2d.fillRect(700, 140, 100, 55);
    g2d.drawImage(picImage9, 130,142, this);   
    g2d.drawImage(picImage9, 230,142, this);   
    g2d.drawImage(picImage9, 330,142, this);   
    g2d.drawImage(picImage9, 430,142, this);   
    g2d.drawImage(picImage9, 530,142, this);  
    g2d.drawImage(picImage9, 630,142, this);   
    g2d.drawImage(picImage9, 730,142, this);
    g2d.drawImage(picImage9, 830,142, this);   
    g2d.fillRect(200, 195, 100, 65);
    g2d.fillRect(400, 195, 100, 65);
    g2d.fillRect(600, 195, 100, 65);
    g2d.fillRect(800, 195, 100, 65);  
  
    g2d.fillRect(100, 255, 100, 65);
    g2d.fillRect(300, 255, 100, 65);
    g2d.fillRect(500, 255, 100, 65);
    g2d.fillRect(700, 255, 100, 65);
   
    g2d.fillRect(200, 315, 100, 65);
    g2d.fillRect(400, 315, 100, 65);
    g2d.fillRect(600, 315, 100, 65);
    g2d.fillRect(800, 315, 100, 65);
   
    g2d.fillRect(100, 375, 100, 65);
    g2d.fillRect(300, 375, 100, 65);
    g2d.fillRect(500, 375, 100, 65);
    g2d.fillRect(700, 375, 100, 65);
      g2d.fillRect(800, 75, 100, 65);
  
    ImageIcon myImage4 = new ImageIcon("f:\\Chesspieces\\chess2.png");
    Image picImage4 = myImage4.getImage();
    g2d.drawImage(picImage4, 830,75, this);
   
    ImageIcon myImage2 = new ImageIcon("f:\\Chesspieces\\chess1.png");
    Image picImage2 = myImage2.getImage();
    g2d.drawImage(picImage2,730,75, this);
   
    g2d.fillRect(600, 75, 100, 65);
    ImageIcon myImage5 = new ImageIcon("f:\\Chesspieces\\chess3.png");
    Image picImage5 = myImage5.getImage();
    g2d.drawImage(picImage5, 630,75, this);
   
    ImageIcon myImage7 = new ImageIcon("f:\\Chesspieces\\chess4.png");
    Image picImage7 = myImage7.getImage();
    g2d.drawImage(picImage7, 530,71, this);

   g2d.fillRect(100, 505, 100, 65);
     ImageIcon myImage17 = new ImageIcon("f:\\Chesspieces\\sam2.png");
    Image picImage17 = myImage17.getImage();
     g2d.drawImage(picImage17,130,505, this);
            
   
     ImageIcon myImage18 = new ImageIcon("f:\\Chesspieces\\sam5.png");
          Image picImage18 = myImage18.getImage();
     g2d.drawImage(picImage18,130,438, this);
   
    g2d.fillRect(200, 440, 100, 65);
    g2d.fillRect(400, 440, 100, 65);
    g2d.fillRect(600, 440, 100, 65);
    g2d.fillRect(800, 440, 100, 65);
  
    g2d.drawImage(picImage18,230,438, this);
    g2d.drawImage(picImage18,330,438, this);
    g2d.drawImage(picImage18,430,438, this); 
    g2d.drawImage(picImage18,530,438, this);
    g2d.drawImage(picImage18,630,438, this);
    g2d.drawImage(picImage18,730,438, this);
    g2d.drawImage(picImage18,830,438, this);
      
  
    ImageIcon myImage28 = new ImageIcon("f:\\Chesspieces\\sam3.png");
    Image picImage28 = myImage28.getImage();
    g2d.drawImage(picImage28,230,505, this);
   
    g2d.fillRect(300, 505, 100, 65);
    ImageIcon myImage29 = new ImageIcon("f:\\Chesspieces\\sam1.png");
    Image picImage29 = myImage29.getImage();
    g2d.drawImage(picImage29,330,505, this);
   
    ImageIcon myImage25 = new ImageIcon("f:\\Chesspieces\\sam2.png");
    Image picImage25 = myImage25.getImage();
    g2d.drawImage(picImage25,830,505, this);
  
   
    g2d.fillRect(700, 505, 100, 65);
    ImageIcon myImage27 = new ImageIcon("f:\\Chesspieces\\sam3.png");
    Image picImage27 = myImage27.getImage();
    g2d.drawImage(picImage27,730,505, this);
   
      
      ImageIcon myImage30 = new ImageIcon("f:\\Chesspieces\\sam1.png");
    Image picImage30 = myImage30.getImage();
    g2d.drawImage(picImage30,630,505, this);
   
    g2d.fillRect(500, 505, 100, 65);
    ImageIcon myImage31 = new ImageIcon("f:\\Chesspieces\\sam4.png");
    Image picImage31 = myImage31.getImage();
    g2d.drawImage(picImage31,530,505, this);
   
    ImageIcon myImage32 = new ImageIcon("f:\\Chesspieces\\sam6.png");
    Image picImage32 = myImage32.getImage();
    g2d.drawImage(picImage32,430,505, this);
     
    }
   }
 }
  
 

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
    *************************************************************************************
 ******************************************************************************************/              

Thursday, October 7, 2010

THE HOTEL BOOKING INTERFACE ASSIGNMENT (SNAPSHOTS)-By Samson Mkandawire

                                         THE HOTEL BOOKING INTERFACE

Introduction
The hotel booking interface is a computer interface that can be specifically designed and programmed in order to allow hotel owners and workers to get clients details and in store them in their computer’s hard disk or a database. In this outline, a brief description through snapshots of outputs of a program that is designed and coded to produce a hotel booking interface is given.

The snapshots below have been produced by a Java program. The program has been developed by using the Java Runtime Environment (JRE) which has the Java Development Kit (JDK) as the compiler and the jGrasp as the text editor. The outline below include attributes and functionalities which can be used to capture the clients information and again either check the client’s information or save the client’s information if the client is first booking the hotel.  At the same time, it provides a section of the experiences that  have been learnt from producing the code that generates the snapshots below.

Attributes
After running the program, the code produces an interface which has the following: name of the hotel and the section where the services of booking are offered (Booking section), the motto of the hotel, labels of the required information from the clients, the fields where the client’s information is supposed to be filled, two subheadings which divides the client’s information (client’s details and booking details), the made of payment used by the client (Cash, cheque or credit card) and a button point which contains three buttons (confirm, cancel and exit).

The name of the hotel and the section where the service is applied makes the interface to directly recognize the hotel and specifically identifies the section where the user of the interface (computer) is working. It is not editable hence no user of the system can alter it in order to access the services of the hotel fraudulently. In other terms, this aspect identifies the hotel uniquely.

The motto of the hotel (The Home You Can Trust!...) gives the client a chance of understanding the type of the hotel and its services.  It is also not editable in order to protect the hotel from individuals who might decide to access the services of the hotel fraudulently.

The client’s details specify the information that is required which uniquely identifies each client. It has two sections namely: the labels of each piece of the required information and where the information is supposed to be filled or selected. The labels are not editable while the information in the fields once filled can be edited by the user. The labels include the title of the client, the name of the client (its corresponding field can take both surname and first name), address of the client, phone number of the client, nationality of the client, emergency contact name and emergency contact number (phone number).
The booking details species details pertaining to the actual process of booking. Again it has two sections namely: the labels of the required information and the fields where the information is expected to be filled.  The labels are not editable while the information in the field can be edited once it has been filled. The labels include number of days booked, room class (single, double, suite), date of booking, date for checking in, date for checking out, booking fees in Malawi Kwacha and mode of payment which specifies the kind of payment which the client can use (cash, cheque or credit card). The mode of payment has three options with a restriction that a client can only use one of the three means for a booking at a time.

The button point has three buttons namely the confirm button, the cancel button and the exit button. The confirm button is used for checking the correctness of the information selected or filled on the form of the interface. It is operated by clicking it with the mouse. Once a user clicks it, there is another form produced that contains all the selected or filled information if the information has been filled, if the information has not been filled it produces a form with the labels but without their respective required information.

The cancel button once clicked clears all the information in the fields. This button allows the user to re enter the details in the field once a mistake has happened in entering the details.

The exit button is used to exit or close the window. The exit button can be used in the same way as the ctrl+x keyboard command or the close icon on the window (X). 

After clicking the confirm button, the user will produce another form which will contain all the information on the first form and at the same time it will have three buttons namely the save button, the open button and the cancel button. The save button allows the user to save the displayed information in a file.  The open button opens an already saved file to check the information of the client who had already made a booking with the hotel and the cancel button cancels the confirm form.  

SNAPSHOTS 
Once the program has been run, an initial window (interface) is produced and it is from this window where the user of the interface can start his or her operations in capturing the client’s information into the computer. The initial window has been produced in such a way that it can be minimized, maximized or even closed at any time the user fills to do so. The interface shows all the required details at the same time as in figure 1.0 below;

                              Figure 1.0: Blank Hotel Booking Interface Form

 




In Figure 1.0 above, the blank field can be filled with the client’s information where the title, the number of days booked and the room class can be changed to suite the client’s details. Once the information has been filled, the above form will look lie figure 2.0 below;

                                Figure 2.0:  Filled Hotel Booking Interface Form


      

Figure 2.0 above shows the hotel form interface with the information of the client filled in the fields. This information can be edit by the user while still filling more information. At the same time, the user can change the information of any field which has already been filled or clear all the information by clicking on the clear button to restart the whole process of filling in the information again. After filling in the information in the fields, there is a need to check whether the filled information is the correct information. To do this the user has to click on the confirm button which produces another form (Window) which contains all the labels of the first plus the filled information. The information on the confirm form (window) is not editable as shown in figure 3.0 below;


                                     Figure 3.0: A verification form
   

Figure 3.0 above shows the filled information of the client produced along side the first form (window). The information on this form is not editable.  In this case, if the user want to change some or all of the information, then he or she should click the cancel button or click on the close icon (X) on the window otherwise the user can maximize the window for clear view as per figure 4.0 below;
        
                                            Figure 4.0: A maximized confirm form
   
      


Figure 4.0 above shows a form containing client’s information in a maximized status which is not editable. If the user is satisfied with the information filled then he or she can proceed to save the information into a file by clicking the save button. Once the save button is clicked, a window will appear on top of the first window requiring the user to specify the folder in which the file will be saved and at the same time, specifying the file name as shown in figure 5.0 below;

                              Figure 5.0: Hotel booking interface with a save dialogue box
    

The Figure 5.0 above shows the form with the client’s information and the save form where the user specifies the fold where the information to be saved is stored and a file name field where the user types the file name for identification at a later stage. If the user has specified the folder and the file name, then he or she has to click on the save button on the save form. Once the save button has been clicked, the program will check whether the file name already exist or not. If the file name typed by the user already exist then the program will produced another box indicating that the file name given to identify the client’s information already exist otherwise the system will save the information in the specified information. The dialogue box for the file name that already exists is as shown on the figure 6.0:

                    Figure 6.0: A message box indicating that the typed file name already exist
        

The figure 6.0 above shows a form with a dialogue box indicating that the file name used by the user already exists in the saving system of the program. Once the program has indicated that the file name then the user is supposed to click on ok button and then return to the save button on the confirm window where he or she is supposed to restart the process of saving the information.

Apart from saving information from new clients, the hotel has clients who had already made bookings with the hotel. In this case there is need to confirm the clients claims by checking his or her information in the system. If this is the case then the user of the interface has to click on confirm on the first window (that is the interface that is generated after running the program) in order to go to the save Window.  After the second window has been generated, then the user has to click on the open button. This will produce an Open dialogue box which provides a text field where the user is supposed to type in the file name where the client’s details were saved. The open dialogue box is as the figure 7.0 below;
    
                           Figure 7.0: A hotel booking form with an open dialogue box
        
The figure 8.0 above shows the open box embedded on the main interface (form). The above open form allows the user to enter the file name and once the file name has been entered, the program will check with the files kept in the system. The figure 8.0 below shows the open box with the file name typed on the file name text field;

                        Figure 8.0: An open dialogue box showing the typed file name
    
  

The figure 8.0 above shows an open window dialogue box with the file name typed on the file name field embedded on the main interface window. After the user has typed the file name, the program checks the file name amongst the files saved in the system. If the file name is not amongst the files saved in the system, a file not find dialogue box is displayed otherwise the program opens the file with the saved information. The figure 9.0 shows the file name not found embedded on the main interface form.   

                     Figure 9.0: The message dialogue box indicating file not found

     

EXPERIENCES THAT HAVE BEEN ACQUIRED
The above displayed snapshots have been produced by a program coded as part of an assessment task for the final grade for the award of a Bachelor of Sciences in Mathematical Sciences Education. At the same time, the task was given as course work for understanding how graphical user interfaces are produced using java language. As part of course work, they are several experiences learnt from the process of coding the program and at the same time compiling and running the code for the above snapshots to be produced. Some of the experiences learnt include debugging of a java code, use of different layout managers in java, positioning of different objects on a panel, linking of different classes (both user defined and the pre defined classes), Use of action listeners, production of different java windows which are linked to each other and placing of press buttons, radio buttons and check lists on the panel of a given window. These experiences are outlined below;
Debugging of a java 
Development of any computer code (program) involves the programmer making a lot of errors. These errors are both compiler and run time errors. It takes a great deal to learn and understand these errors in order to come up with a code which can run and at the same time produce the desired output. With this assignment, debugging of codes that involve the production of graphical user interfaces has been learnt and at the same time mastered such that using this experience, codes which produce graphical user interfaces can be produced.

Use of different layout manager
The layout managers in java are responsible for controlling the positioning of components on a panel or frame. In the program which was coded to produce the above snapshots employed two layout managers. These layout managers are the flow layout manager and the GrigBagLayout manager. The Flow layout manager which in java is used as a default layout manager was used in placing the press buttons. On the other, the GridBagLayout manager was used to place all the components on both panels.

Positioning of different Components on a panel
The hotel booking interface has different components like the labels and the fields where information is filled. These components are supposed to be aligned correctly so that the interface should be meaningful and the collect information should be filled in the collect fields. The production of the above snapshots has made this to be learnt and mastered. 

Linking of different classes
The code which has generated the above snapshots has used both user defined classes and the already defined classes that have their attributes and methods inherited in the code. The linking together of these classes has given much experience in developing of graphical user interfaces by using both user defined classes and pre defined classes in java.

Use of action listeners
The above snapshots have been generated by a java program which has buttons that are supposed to be operational (doing what they are intended for) on the produced interface. For these buttons and check lists to be operational, there is need for action listeners and the application of these action listeners has provided much experience in working with action listeners.

Production of different java windows that are linked to each other
The code that produced the above snapshots has two windows. The first window is produced after running the program while the second window is produced after the user has clicked on the confirm button of the first window. These two windows are generated by two different classes are calling each others’ methods.

Placing of press buttons, check lists and radio buttons   
The above snapshots have press buttons, radio buttons and check lists which are linked to action listeners so that after clicking them, an action is generated. The inclusion of these buttons has provided much experience in developing graphical user interfaces that simplifies the work of the user as the user just clicks the buttons instead of keying in every information from the clients. 

Conclusion
The major objective of the program was to develop a hotel booking interface (using Java Programming Language) that hotel owners can be used online or electronically. It does some basic features like capturing clients’ information, saving clients’ information and opening fclients' files from the hard disk. There is, however, a need to develop it further so that the file can be saved to or retrieved from the database. At the same time, the program should be able to generate a dialogue box for checking cheque numbers or credit card numbers so that the client should not fraudulently acquire the booking services of the hotel.