/******************************************************************
CSCI 240 Assignment 5 - Fall 2001

Programmer: Matt Hartman
Section:    19
GA: 	      Shannon Boedewig
Date Due:   10/05/01

Purpose:    This program promts the user to go into their checking,
savings accounts, and for their balance in savings and checking.  The
program also promts the user to withdrawal or deposit either from their
savings or checking accounts.  The program will then output the checking
and savings balances. The program will also not allow the user to
withdrawal more than whats in the account as well.  This program is
made of functions to make the program easier to read.
*******************************************************************/
#include<stdio.h>
#include<cslib.h>

#define withdrawfee 1.50

string promptAndGetLine(string);
double promptAndGetReal(string);

double processCheckWithdrawal(double);
double processSavingsWithdrawal(double);
double processCheckDeposit(double);
double processSavingsDeposit(double);


/**********************************************************************

Name:	      main
Use:        declares integers, bank information, also displays
				balances of checking and savings, and also asks if the
				user wants to do another transaction.
Arguments:  none
Return:     none
Notes:      none
**********************************************************************/

void main()

{
  double check, saving;
  string choice;

  check = 0;
  saving = 0;

  clrscr();
  printf(" *** ACME BANK ATM *** \n\n");

  do
	{
	  choice = promptAndGetLine("\nChoose C)hecking S)avings B)alance: ");

	  if (StringEqual (choice, "C") || StringEqual (choice, "c"))
		  {
				choice = promptAndGetLine("\nW)ithdrawal or D)eposit? ");

			if (StringEqual (choice, "W") || StringEqual (choice, "w"))

					check = processCheckWithdrawal(check);

			else if (StringEqual (choice, "D") || StringEqual (choice, "d"))

					check = processCheckDeposit(check);

			else
					printf("\nInvalid Choice ");
		  }


	  else if (StringEqual (choice, "S") || StringEqual (choice, "s"))
		  {
				 choice = promptAndGetLine("\nW)ithdrawal or D)eposit? ");


			if (StringEqual (choice, "W") || StringEqual (choice, "w"))

				 saving = processSavingsWithdrawal(saving);


			else if (StringEqual (choice, "D") || StringEqual (choice, "d"))

				 saving = processSavingsDeposit(saving);

			else
				 printf("\nInvalid Choice");

		  }
	  else if (StringEqual (choice, "B") || StringEqual (choice, "b"))
		  {

			printf("Checking Balance is: %10.2f\n", check);
			printf("Savings Balance is:  %10.2f\n", saving);
		  }

	  else
			printf("\nInvalid Choice ");


			printf("\n\nAnother? (y/n) ");
			choice = GetLine();

	}
	while(StringEqual (choice, "Y") || StringEqual (choice, "y"));
		 printf("\nThank you for using ACME Bank ATM!\n");
		 printf("Press <Enter> to quit");
		 choice = GetLine();

}



/**********************************************************************

Name:	      promptAndGetLine
Use:        determines what the user wants to do
Arguments:  1. input should be either s, S, B, b, C, and c
Return:     if the user chooses B or b the balance is given.  if the
				user chooses S, s, C, or c the user is prompted with
				promtAndGetReal
Notes:      none
**********************************************************************/

string promptAndGetLine(string s)
{
string choice;
printf("%s", s);
choice = GetLine();
return choice;
}

/**********************************************************************

Name:       promptAndGetReal
Use:        makes sure teh amount deposited or withdrawn is a number
Arguments:  1. must be a number
				2. can't be negative
Return:     enters amount into balance or deducts from balance
Notes:      none
**********************************************************************/

double promptAndGetReal(string s)
{
	double answer;

	printf("%s", s);
	answer = GetReal();
	return answer;
}

/**********************************************************************

Name:	      processCheckWithdrawal
Use:        takes money out of the checking account
Arguments:  1. can't take out more money that what is in the account
Return:     takes money out of the checking account
Notes:      none
**********************************************************************/

double processCheckWithdrawal(double bal)
{
	double amount;
	amount = promptAndGetReal("Enter amount to withdrawal from checking: ");

	if (amount > bal)
	{
		printf("\nRequested amount (%10.2f) exceeds balance (%10.2f)", amount, bal);
		printf("\nPlease deposit funds or try a smaller withdrawal.");
	}
	else
	{
		bal -= amount + withdrawfee;
		return bal;
	}
}
/**********************************************************************

Name:	      processSavingsWithdrawal
Use:        takes money out of the savings account
Arguments:  1. can't take more money than what is in the savings account
Return:     takes money our of the checking account
Notes:      none
**********************************************************************/

double processSavingsWithdrawal(double bal)
{
	double amount;
	amount = promptAndGetReal("Enter amount to withdrawal from savings: ");

	if (amount > bal)
	{
		printf("\nRequested amount (%10.2f) exceeds balance (%10.2f)", amount, bal);
		printf("\nPlease deposit funds or try a smaller withdrawal.");
	}
	else
	{
		bal -= (amount + withdrawfee);
		return bal;
	}

}

/**********************************************************************

Name:	      processCheckDeposit
Use:        deposit money into checking account
Arguments:  1. must be a number
Return:     enters money into checking account
Notes:      none
**********************************************************************/

double processCheckDeposit(double bal)
{
	 double amount;
	 amount = promptAndGetReal("Enter amount to deposit to checking: ");
	 bal += amount;
	 return bal;
}

/**********************************************************************

Name:	      processSavingsDeposit
Use:        deposit money into savings account
Arguments:  1. must be a number
Return:     enters money into savings account
Notes:      none
**********************************************************************/

double processSavingsDeposit(double bal)
{
	double amount;
	amount = promptAndGetReal("Enter amount to deposit to savings: ");
	bal += amount;
	return bal;
}

