Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

program that impliments an atm machine. The program should consist of three clas

ID: 3758993 • Letter: P

Question

program that impliments an atm machine. The program should consist of three classes. The first class should define the GUI and should be hand-coded and not generated by a GUI generator. In addition to the main method and a constructor to build the GUI, event handlers will be needed to handle each of the four buttons shown above. When the Withdraw button is clicked, several checks must be made. The first check is to ensure the value in the text field is numeric. Next a check must be made to ensure the amount is in increments of $20. At that point an attempt to withdraw the funds is made from the account selected by the radio buttons. The attempt might result in an exception being thrown for insufficient funds, If any of those three errors occur a JOptionPane window should be displayed explaining the error. Otherwise a window should be displayed confirming that the withdrawal has succeeded. When the Deposit button is clicked the only necessary check is to ensure that the amount input in the textfield is numeric. Clicking the Transfer button signifies transferring funds to the selected account from the other account. The checks needed are to confirm that the amount supplied is numeric and that there are sufficient funds in the account from which the funds are being transferred. Clicking the Balance button will cause a JOptionPane window to be displayed showing the current balance in the selected account. The main class must contain two Account objects, one for the checking account and another for the savings account. The second class is Account.It must have a constructor plus a method that corresponds to each of the four buttons in the GUI. It must also incorporate logic to deduct a service charge of $1.50 when more than four total withdrawals are made from either account. Note that this means, for example, if two withdrawals are made from the checking and two from the savings, any withdrawal from either account thereafter incurs the service charge. The method that performs the withdrawals must throw an InsufficientFunds exception whenever an attempt is made to withdraw more funds than are available in the account. Note that when service charges apply, there must also be sufficient funds to pay for that charge. The third class is InsufficientFunds, which is a user defined checked exception. Be sure to follow good programming style, which means making all instance and class variables private, naming all constants and avoiding the duplication of code. Furthermore you must select enough scenarios to completely test the program.

Explanation / Answer

package atm;

import java.awt.*;

import atm.atmparts.*;

import atm.util.Status;

import atm.util.Money;

public class ATM

{

    public ATM(int number, String location, Bank bank, Container container)

      {

        super();

       _number = number;

        _location = location;

        _bank = bank;

       _cardReader = new CardReader();

        _display = new Display();

        _keyboard = new Keyboard();

        _cashDispenser = new CashDispenser();

        _envelopeAcceptor = new EnvelopeAcceptor();

        _receiptPrinter = new ReceiptPrinter();

        _operatorPanel = new OperatorPanel();

        GUILayout.doLayout(container,

                           _cardReader, _display, _keyboard, _cashDispenser,

                           _envelopeAcceptor, _receiptPrinter, _operatorPanel);

      }

    public synchronized Money startupOperation()

      {

        while (! _operatorPanel.switchOn())

            try

              { Thread.sleep(1000); }

            catch (InterruptedException e)

              { }

        _state = RUNNING;

        return _operatorPanel.getInitialCash();

      }

    public void serviceCustomers(Money initialCash)

      {

        _cashDispenser.setCash(initialCash);

        while (_state == RUNNING)

          {

            int readerStatus = CardReader.NO_CARD; // Initialization needed only

                                                    // to keep the compiler happy!

            _display.requestCard();

            do

              {

                if (! _operatorPanel.switchOn())

                    _state = STOPPED;

                else

                    readerStatus = _cardReader.checkForCardInserted();

              }

            while (_state == RUNNING && readerStatus == CardReader.NO_CARD);

            _display.clearDisplay();

           

            if (_state == RUNNING)

              switch (readerStatus)

                {

                  case CardReader.CARD_HAS_BEEN_READ:     

                    {

                      Session session = new Session(_cardReader.cardNumber(),

                                                    this,

                                                    _bank);

                      session.doSessionUseCase();

                      break;

                    }

                  case CardReader.UNREADABLE_CARD:

                    _display.reportCardUnreadable();

                    _cardReader.ejectCard();

                    _display.clearDisplay();

                }

          }

      }

    public int getPIN()

      { _display.requestPIN();

        int PIN = _keyboard.readPIN(_display);

        _display.clearDisplay();

        return PIN;

      }

    public int getMenuChoice(String whatToChoose,

                      int numItems,

                      String items[])

    { _display.displayMenu(whatToChoose, numItems, items);

        int choice = _keyboard.readMenuChoice(numItems);

        _display.clearDisplay();

        return choice;

      }

    public Money getAmountEntry()

      { _display.requestAmountEntry();

        Money amount = _keyboard.readAmountEntry(_display);

        _display.clearDisplay();

        return amount;

      }

    public boolean checkIfCashAvailable(Money amount)

      { return ! _cashDispenser.currentCash().less(amount);

      }

  

    public void dispenseCash(Money amount)

      { _cashDispenser.dispenseCash(amount);

      }

        public boolean acceptEnvelope()

      { return _envelopeAcceptor.acceptEnvelope();

      }

   

    public void issueReceipt(int cardNumber,

                             int serialNumber,

                             String description,

                             Money amount,

                             Money balance,

                             Money availableBalance)

      {

        _receiptPrinter.printReceipt(_number, _location, cardNumber, serialNumber,

                                     description, amount,

                                     balance, availableBalance);;

      }

        public int reEnterPIN()

      { _display.requestReEnterPIN();

        int PIN = _keyboard.readPIN(_display);

        _display.clearDisplay();

        return PIN;

      }

   

    public boolean reportTransactionFailure(String explanation)

      { _display.reportTransactionFailure(explanation);

        int response = _keyboard.readMenuChoice(2);

        _display.clearDisplay();

        return response == 1;

      }

       

    public void ejectCard()

      {

       _cardReader.ejectCard();

      }

        public void retainCard()

      {

     _display.reportCardRetained();

        _cardReader.retainCard();

        _display.clearDisplay();

      }

        public int number()

      { return _number; }

    private int              _number;

    private String           _location;

    private Bank             _bank;

    private static final int RUNNING = 0;

    private static final int STOPPED = 1;

    private int              _state;

    private CardReader       _cardReader;

    private Display          _display;

    private Keyboard         _keyboard;

    private CashDispenser    _cashDispenser;

    private EnvelopeAcceptor _envelopeAcceptor;

    private ReceiptPrinter   _receiptPrinter;

    private OperatorPanel    _operatorPanel;

}