Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need comments on a Java program (learning experience)

Posted on 2006-05-02
6
Medium Priority
?
2,086 Views
Last Modified: 2012-08-14
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);
                 }
           });
    }
}
0
Comment
Question by:marcoullisp
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

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

;JOOP!
0
 
LVL 13

Accepted Solution

by:
kawas earned 2000 total points
ID: 16588329
// import libraries that are needed for this application. You can get more insight by viewing the javadoc (google: java class-name-here)
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
{
// when applet is started, this is called automatically by the applet super class
    public void init()
    {
        // add panel to applet - the ATMPanel.

         add(new ATMPanel());
    }

    static class ATMPanel extends Panel
    {
           // setting up a pin that is needed for the atm
            private static String PIN= "3602";
         private int accountmoney = 3400;
         //constants that provide state information for the atm
         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;

         // text area that most likely contains the output from the ATM. Look up on google 'java TextArea' to view javadoc.
         private TextArea mScreen=
              new TextArea("", 6, 45, TextArea.SCROLLBARS_NONE);

         // some buttons
         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; // the current state of the ATM

         public ATMPanel()
         {
 // set  the graphical layout for the applet.
              setLayout(new BorderLayout());
// dont allow the text area to be editable
              mScreen.setEditable(false);

// a listener that acts as a callback function for the keypad. when the keypad is pressed, this is called
              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);

// what to do when the enter button is clicked
              mEnter.addActionListener(new ActionListener()
              {
                   public void actionPerformed(ActionEvent e)
                   {
// call the method enter() when enter is pressed.
                            enter();
                       }
                });

// see enter() - same idea
              mClear.addActionListener(new ActionListener()
              {
                   public void actionPerformed(ActionEvent e)
                   {
                            clear();
                       }
                });
// same idea as enter()
              mCancel.addActionListener(new ActionListener()
              {
                   public void actionPerformed(ActionEvent e)
                   {
                            cancel();
                       }
                 });
// same idea as enter()
                 Receipt.addActionListener(new ActionListener()
                 {
                      public void actionPerformed(ActionEvent e)
                      {
                               receipt();
                       }
                 });
// same idea as enter()
                 Quit.addActionListener(new ActionListener()
                 {
                      public void actionPerformed(ActionEvent e)
                      {
                           quit();
                      }
                 });
// same idea as enter()
                 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.");
         }

// the logic of what to do for various states of the atm when keys are pressed.
         private void key(int key)
         {
             // called (by action listener) whenever a number button is pressed
// switch statement - look it up on google. basically same as an if, else if ... else statement
             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

// what to do when enter is pressed according to various states of the ATM - could have been a switch statement like key()
         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");
         }
    }


// the main method that creates a frame, and adds your panel to it so that you can see it.
    public static void main(String[] argv)
    {
         Frame frame= new Frame("ATM");
         frame.add(new ATMPanel());
         frame.pack();
         frame.setResizable(false);
         frame.setVisible(true);
// what should happen when you close the window? if this is absent, basically the window disappears but the program is still running.
         frame.addWindowListener(new WindowAdapter()
         {
              public void windowClosing(WindowEvent e)
              {
                      System.exit(0);
                 }
           });
    }
}

// what else do you want. the program is nicely laid out so that the logic is readable.
0
 

Author Comment

by:marcoullisp
ID: 16588420
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Expert Comment

by:kawas
ID: 16588451
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.
0
 

Author Comment

by:marcoullisp
ID: 16588511
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
0
 
LVL 13

Expert Comment

by:kawas
ID: 16592034
techniques used?
If you can be more specific, i will answer your questions.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question