?
Solved

Requesting a commenting out of a Java program

Posted on 2006-05-01
8
Medium Priority
?
508 Views
Last Modified: 2008-02-01
Hi... I am a grad student trying to learn Java on the fly. I came across this interesting Java program in this forum on Java, and was hoping one of the experts here could comment the code out for me so I can sit down tonite and look over the code, and maybe learn alot from it. The program is an ATM simulation.

I don't need every line of code commented out, just enough to get some like me who needs help with medium sized programming skills to understand whats going on...

Thanks,

PKM

Code follows in a seperate dialog box...

0
Comment
Question by:marcoullisp
  • 5
  • 2
8 Comments
 

Author Comment

by:marcoullisp
ID: 16576221
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(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()));
                   }
              };

              JPanel keypad= new JPanel(new GridLayout(0,3));
              for(int i=1; i<10; i++)
              {
                   Button btn= new Button(String.valueOf(i));
                   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();
                         }
                 });

                 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)
         {
             switch (state)
             {
                 case WAIT_FOR_CARD:     // waiting for card
                       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
                      if (mPin.length() == 0)
                             mScreen.setText("");
                                mPin += String.valueOf(key);
                                mScreen.setText(mScreen.getText() +"*");
                      break;
                 case MENU_DISPLAYED:
                      switch(key)
                      {
                           case 1:
                             TransMoney();
                                break;
                           case 2:
                             DispenseMoney();
                                break;
                           case 3:
                             CheckBalance();
                                break;
                           default:
                             InvalidOption();
                      }
                      break;
                 case TRANSFER_MONEY:
                           if(Transfer.length() == 0)
                               mScreen.setText("");
                               Transfer += String.valueOf(key);
                               mScreen.setText(mScreen.getText() + key );

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

                           break;
                 case CHECK_BALANCE:
                           break;
              }
         }

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

           if (state == WAIT_FOR_PIN)
           {
              if (mPin.equals(PIN))
              {
                  menu();
              }
              else
              {
                  clear();
                  mScreen.setText("Invalid pin, try again (it's " +PIN +")");
              }
           }

           if (state == TRANSFER_MONEY)
           {
                    Transfer1 = Integer.parseInt(Transfer);
                    accountmoney += Transfer1;

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

           if (state == DISBURSE_MONEY)
           {
                Transfer1 = Integer.parseInt(Transfer);
                accountmoney -= Transfer1;

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

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

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

         private void cancel()
         {
              menu();
         }

         private void menu()
         {

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

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

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

            private void quit()
            {
                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
 

Author Comment

by:marcoullisp
ID: 16576225
Also when I try and run this in eclipse I get the error message:

"Warning: Can't read AppletViewer properties file: /home/Panos/.hotjava/properties Using defaults."

Please to translate...

PKM
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16576254
Its basically an ATM application that displays an applet and gives you various options. When you click on 1, it allows you to transfer money. When you click on 2, it allows you to withdraw cash. When you click on 3, it allows you to display your current balance.

>> Can't read AppletViewer properties file: /home/Panos/.hotjava/properties Using defaults

Does it run after that warning?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 16576268
should get u started:

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
 

Author Comment

by:marcoullisp
ID: 16576273
Yes it runs fine after that warning. What I am looking for is not the general overview, but the commenting out of the code, so I can follow the logic.

-PKM
0
 

Author Comment

by:marcoullisp
ID: 16576284
objects: thanks... with a little more commenting I should be all set to follow the logic. I think you might of assumed I am more Java-savvy than I really am. Maybe dumb it down just a little more please so I can follow the logic better?

-PKM
0
 
LVL 92

Expert Comment

by:objects
ID: 16576312
which bits specifically do you not understand I'll try and explain them better.
0
 

Author Comment

by:marcoullisp
ID: 16576415
Well, you might think I am lazy, but I just want the major bits commented out. I tried reading through this and I can't follow the logic... I get lost. Your commenting is a little sporadic... for a novice like me a little more is needed to understand what is going on...

-PKM
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 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