//Programmer: Bo Bayles  Date: 28 September 2004
//File: lab6.cpp         Class: CS54 C
//Purpose: This program simulates an ATM machine. It gives the user a menu
//  for depositing, withdrawing, and displaying balance until the user
//  chooses to exit.

#include <iostream>
using namespace std;

int main()
{
  float fBalance = 0, fChange = 0;
  int iChoice = 0;
  //Floats for the balance, which will need a decimal point.

  cout.setf (ios::showpoint);
  cout.precision (2);
  cout.setf (ios::fixed);
  //Set it to display decimal points for whole numbers, 2 places, and in
  //  fixed notation.

  cout << endl << "Welcome to Lab 6 ATM." << endl
       << "Enter your account's initial value: $";
  do
  {
    cin >> fBalance;
    if (fBalance <= 0)
      cout << endl << "Please enter a value greater than $0.00: ";
  }
  while (fBalance <= 0);
  //Make sure balance isn't negative.

  do
  {
	cout << endl << "Select a transaction type." << endl;

	cout << "1. Make a deposit." << endl
	     << "2. Make a withdrawl." << endl
	     << "3. Report account balance." << endl
	     << "4. Exit." << endl
	     << "Selection (1 - 4): ";
    cin >> iChoice;

    switch  (iChoice)
    {
      case 1:
      //Deposit branch.
            cout << endl << "Enter the deposit amount: $";

            do
            {
              cin >> fChange;
              if (fChange <= 0)
                cout << endl << "Please enter a positive amount to deposit: ";
	        }
	        while (fChange <= 0);
	        //Ask user until they enter an appropriate value.

	        cout << endl << "Previous balance: $" << fBalance << endl
	             << "Deposit amount : $" << fChange << endl;
	        fBalance = fBalance + fChange;
	        cout << "New balance: $" << fBalance << endl;

            break;
            //Break out of switch to repeat the loop.

      case 2:
            cout << endl << "Enter the withdrawl amount: $";
            //Withdrawl branch

            do
            {
              cin >> fChange;
              if (fChange < 0)
                cout << endl << "Please enter a positive amount to withdraw: ";
		    }
		    while (fChange < 0);
		    //Ask the user until they enter an appropriate value.

		    if (fChange > fBalance)
            {
              cout << endl << "Cannot withdrawl more than $" << fBalance
                   << endl;

              break;
		    }
			//Don't allow a withdrawl larger than the balance.

	        cout << endl << "Previous balance: $" << fBalance << endl
	             << "Withdrawl amount: $" << fChange << endl;
	        fBalance = fBalance - fChange;
	        cout << "New balance: $" << fBalance << endl;

	        break;
	        //Break out of switch to repeat the loop.

      case 3:
            cout << endl << "Your account's balance is: $" << fBalance << endl;

            break;
            //Break out of switch to repeat the loop.

      case 4:
            cout << endl << "Exiting..." << endl;

            break;

      default:
              cout << "Please enter a number between 1 and 4." << endl;

              break;
              //Break out of switch to repeat the loop.
      }
  }
  while (iChoice != 4);

  return 0;
}

