[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

Need comments on a Java program (learning experience)

Asked by marcoullisp in Java Programming Language

Tags: java, program, atm

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);
                 }
           });
    }
}
Related Solutions: class diagram
[+][-]05/02/06 10:13 AM, ID: 16588329Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Java Programming Language
Tags: java, program, atm
Sign Up Now!
Solution Provided By: kawas
Participating Experts: 2
Solution Grade: A
 
[+][-]05/02/06 10:13 AM, ID: 16588320Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/02/06 10:21 AM, ID: 16588420Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/02/06 10:25 AM, ID: 16588451Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/02/06 10:30 AM, ID: 16588511Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05/02/06 05:25 PM, ID: 16592034Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93