
/**
 *  ALTERNATIVE USER INPUT This is a banking program that relies on the methods
 *  in the BankAccount class. It performs basic bank account functions.
 *
 *@author
 *@created    May 15, 2003
 *@version    11-07-2002
 */
public class Banking {
	/**
	 *  The main program for the Banking class
	 *
	 *@param  args  The command line arguments
	 */
	public static void main(String[] args) {
		String codes = "DWITG";
		BankAccount C = new BankAccount();
		// Declare all new bank accounts
		BankAccount P = new BankAccount();
		BankAccount S = new BankAccount();
		BankAccount L = new BankAccount();
		C.ownersName = "Jeffrey Gardner";
		P.ownersName = "Jeffrey Gardner";
		// Declare owners name for all bank accounts
		S.ownersName = "Jeffrey Gardner";
		L.ownersName = "Jeffrey Gardner";
		C.accountNumber = 1001;
		P.accountNumber = 1002;
		S.accountNumber = 1003;
		L.accountNumber = 1004;
		System.out.println("\tWARNING! Invalid transaction type terminates program!");
		// Sets sentinel stop value.
		System.out.println("\t_____________________________________________________");
		for (int x = 0; x < 25; x++) {
			int type1;
			int transType;
			int type2 = 0;
			double amount;
			boolean cheater = false;
			// Local variables.
			String actionTaken = "";
			String acct1 = "";
			String acct2 = "";
			ConsoleReader cr = new ConsoleReader(System.in);
			System.out.print("\n\tPlease enter a valid transaction type. (D, W, I, T or G): ");
			transType = cr.readChar();
			// Read transaction type
			String z = cr.readLine();
			//Overcome shortcomings in ConsoleReader
			if (codes.indexOf(transType) < 0) {
				break;
			}
			System.out.print("\tPlease enter the amount: ");
			amount = cr.readDouble();
			// Get amount of transaction
			System.out.print("\tPlease enter the account type, (C, P, S, or L): ");
			type1 = cr.readChar();
			z = cr.readLine();
			if (transType == 'T') {
				System.out.print("\tWhich account do you want to transfer funds to? ");

				type2 = cr.readChar();
			}
			if (transType == 'D') {
				// Deposit transaction
				actionTaken = (actionTaken + "deposited");
				if (type1 == 'C') {
					C.deposit(amount);
					acct1 = (acct1 + "checking");
				}
				if (type1 == 'P') {
					P.deposit(amount);
					acct1 = (acct1 + "primary savings");
				}
				if (type1 == 'S') {
					S.deposit(amount);
					acct1 = (acct1 + "student loan repayment");
				}
				if (type1 == 'L') {
					L.deposit(amount);
					acct1 = (acct1 + "car loan repayment");
				}
			}
			if (transType == 'W') {
				//Withdrawal transaction.
				actionTaken = (actionTaken + "withdrew");

				if (type1 == 'C') {
					acct1 = "checking";
					if (amount <= C.getBalance()) {
						C.withdraw(amount);
					} else {
						P.withdraw(amount - C.getBalance() + 15);
						C.withdraw(C.getBalance());
						cheater = true;
					}
				}

				if (type1 == 'P') {
					acct1 = "primary savings";
					if (amount <= P.getBalance()) {
						P.withdraw(amount);
					} else {
						System.out.println("\n\t\tInsufficient Funds.");
						cheater = true;
					}
				}
				if (type1 == 'S') {
					acct1 = "student loan";
					if (amount <= S.getBalance()) {
						S.withdraw(amount);
					} else {
						System.out.println("\n\t\tInsufficient Funds.");
						cheater = true;
					}
				}
				if (type1 == 'L') {
					acct1 = "car loan repayment";
					if (amount <= L.getBalance()) {
						L.withdraw(amount);
					} else {
						System.out.println("\n\t\tInsufficient Funds.");
						cheater = true;
					}
				}
			}
			if (transType == 'I') {
				//Interest payment transaction
				actionTaken = (actionTaken + "collected");

				if ((type1 == 'P') && (P.getBalance() >= 500)) {
					double temp = (P.getBalance() * (amount / 100));
					P.deposit(temp);
					acct1 = "primary savings";
				}

				if ((type1 == 'S') && (S.getBalance() >= 500)) {
					double temp = (S.getBalance() * (amount / 100));
					S.deposit(temp);
					acct1 = "student loan";
				}

				if ((type1 == 'L') && (L.getBalance() >= 500)) {
					double temp = (L.getBalance() * (amount / 100));
					L.deposit(temp);
					acct1 = "car loan repayment";
				}

			}
			if (transType == 'T') {
				//Transfer transaction
				actionTaken = (actionTaken + "transferred");
				if (type1 == 'C') {
					if (type2 == 'P') {
						C.withdraw(amount);
						P.deposit(amount);
						acct1 = "checking";
						acct2 = "primary savings";
					}
					if (type2 == 'S') {
						C.withdraw(amount);
						S.deposit(amount);
						acct1 = "checking";
						acct2 = "student loan repayment";
					}
					if (type2 == 'L') {
						C.withdraw(amount);
						L.deposit(amount);
						acct1 = "checking";
						acct2 = "car loan repayment";
					}
				}
				if (type1 == 'P') {
					if (type2 == 'S') {
						P.withdraw(amount);
						S.deposit(amount);
						acct1 = "primary savings";
						acct2 = "student loan repayment";
					}
					if (type2 == 'L') {
						P.withdraw(amount);
						L.deposit(amount);
						acct1 = "primary savings";
						acct2 = "car loan repayment";
					}
					if (type2 == 'C') {
						P.withdraw(amount);
						C.deposit(amount);
						acct1 = "primary savings";
						acct2 = "checking";
					}
				}
				if (type1 == 'S') {
					if (type2 == 'P') {
						S.withdraw(amount);
						P.deposit(amount);
						acct1 = "student loan repayment";
						acct2 = "primary savings";
					}
					if (type2 == 'C') {
						S.withdraw(amount);
						C.deposit(amount);
						acct1 = "student loan repayment";
						acct2 = "checking";
					}
					if (type2 == 'L') {
						S.withdraw(amount);
						L.deposit(amount);
						acct1 = "student loan repayment";
						acct2 = "car loan repayment";
					}
				}
				if (type1 == 'L') {
					if (type2 == 'P') {
						L.withdraw(amount);
						P.deposit(amount);
						acct1 = "car loan repayment";
						acct2 = "primary savings";
					}
					if (type2 == 'S') {
						L.withdraw(amount);
						S.deposit(amount);
						acct1 = "car loan repayment";
						acct2 = "student loan repayment";
					}
					if (type2 == 'C') {
						L.withdraw(amount);
						C.deposit(amount);
						acct1 = "car loan repayment";
						acct2 = "checking";
					}
				}
			}
			if (transType == 'G') {
				// Get balance transaction
				actionTaken = (actionTaken + "got");
				if (type1 == 'C') {
					double temp = C.getBalance();
					System.out.println("\n\tYour balance in checking is $" + temp);
				}
				if (type1 == 'P') {
					double temp = P.getBalance();
					System.out.println("\n\tYour balance in primary savings is $" + temp);
				}
				if (type1 == 'S') {
					double temp = S.getBalance();
					System.out.println("\n\tYour balance in student loan account is $" + temp);
				}
				if (type1 == 'L') {
					double temp = L.getBalance();
					System.out.println("\n\tYour balance in car loan account is $" + temp);
				}
			}
			action(actionTaken, amount, acct1, acct2, cheater);
			//Sends call to print actions taken in transaction.

		}
		System.out.println("\n\n\t\t\tAccount Information");
		System.out.println("\nAccount Number\t\t\tName\t\tBalance");
		System.out.println("   " + C.accountNumber + "\t\t\t  " + C.ownersName + "\t  " + C.getBalance());
		System.out.println("   " + P.accountNumber + "\t\t\t  " + P.ownersName + "\t  " + P.getBalance());
		System.out.println("   " + S.accountNumber + "\t\t\t  " + S.ownersName + "\t  " + S.getBalance());
		System.out.println("   " + L.accountNumber + "\t\t\t  " + L.ownersName + "\t  " + L.getBalance());
	}


