Link to home
Start Free TrialLog in
Avatar of marcoullisp
marcoullisp

asked on

Need comments on a Java program (learning experience)

Hi all,

I am trying to understand Java in a small amount of time. What I need is for someone who is proficient in Java to comment out the following Java program that I found on these lists. All I need is commenting out so a novice like myself could better understand how the program flows. I posted this same question the other day, and got only maybe 10-15 comments on this program. I am in it for the learning experience. My wish is to understand Java better by the end of the week.

Kind Regards,

PKM

Code follows (The previous posts comments are included):

===

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;

//This class defines one field in a record

public class ATM extends Applet
{
    public void init()
    {
        // add panel to applet

         add(new ATMPanel());
    }

    static class ATMPanel extends Panel
    {
            private static String PIN= "3602";
         private int accountmoney = 3400;
         private static final int WAIT_FOR_CARD = 1;
         private static final int WAIT_FOR_PIN = 2;
         private static final int MENU_DISPLAYED = 3;
         private static final int TRANSFER_MONEY = 4;
         private static final int DISBURSE_MONEY = 5;
         private static final int CHECK_BALANCE = 6;

         private TextArea mScreen=
              new TextArea("", 6, 45, TextArea.SCROLLBARS_NONE);

         private Button mEnter= new Button("Enter");
         private Button mClear= new Button("Clear");
         private Button mCancel= new Button("Cancel");

         private JButton Receipt = new JButton("Receipts Slot");
         private Button Quit = new Button("Quit");
           private JButton Cash = new JButton("Cash Slot");

           private JLabel something = new JLabel("Yeah");

         private String mPin= "";
         private String Transfer= "";
         private int Transfer1;
         private int state = WAIT_FOR_CARD;

         public ATMPanel()
         {
              setLayout(new BorderLayout());

              mScreen.setEditable(false);

              ActionListener keyPadListener = new ActionListener()
              {
                   public void actionPerformed(ActionEvent e)
                   {
                        key(Integer.parseInt(((Button) e.getSource()).getLabel()));
                   }
              };

            // create keypad with 10 buttons

              JPanel keypad= new JPanel(new GridLayout(0,3));
              for(int i=1; i<10; i++)
              {
                   Button btn= new Button(String.valueOf(i));

                    // add keyPadListener as action listener to each button, the listener will get called when a button is pressed

                   btn.addActionListener(keyPadListener);
                   keypad.add(btn);
              }

              keypad.add(new Label());
              Button btn= new Button("0");
              btn.addActionListener(keyPadListener);
              keypad.add(btn);

              mEnter.addActionListener(new ActionListener()
              {
                   public void actionPerformed(ActionEvent e)
                   {
                            enter();
                       }
                });

              mClear.addActionListener(new ActionListener()
              {
                   public void actionPerformed(ActionEvent e)
                   {
                            clear();
                       }
                });

              mCancel.addActionListener(new ActionListener()
              {
                   public void actionPerformed(ActionEvent e)
                   {
                            cancel();
                       }
                 });

                 Receipt.addActionListener(new ActionListener()
                 {
                      public void actionPerformed(ActionEvent e)
                      {
                               receipt();
                       }
                 });

                 Quit.addActionListener(new ActionListener()
                 {
                      public void actionPerformed(ActionEvent e)
                      {
                           quit();
                      }
                 });

                 Cash.addActionListener(new ActionListener()
                 {
                      public void actionPerformed(ActionEvent e)
                      {
                               cash();
                         }
                 });

               // add enter, clear, receeipt, quit, and cancel buttons to panel

                 Panel controls= new Panel(new GridLayout(2,3));
              controls.add(mEnter);
              controls.add(mClear);
              controls.add(mCancel);

                 controls.add(Receipt);
                 controls.add(Quit);
              controls.add(Cash);

              add("North",  mScreen);
              add("Center", keypad);
              add("South",   controls);

              mScreen.setText("Enter your card and press a number key.");
         }

         private void key(int key)
         {
             // called (by action listener) whenever a number button is pressed

             switch (state)
             {
                 case WAIT_FOR_CARD:     // waiting for card

                       // clear screen, and ask user to enter pin

                       clear();
                       mScreen.setText(
                            "Card accepted.\n" +
                            "Good evening Mr Singh.\n" +
                            "Please enter your pin...");
                       state = WAIT_FOR_PIN;
                       break;
                 case WAIT_FOR_PIN:      // waiting for pin

                      // accept number as part of pin

                      if (mPin.length() == 0)
                             mScreen.setText("");
                                mPin += String.valueOf(key);
                                mScreen.setText(mScreen.getText() +"*");
                      break;
                 case MENU_DISPLAYED:

                      // treat number as menu selection

                      switch(key)
                      {
                           case 1:
                             TransMoney();
                                break;
                           case 2:
                             DispenseMoney();
                                break;
                           case 3:
                             CheckBalance();
                                break;
                           default:
                             InvalidOption();
                      }
                      break;
                 case TRANSFER_MONEY:
                           // treat number as part of amount to transfer

                           if(Transfer.length() == 0)
                               mScreen.setText("");
                               Transfer += String.valueOf(key);
                               mScreen.setText(mScreen.getText() + key );

                          break;
                 case DISBURSE_MONEY:
                           // treat number as part of amount to disburse
                          if(Transfer.length() == 0)
                                  mScreen.setText("");
                                  Transfer += String.valueOf(key);
                                mScreen.setText(mScreen.getText() + key );

                           break;
                 case CHECK_BALANCE:
                           break;
              }
         }

