Guest User

Untitled

a guest
Nov 17th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace atm
  7. {
  8.     // =======================================
  9.     // Author: Sam Pourzinal
  10.     // Author ID: n8321744
  11.     // =======================================
  12.  
  13.     class ATM
  14.     {
  15.  
  16.         const int SAVING_ACCOUNT = 1;
  17.         const int DEBIT_CARD = 2;
  18.         const int CREDIT_CARD = 3;
  19.         const int LINE_OF_CREDIT = 4;
  20.         static double[] accountBalances = { 0.0, 901.45, 450.0, 1500.0, 2000.0 };
  21.         static string[] accountTypes = { "", "Savings Account", "Debit Card", "Credit Card",
  22.                                        "Line of Credit Account" };
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             // Allows the program to continue running through the use of a while loop.
  27.             //Until you clean up the code with Environment.Exit(0) the program will continue running.
  28.  
  29.             while (true)
  30.             {
  31.                 // Prints the main menu selections.
  32.                 Console.WriteLine("Welcome to Sam's ATM Simulator \n       Transaction Menu");
  33.                 Console.WriteLine("       ================\n       1. Check Balance\n       2. Withdrawal");
  34.                 Console.Write("       3. Transfer\n\n\nPlease Enter your choice 1 ... 3 or 0 to exit:  ");
  35.                 // Collects the users input and calls the method pertaining to your choice.
  36.                 string input = Console.ReadLine();
  37.                 //I use else if statements as they allow me to
  38.                        // a) made the codes execution faster by not running unecessary conditionals
  39.                        // b) so I can use one else statement to relate to multiple if statements.
  40.                 if (input == "1") CheckBalanceMain();
  41.                 else if (input == "2") WithdrawalMain();
  42.                 else if (input == "3") TransferMain();
  43.                 else if (input == "0") Environment.Exit(0);
  44.                 //This else statement is simply some minor error-handling, it does nothing apart from
  45.                 //notifying the user that they need to re-enter their input.
  46.                 else Console.WriteLine("You pressed an invalid character! Try again.\n\n");
  47.  
  48.             }
  49.         }
  50.  
  51.         static void CheckBalanceMain()
  52.         {
  53.             // Prints the account selections.
  54.             Console.WriteLine("\n\n       1. Savings Account\n       2. Debit Card\n       3. Credit Card");
  55.             Console.Write("       4. Line of Credit\n\nPlease Enter your choice 1 ... 4 or 0 to return: ");
  56.             // Collects the users input and converts it to an int, so we can use it to reference an array element.
  57.             string input = Console.ReadLine();
  58.             int whichAccount = Convert.ToInt32(input);
  59.             // Returns the user to the main menu if you input 0.
  60.             if (whichAccount > 0)
  61.                 DisplayBalance(whichAccount);
  62.             else Main(new String[0]);
  63.         }
  64.  
  65.         // Precondition whichAccount
  66.         static void DisplayBalance(int whichAccount)
  67.         {
  68.             // Calls the Date method.
  69.             DateTime date = Date();
  70.             // Prints the balance of the account then returns the user to Main.
  71.             Console.WriteLine("\n\nBalance of {0} is {1}\n",
  72.                               accountTypes[whichAccount], accountBalances[whichAccount]);
  73.             Main(new String[0]);
  74.         }
  75.  
  76.         static DateTime Date()
  77.         {
  78.             // Prints the current date and time.
  79.             Console.WriteLine();
  80.             DateTime date = DateTime.Now;
  81.             Console.WriteLine("Current Date: {0} ", date);
  82.             return date;
  83.             //As a Post-Condition, we return date, just incase we need to use it later.
  84.             //Post-Condition - Date must be a valid DateTime variable.
  85.         }
  86.  
  87.         static void WithdrawalMain()
  88.         {
  89.             // Prints the selection of accounts.
  90.             Console.WriteLine("\n\n       1. Savings Account\n       2. Debit Card\n       3. Credit Card");
  91.             Console.Write("       4. Line of Credit\n\nPlease Enter your choice 1 ... 4 or 0 to return:");
  92.             // Reads the users input and converts it to an int.
  93.             string input = Console.ReadLine();
  94.             int whichAccount = Convert.ToInt32(input);
  95.             // If user inputed 0, returns to Main.
  96.             if (whichAccount > 0) WithdrawAmount(whichAccount);
  97.         }
  98.  
  99.         // Precondition - whichAccount must be a valid integer
  100.         static void WithdrawAmount(int whichAccount)
  101.         {
  102.             // Asks for an amount and converts the input to a double.
  103.             Console.Write("Please enter amount to withdraw: ");
  104.             double amount = Convert.ToDouble(Console.ReadLine());
  105.             // Checks if amount is a a multiple of 10, and is not 10 or 30 (because you can have
  106.             //any odd multiple of 10, but ONLY if it above 30
  107.             if (amount % 10 == 0 & amount != 10 & amount != 30)
  108.             {   //Below ensures that we will have enough money to do the withdraw.
  109.                 if ((accountBalances[whichAccount]) - amount < 10)
  110.                 {
  111.                     Console.WriteLine("\nThere are insufficient funds in {0}", accountTypes[whichAccount]);
  112.                     WithdrawAmount(whichAccount);
  113.                 }
  114.                 else
  115.                 {
  116.                     if (accountTypes[whichAccount] == "Debit Card" || accountTypes[whichAccount] == "Credit Card")
  117.                         //This applies the 10 cent fee to your account.
  118.                         accountBalances[whichAccount] -= 0.10;
  119.                     accountBalances[whichAccount] -= amount; DispenseCash(amount, whichAccount);
  120.                 }
  121.             }
  122.             else
  123.             {
  124.                 //Since we're calling this again we dont want the arithmatic to be calculated twice
  125.                 Console.WriteLine(" {0} cannot be dispensed.", amount); WithdrawAmount(whichAccount);
  126.  
  127.             }
  128.         }
  129.  
  130.         // Precondition - amount must be a valid double, whichAccount must be a valid int
  131.         static void DispenseCash(double amount, int whichAccount)
  132.         {
  133.             // Calculates the number of $50 notes required.
  134.             // We convert it to an int in order to remove its decimal place.
  135.             int numlargenotes = (int)amount / 50;
  136.             //a temporary variable, as we can only declare a variable in an acceptable scope
  137.             int numsmallnotes = 0;
  138.             // Calculates the number of notes in the case that 3 $20 notes are required.
  139.             // i.e. $110 is 1 $50 note, 3 $20 notes.
  140.             if (amount % 50 == 10 || amount % 50 == 30)
  141.             {
  142.                 numlargenotes = numlargenotes - 1;
  143.                 numsmallnotes = (int)(amount - (50 * numlargenotes)) / 20;
  144.             }
  145.             else
  146.                 numsmallnotes = (int)amount % 50 / 20;
  147.             // Prints the amount of notes required for collection.
  148.             Console.WriteLine("\nPlease collect ${0} consisting of:\n{1} $50.00 notes and {2} $20.00 notes."
  149.                             , amount, numlargenotes, numsmallnotes);
  150.             // Calls the Date method to print the date.
  151.             DateTime date = Date();
  152.             // Prints the final balance of the account.
  153.             Console.WriteLine("Balance of {0} is {1}.\n",
  154.                 accountTypes[whichAccount], accountBalances[whichAccount]);
  155.         }
  156.  
  157.         static void TransferMain()
  158.         {
  159.             // Prints the account selection menu and asks which account.
  160.             Console.WriteLine("\n       1. Savings Account\n       2. Debit Card\n       2. Debit Card");
  161.             Console.WriteLine("       3. Credit Card\n       4. Line of Credit\n");
  162.             Console.WriteLine("Transfer from which account?   ");
  163.             Console.Write("Please Enter your choice 1 ... 4 or 0 to return:  ");
  164.             // Converts the users input into an int.
  165.             int fromAccount = Convert.ToInt32(Console.ReadLine());
  166.             // Asks which account to trasnfer to and converts input into int.
  167.             Console.WriteLine("\nTransfer to which account?");
  168.             Console.Write("Please Enter your choice 1 ... 4 or 0 to return:  ");
  169.             int toAccount = Convert.ToInt32(Console.ReadLine());
  170.             // Checks if user has inputed two different accounts.
  171.             if (fromAccount > 0 & toAccount > 0)
  172.                 if (fromAccount == toAccount) Console.WriteLine("Must select two different accounts");
  173.                 else TransferAmount(fromAccount, toAccount);
  174.         }
  175.  
  176.         // Precondition - fromAccount must be a valid integer, toAccount must be a valid integer
  177.         static void TransferAmount(int fromAccount, int toAccount)
  178.         {
  179.             // Asks user amount to transfer and converts input to double.
  180.             Console.Write("Please enter amount to Transfer: ");
  181.             string input = Console.ReadLine();
  182.             double amount = Convert.ToDouble(input);
  183.             // Checks if the transfer will leave the account with less than $10.
  184.             if (accountBalances[fromAccount] - accountBalances[toAccount] < 10)
  185.                 Console.WriteLine("\nInsufficient Funds to complete transaction\n");
  186.             // If funds available proceeds with the transaction and substitutes 50c.
  187.             else
  188.             {
  189.                 accountBalances[fromAccount] = accountBalances[fromAccount] - amount - 0.5;
  190.                 accountBalances[toAccount] = accountBalances[toAccount] + amount;
  191.                 Console.WriteLine("Transaction was successful.\n");
  192.                 // Prints the date and the balance of both accounts.
  193.                 DateTime date = Date();
  194.                 Console.WriteLine("Balance of {0} is {1}", accountTypes[fromAccount],
  195.                     accountBalances[fromAccount]);
  196.                 Console.WriteLine("Balance of {0} is {1}", accountTypes[toAccount],
  197.                     accountBalances[toAccount]);
  198.             }
  199.  
  200.         }
  201.     }
  202. }
Add Comment
Please, Sign In to add comment