	/**
	 *  Description of the Method
	 *
	 *@param  actionTaken  Description of the Parameter
	 *@param  amount       Description of the Parameter
	 *@param  acct1        Description of the Parameter
	 *@param  acct2        Description of the Parameter
	 *@param  cheater      Description of the Parameter
	 */
	public static void action(String actionTaken, double amount, String acct1, String acct2, boolean cheater) {
		//Separate transaction method.
		if ((cheater == true) && (acct1.equals("checking"))) {
			System.out.println("\n\tYou TRIED to overdraw your account! Your primary savings \n\taccount has been debited $15.00 and the difference was covered.");
		}
		if (actionTaken.equals("withdrew")) {
			System.out.println("\n\tYou " + actionTaken + " $" + amount + " from your " + acct1 + " account.");
		}

		if (actionTaken.equals("deposited")) {
			System.out.println("\n\tYou " + actionTaken + " $" + amount + " into your " + acct1 + " account.");
		}
		if (actionTaken.equals("got")) {
			System.out.println("\n\tYou " + actionTaken + " the balance in your " + acct1 + " account.");
		}
		if (actionTaken.equals("transferred")) {
			System.out.println("\n\tYou " + actionTaken + " $" + amount + " from " + acct1 + " to " + acct2 + ".");
		}
		if (actionTaken.equals("collected")) {
			if (cheater == false) {
				System.out.println("\n\tYou " + actionTaken + amount + " percent interest on your " + acct1 + " account.");
			} else if (cheater == true) {
				System.out.println("\n\tInterest not paid - insufficient balance.");
			}
		}
	}
}
