Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace atm
- {
- // =======================================
- // Author: Sam Pourzinal
- // Author ID: n8321744
- // =======================================
- class ATM
- {
- const int SAVING_ACCOUNT = 1;
- const int DEBIT_CARD = 2;
- const int CREDIT_CARD = 3;
- const int LINE_OF_CREDIT = 4;
- static double[] accountBalances = { 0.0, 901.45, 450.0, 1500.0, 2000.0 };
- static string[] accountTypes = { "", "Savings Account", "Debit Card", "Credit Card",
- "Line of Credit Account" };
- static void Main(string[] args)
- {
- // Allows the program to continue running through the use of a while loop.
- //Until you clean up the code with Environment.Exit(0) the program will continue running.
- while (true)
- {
- // Prints the main menu selections.
- Console.WriteLine("Welcome to Sam's ATM Simulator \n Transaction Menu");
- Console.WriteLine(" ================\n 1. Check Balance\n 2. Withdrawal");
- Console.Write(" 3. Transfer\n\n\nPlease Enter your choice 1 ... 3 or 0 to exit: ");
- // Collects the users input and calls the method pertaining to your choice.
- string input = Console.ReadLine();
- //I use else if statements as they allow me to
- // a) made the codes execution faster by not running unecessary conditionals
- // b) so I can use one else statement to relate to multiple if statements.
- if (input == "1") CheckBalanceMain();
- else if (input == "2") WithdrawalMain();
- else if (input == "3") TransferMain();
- else if (input == "0") Environment.Exit(0);
- //This else statement is simply some minor error-handling, it does nothing apart from
- //notifying the user that they need to re-enter their input.
- else Console.WriteLine("You pressed an invalid character! Try again.\n\n");
- }
- }
- static void CheckBalanceMain()
- {
- // Prints the account selections.
- Console.WriteLine("\n\n 1. Savings Account\n 2. Debit Card\n 3. Credit Card");
- Console.Write(" 4. Line of Credit\n\nPlease Enter your choice 1 ... 4 or 0 to return: ");
- // Collects the users input and converts it to an int, so we can use it to reference an array element.
- string input = Console.ReadLine();
- int whichAccount = Convert.ToInt32(input);
- // Returns the user to the main menu if you input 0.
- if (whichAccount > 0)
- DisplayBalance(whichAccount);
- else Main(new String[0]);
- }
- // Precondition whichAccount
- static void DisplayBalance(int whichAccount)
- {
- // Calls the Date method.
- DateTime date = Date();
- // Prints the balance of the account then returns the user to Main.
- Console.WriteLine("\n\nBalance of {0} is {1}\n",
- accountTypes[whichAccount], accountBalances[whichAccount]);
- Main(new String[0]);
- }
- static DateTime Date()
- {
- // Prints the current date and time.
- Console.WriteLine();
- DateTime date = DateTime.Now;
- Console.WriteLine("Current Date: {0} ", date);
- return date;
- //As a Post-Condition, we return date, just incase we need to use it later.
- //Post-Condition - Date must be a valid DateTime variable.
- }
- static void WithdrawalMain()
- {
- // Prints the selection of accounts.
- Console.WriteLine("\n\n 1. Savings Account\n 2. Debit Card\n 3. Credit Card");
- Console.Write(" 4. Line of Credit\n\nPlease Enter your choice 1 ... 4 or 0 to return:");
- // Reads the users input and converts it to an int.
- string input = Console.ReadLine();
- int whichAccount = Convert.ToInt32(input);
- // If user inputed 0, returns to Main.
- if (whichAccount > 0) WithdrawAmount(whichAccount);
- }
- // Precondition - whichAccount must be a valid integer
- static void WithdrawAmount(int whichAccount)
- {
- // Asks for an amount and converts the input to a double.
- Console.Write("Please enter amount to withdraw: ");
- double amount = Convert.ToDouble(Console.ReadLine());
- // Checks if amount is a a multiple of 10, and is not 10 or 30 (because you can have
- //any odd multiple of 10, but ONLY if it above 30
- if (amount % 10 == 0 & amount != 10 & amount != 30)
- { //Below ensures that we will have enough money to do the withdraw.
- if ((accountBalances[whichAccount]) - amount < 10)
- {
- Console.WriteLine("\nThere are insufficient funds in {0}", accountTypes[whichAccount]);
- WithdrawAmount(whichAccount);
- }
- else
- {
- if (accountTypes[whichAccount] == "Debit Card" || accountTypes[whichAccount] == "Credit Card")
- //This applies the 10 cent fee to your account.
- accountBalances[whichAccount] -= 0.10;
- accountBalances[whichAccount] -= amount; DispenseCash(amount, whichAccount);
- }
- }
- else
- {
- //Since we're calling this again we dont want the arithmatic to be calculated twice
- Console.WriteLine(" {0} cannot be dispensed.", amount); WithdrawAmount(whichAccount);
- }
- }
- // Precondition - amount must be a valid double, whichAccount must be a valid int
- static void DispenseCash(double amount, int whichAccount)
- {
- // Calculates the number of $50 notes required.
- // We convert it to an int in order to remove its decimal place.
- int numlargenotes = (int)amount / 50;
- //a temporary variable, as we can only declare a variable in an acceptable scope
- int numsmallnotes = 0;
- // Calculates the number of notes in the case that 3 $20 notes are required.
- // i.e. $110 is 1 $50 note, 3 $20 notes.
- if (amount % 50 == 10 || amount % 50 == 30)
- {
- numlargenotes = numlargenotes - 1;
- numsmallnotes = (int)(amount - (50 * numlargenotes)) / 20;
- }
- else
- numsmallnotes = (int)amount % 50 / 20;
- // Prints the amount of notes required for collection.
- Console.WriteLine("\nPlease collect ${0} consisting of:\n{1} $50.00 notes and {2} $20.00 notes."
- , amount, numlargenotes, numsmallnotes);
- // Calls the Date method to print the date.
- DateTime date = Date();
- // Prints the final balance of the account.
- Console.WriteLine("Balance of {0} is {1}.\n",
- accountTypes[whichAccount], accountBalances[whichAccount]);
- }
- static void TransferMain()
- {
- // Prints the account selection menu and asks which account.
- Console.WriteLine("\n 1. Savings Account\n 2. Debit Card\n 2. Debit Card");
- Console.WriteLine(" 3. Credit Card\n 4. Line of Credit\n");
- Console.WriteLine("Transfer from which account? ");
- Console.Write("Please Enter your choice 1 ... 4 or 0 to return: ");
- // Converts the users input into an int.
- int fromAccount = Convert.ToInt32(Console.ReadLine());
- // Asks which account to trasnfer to and converts input into int.
- Console.WriteLine("\nTransfer to which account?");
- Console.Write("Please Enter your choice 1 ... 4 or 0 to return: ");
- int toAccount = Convert.ToInt32(Console.ReadLine());
- // Checks if user has inputed two different accounts.
- if (fromAccount > 0 & toAccount > 0)
- if (fromAccount == toAccount) Console.WriteLine("Must select two different accounts");
- else TransferAmount(fromAccount, toAccount);
- }
- // Precondition - fromAccount must be a valid integer, toAccount must be a valid integer
- static void TransferAmount(int fromAccount, int toAccount)
- {
- // Asks user amount to transfer and converts input to double.
- Console.Write("Please enter amount to Transfer: ");
- string input = Console.ReadLine();
- double amount = Convert.ToDouble(input);
- // Checks if the transfer will leave the account with less than $10.
- if (accountBalances[fromAccount] - accountBalances[toAccount] < 10)
- Console.WriteLine("\nInsufficient Funds to complete transaction\n");
- // If funds available proceeds with the transaction and substitutes 50c.
- else
- {
- accountBalances[fromAccount] = accountBalances[fromAccount] - amount - 0.5;
- accountBalances[toAccount] = accountBalances[toAccount] + amount;
- Console.WriteLine("Transaction was successful.\n");
- // Prints the date and the balance of both accounts.
- DateTime date = Date();
- Console.WriteLine("Balance of {0} is {1}", accountTypes[fromAccount],
- accountBalances[fromAccount]);
- Console.WriteLine("Balance of {0} is {1}", accountTypes[toAccount],
- accountBalances[toAccount]);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment