Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The ATM class is a mockup of a functional ATM containing
- * check balance, withdraw funds and transfer funds options.
- *
- * Author: Ben Harris
- * Date: September, 2012
- */
- using System;
- using System.Collections.Generic;
- namespace ATM_Assignment_2010
- {
- class ATM
- {
- //Arrays used for storing balance and account name information
- private static readonly List<decimal> Balances = new List<decimal> { 0, 901.45m, 450, 1500, 2000 };
- private static readonly List<string> Accounts = new List<string> { "Blank", "Savings Account", "Debit Account",
- "Credit Account", "Line of Credit Account" };
- private const int SavingsAccount = 1;
- private const int DebitAccount = 2;
- private const int CreditAccount = 3;
- private const int LineOfCredit = 4;
- private const int QuitOption = 0;
- private const int CheckBalanceOption = 1;
- private const int WithdrawOption = 2;
- private const int TransferOption = 3;
- // ------------------- MAIN FUNCTION --------------------- //
- static void Main(string[] args)
- {
- bool finished = false;
- while (!finished)
- {
- finished = MainMenu();
- }
- Console.WriteLine("Thankyou for using BencoBank. Have a nice day.");
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- } // End Main
- // ------------- MENU + CHOICE CHECKING ------------------------- //
- /// <summary>
- /// Prints the main menu of the ATM and waits for a valid choice
- /// before performing it. Entering 0 will exit the program.
- ///
- /// pre: finished boolean in Main method must be set to false
- /// post: gives menu option to Perform method for validation.
- /// </summary>
- /// <returns>Returns a boolean - true if program should quit</returns>
- static bool MainMenu()
- {
- Console.Clear();
- Console.WriteLine("=============================================================");
- Console.WriteLine("Welcome to BencoBank.");
- Console.WriteLine("=============================================================");
- Console.WriteLine("\nPlease choose an option, or press 0 to quit.\n");
- Console.WriteLine("1. Check Balance\n2. Withdrawl\n3. Transfer\n");
- Console.Write("Enter option:");
- var validMenuChoices = new List<int> { 0, 1, 2, 3 };
- int choice;
- while (!int.TryParse(Console.ReadLine(), out choice) && !validMenuChoices.Contains(choice))
- {
- // While the input can't be parsed, and the choice isnt valid
- Console.WriteLine("Bad input, please try again");
- }
- Console.WriteLine("\n");
- if (choice == QuitOption)
- {
- return true;
- }
- Perform(choice);
- return false;
- } // End MainMenu
- /// <summary>
- /// Checks the menu choice selected in MainMenu method to see if it is an acceptable option.
- /// pre: menu selection must be made in MainMenu method.
- /// post: program continues to relevant method for checking, withdrawl or transfer.
- /// </summary>
- /// <param name="choice">Valid </param>
- static void Perform(int choice)
- {
- switch (choice)
- {
- case CheckBalanceOption:
- CheckBalance();
- break;
- case WithdrawOption:
- Withdraw();
- break;
- case TransferOption:
- Transfer();
- break;
- }
- }
- // End of Perform()
- // ------------------ ACCOUNT SELECTION METHODS --------------------- //
- /// <summary>
- /// Lists account menu, used to avoid repetition in other methods.
- /// </summary>
- static void ListAccounts()
- {
- Console.WriteLine("1. Savings\n2. Debit\n3. Credit\n4. Line of Credit\n");
- Console.Write("Enter option:");
- }
- // End of ListAccounts()
- /// <summary>
- /// Used for account selection of CheckBalance and Withdraw methods.
- /// User selects account and method checks if account is an acceptable option,
- /// then returns the account number to previous method.
- /// pre: specific menu action method has been called
- /// post: returns account option to be saved as a variable in action method.
- /// </summary>
- /// <returns>Returns account option to be used as variable in method.</returns>
- static int SelectAccount()
- {
- int account;
- var validAccounts = new List<int> { 1, 2, 3, 4 };
- while (!int.TryParse(Console.ReadLine(), out account) && !validAccounts.Contains(account))
- {
- Console.WriteLine("You have selected an invalid account, {0}. Please choose again.");
- ListAccounts();
- }
- return account;
- }
- // End of SelectAccount()
- // ------------------- CHECK BALANCE --------------------- //
- /// <summary>
- /// Account balance check method, calls SelectAccount and then forwards
- /// that account number to DisplayBalance method to finish the action.
- /// pre: user has selected check balance menu option in MainMenu
- /// post: continues to DisplayBalance method which prints balance information.
- /// </summary>
- static void CheckBalance()
- {
- Console.WriteLine("Please select an account to check the balance, or press 0 to cancel.");
- int account = SelectAccount();
- if (account != QuitOption)
- {
- DisplayBalance(account);
- }
- }
- /// <summary>
- /// Finishes balance checking action by printing balance information and returning to main menu.
- /// pre: Account has been selected by CheckBalance method
- /// post: Returns to main menu upon user key entry.
- /// </summary>
- /// <param name="whichAccount">Given to method by CheckBalance method, provides relevant
- /// account number for balances and accounts array.</param>
- static void DisplayBalance(int whichAccount)
- {
- Console.WriteLine("\n\n\nCurrent Date:" + DateTime.Now + "\n");
- Console.WriteLine("Your {0} contains {1:C} \n", Accounts[whichAccount], Balances[whichAccount]);
- Console.WriteLine("Press any key to return to the main menu.");
- Console.ReadKey();
- Console.Clear();
- }
- // End of DisplayBalance()
- // ------------------- WITHDRAW ---------------------- //
- /// <summary>
- /// Account withdraw method, calls SelectAccount and then forwards
- /// account number to WithdrawAmount method for amount entry and checking.
- /// pre: user has selected the withdraw option from the MainMenu method.
- /// post: account number is handed to WithdrawAmount method, which then
- /// takes a withdraw amount and checks that value.
- /// </summary>
- static void Withdraw()
- {
- Console.WriteLine("Please select an account to withdraw from, or press 0 to cancel.");
- int account = SelectAccount();
- if (account != QuitOption)
- {
- WithdrawAmount(account);
- }
- }
- /// <summary>
- /// Asks user for amount to withdraw, then checks for invalid amounts. If the amount is
- /// a valid amount, withdraws the amount from the accounts balance in the balances array
- /// and a bank fee if applicable, and then hands amount to DispenseCash method.
- /// pre: Account has been selected in Withdraw()
- /// post: amount is handed to DispenseCash to complete withdraw transaction.
- /// </summary>
- /// <param name="whichAccount">
- /// Given to method by Withdraw method, provides relevant account number
- /// for balances and accounts arrays.
- /// </param>
- static void WithdrawAmount(int whichAccount)
- {
- const decimal minimumBalance = 10.0m;
- const decimal bankFee = 0.10m;
- Console.WriteLine("How much would you like to withdraw?\n");
- Console.WriteLine("Enter amount:");
- var validAmount = false;
- while (!validAmount)
- {
- decimal withdrawAmount;
- while (!decimal.TryParse(Console.ReadLine(), out withdrawAmount))
- {
- Console.WriteLine("Please enter a valid dollar value");
- }
- // Checks to see if withdraw will take balance below minimum
- if (withdrawAmount >= (Balances[whichAccount] - minimumBalance))
- {
- Console.WriteLine("\nInsufficient funds. Please try again.\n");
- }
- // Checks for special cases of 10 or 30, and for cent values
- else if (withdrawAmount % 10 != 0 || withdrawAmount == 10 || withdrawAmount == 30)
- {
- Console.WriteLine("Cannot withdraw {0:C} from your account." +
- "This ATM only has $20 and $50 notes.", withdrawAmount);
- }
- else
- {
- validAmount = true;
- }
- //Hands acceptable amount to DispenseCash
- Balances[whichAccount] = Balances[whichAccount] - withdrawAmount;
- if (whichAccount == CreditAccount || whichAccount == DebitAccount)
- {
- Balances[whichAccount] = Balances[whichAccount] - bankFee;
- }
- DispenseCash(withdrawAmount);
- }
- }
- // End of WithdrawAmount
- /// <summary>
- /// Uses division and modulus operators to figure out how many 50 dollar notes and
- /// 20 dollar notes are handed out by the ATM. Prints information and returns to
- /// MainMenu method upon user key entry.
- /// pre: correct amount has been entered into the WithdrawAmount method.
- /// post: returns to MainMenu after method completion.
- /// </summary>
- /// <param name="amount">
- /// Value handed to method by WithdrawAmount methodm specifies how
- /// much the user wants to withdraw from their account.
- /// </param>
- static void DispenseCash(decimal amount)
- {
- int fiftyNotes = 0;
- int twentyNotes = 0;
- int leftoverAmount;
- int dispenseAmount = Convert.ToInt16(amount);
- //Gets amount of 50 dollar notes in amount, then finds leftover amount.
- fiftyNotes = dispenseAmount / 50;
- leftoverAmount = dispenseAmount % 50;
- //If leftover is 10 or 30, removes one 50 dollar note and adds 50 dollars to leftover
- //so it can be given in 20 dollar notes.
- if (leftoverAmount != 0 && leftoverAmount == 10 || leftoverAmount == 30)
- {
- leftoverAmount = leftoverAmount + 50;
- fiftyNotes = fiftyNotes - 1;
- }
- //Gets amount of 20 dollar notes in leftover amount.
- twentyNotes = leftoverAmount / 20;
- leftoverAmount = leftoverAmount % 20;
- Console.WriteLine("\n\nThe current date is : {0}\n", DateTime.Now);
- Console.WriteLine("{0:C0} successfully withdrawn. " +
- "Please take your notes from the ATM.\n", amount);
- Console.WriteLine("{0} $20 notes expected\n{1} $50 notes expected.\n", twentyNotes, fiftyNotes);
- Console.WriteLine("Press any key to return to the main menu.");
- Console.ReadKey();
- Console.Clear();
- }
- // End of DispenseCash
- // -------------------- TRANSFER ----------------------- //
- /// <summary>
- /// Method for transfer option. Selects to and from accounts, checks if they are different,
- /// then forwards both account selections to the TransferAmount method for completion of
- /// transfer action.
- /// pre: Transfer has been selected in MainMenu method.
- /// post: Two account values handed to TransferAmount method for completion.
- /// </summary>
- static void Transfer()
- {
- int accountFrom = 0;
- int accountTo = 0;
- Console.WriteLine("Please select an account to transfer from, or press 0 to cancel.");
- accountFrom = SelectAccount();
- if (accountFrom == QuitOption)
- {
- MainMenu();
- }
- else
- {
- Console.WriteLine("Please select an account to transfer to, or press 0 to cancel.");
- accountTo = SelectAccount();
- }
- if (accountTo == QuitOption)
- {
- MainMenu();
- }
- else if (accountFrom == accountTo)
- {
- Console.WriteLine("You have selected the same account. Please try again.");
- Transfer();
- }
- else
- {
- TransferAmount(accountFrom, accountTo);
- }
- }
- /// <summary>
- /// Completes transfer action. User enters a value which is then checked for invalid amounts,
- /// if amount is valid, completes transfer and charges bank fees where applicable, then
- /// returns to MainMenu method upon user key entry.
- /// pre: two valid accounts have been selected from Transfer method
- /// post: prints completed transfer information and returns to main menu upon user key entry.
- /// </summary>
- /// <param name="fromAccount">Account to transfer money from, recieved from Transfer method</param>
- /// <param name="toAccount">Account to transfer money to, recieved from Transfer method</param>
- static void TransferAmount(int fromAccount, int toAccount)
- {
- decimal amount = 0;
- decimal bankFee = 0.50m;
- decimal minimumBalance = 10.0m;
- Console.WriteLine("How much would you like to transfer? Enter amount or press 0 to cancel.");
- Console.Write("Enter amount: ");
- amount = Convert.ToDecimal(Console.ReadLine());
- if (amount == 0)
- {
- MainMenu();
- }
- // Checks to see if transfer would take account below minimum balance.
- else if (amount > Convert.ToDecimal(Balances[fromAccount]) - minimumBalance)
- {
- Console.WriteLine("Insufficient funds, please choose again.");
- TransferAmount(fromAccount, toAccount);
- }
- // If not below minimum balance, completes transfer and subtracts fee.
- else
- {
- Balances[fromAccount] = ((Balances[fromAccount] - amount) - bankFee);
- Balances[toAccount] = (Balances[toAccount] + amount);
- Console.WriteLine("new balance for {0} is {1}",
- Accounts[fromAccount], Balances[fromAccount]);
- Console.WriteLine("new balance for {0} is {1}",
- Accounts[toAccount], Balances[toAccount]);
- }
- } // End TransferAmount()
- }
- }
Add Comment
Please, Sign In to add comment