            // called (by action listener) whenever a enter button is pressed


         private void enter()
         {
           if (state == WAIT_FOR_CARD)
              return;

           if (state == WAIT_FOR_PIN)
           {
              // check pin

              if (mPin.equals(PIN))
              {
                  // pin correct, display menu
                  menu();
              }
              else
              {
                  // pin incorrect, display message
                  clear();
                  mScreen.setText("Invalid pin, try again (it's " +PIN +")");
              }
           }

           if (state == TRANSFER_MONEY)
           {
                  // perform transfer

                    Transfer1 = Integer.parseInt(Transfer);
                    accountmoney += Transfer1;

                Transfer = "";
                Transfer1 = 0;
                menu();
           }

           if (state == DISBURSE_MONEY)
           {
                // perform disburse

                Transfer1 = Integer.parseInt(Transfer);
                accountmoney -= Transfer1;

                    Transfer = "";
                    Transfer1 = 0;
                menu();
           }
         }

             // called (by action listener) whenever clear button is pressed

        private void clear()
         {
              if (state == WAIT_FOR_CARD)
                   return;

              if (state == WAIT_FOR_PIN)
              {
                   mScreen.setText("");
                   mPin= "";
              }
         }

             // called (by action listener) whenever cancel button is pressed

        private void cancel()
         {
              menu();
         }

         private void menu()
         {
                // display menu

               state = MENU_DISPLAYED;
              clear();
              mScreen.setText(
                   "1. Transfer money\n" +
                   "2. Dispense money\n" +
                   "3. Check balance");
         }

             // called (by action listener) whenever receipt button is pressed

        private void receipt()
          {
                 // display popuup
                 JOptionPane.showMessageDialog(null, new javax.swing.ImageIcon("receipt.jpg"));
              }

              // called (by action listener) whenever cash button is pressed

            private void cash()
              {
                 // display popuup

                JOptionPane.showMessageDialog(null, new javax.swing.ImageIcon("money.jpg"));
            }

              // called (by action listener) whenever quit button is pressed

          private void quit()
            {
                // quit application

                System.exit(0);
           }

         private void TransMoney()
         {
              clear();
                 mScreen.setText("How much money would you like to transfer? ...");
              state = TRANSFER_MONEY;
         }

         private void DispenseMoney()
         {
              clear();
              mScreen.setText("How much money would you like to take out? ...");
             
              if (Transfer1 > accountmoney)
                 {
                      mScreen.setText("You can't have more money than you already have");
                 }

              state = DISBURSE_MONEY;
         }

         private void CheckBalance()
         {
              state = CHECK_BALANCE;
              clear();
              mScreen.setText("This is the amount of money u have in your account.\n" +
                                  " ---> " + "$"+ accountmoney);
         }

         private void InvalidOption()
         {
              clear();
              mScreen.setText("You have choose the incorrect option. Try again");
         }
    }



    public static void main(String[] argv)
    {
         Frame frame= new Frame("ATM");
         frame.add(new ATMPanel());
         frame.pack();
         frame.setResizable(false);
         frame.setVisible(true);
         frame.addWindowListener(new WindowAdapter()
         {
              public void windowClosing(WindowEvent e)
              {
                      System.exit(0);
                 }
           });
    }
}
Avatar of sciuriware
sciuriware

That's even the problem of professionals:
 "Who did write this shit without sufficient comments?
  ............ ahhh! Maybe it was me last year ....."

;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of kawas
kawas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marcoullisp

ASKER

Thanks! That was really helpful. Hey you get the points and all, I was just wondering how would you summarize the way this program works in a nutshell. Not a whole story line, just something I could have printed out next to me while I am reading your comments. Like a small paragraph, or 2-3 sentences. Something I could read along with while I am looking through the questions.

PKM
You know, there are some really useful notes on java programming (general) and guis in java (swing) at http://www.ugrad.cs.ubc.ca/~cs219/ these notes are old but are what i used when i first created a GUI.

This program basically acts as an ATM machine.
No, I know that it acts as an ATM machine. I mean, well I guess I am just looking for something like a summary of the program, the techniques being used, stuff like that... a follow-on kinda thing. I have a great JAVA book by Horton which I am using for theory.

-PKM
techniques used?
If you can be more specific, i will answer your questions.