Guest User

Untitled

a guest
Nov 16th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.39 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.     class ATM {
  8.  
  9.         const int SAVING_ACCOUNT = 1;
  10.         const int DEBIT_CARD = 2;
  11.         const int CREDIT_CARD = 3;
  12.         const int LINE_OF_CREDIT = 4;
  13.         static double[] accountBalances = { 0.0, 901.45, 450.0, 1500.0, 2000.0 };
  14.         static string[] accountTypes = { "", "Savings Account", "Debit Card", "Credit Card",
  15.                                        "Line of Credit Account" };
  16.  
  17.         static void Main(string[] args) {
  18.             while (true) {
  19.                 Console.WriteLine("Welcome to Sam's ATM Simulator \n       Transaction Menu");
  20.                 Console.WriteLine("       ================\n       1. Check Balance\n       2. Withdrawal");
  21.                 Console.Write("       3. Transfer\n\n\nPlease Enter your choice 1 ... 3 or 0 to exit:  ");
  22.                 string input = Console.ReadLine();
  23.                 if (input == "1") CheckBalanceMain();
  24.                 else if (input == "2") WithdrawalMain();
  25.                 else if (input == "3") TransferMain();
  26.                 else if (input == "0") Environment.Exit(0);
  27.                 else Console.WriteLine("You pressed an invalid character! Try again.\n\n");
  28.  
  29.             }
  30.         }
  31.  
  32.         static void CheckBalanceMain() {
  33.             Console.WriteLine("\n\n       1. Savings Account\n       2. Debit Card\n       3. Credit Card");
  34.             Console.Write("       4. Line of Credit\n       \nPlease Enter your choice 1 ... 4 or 0 to return:  ");
  35.             string input = Console.ReadLine();
  36.             int whichAccount = Convert.ToInt32(input);
  37.             if (whichAccount > 0) DisplayBalance(whichAccount);
  38.             else Main(new String[0]);
  39.         }
  40.  
  41.  
  42.         static void DisplayBalance(int whichAccount) {
  43.             Date();
  44.             Console.WriteLine("\n\nBalance of {0} is {1}\n",
  45.                               accountTypes[whichAccount], accountBalances[whichAccount]);
  46.             Main(new String[0]);
  47.         }
  48.  
  49.         static void Date() {
  50.             DateTime date = DateTime.Now;
  51.             Console.WriteLine("Current Date: {0} ", date);
  52.         }
  53.  
  54.         static void WithdrawalMain() {
  55.             Console.WriteLine("\n\n       1. Savings Account\n       2. Debit Card\n       3. Credit Card");
  56.             Console.Write("       4. Line of Credit\n       \nPlease Enter your choice 1 ... 4 or 0 to return:  ");
  57.             string input = Console.ReadLine();
  58.             int whichAccount = Convert.ToInt32(input);
  59.             if (whichAccount > 0) WithdrawAmount(whichAccount);
  60.         }
  61.  
  62.         static void WithdrawAmount(int whichAccount) {
  63.             Console.WriteLine("Please enter amount to withdraw: "); double amount = Convert.ToDouble(Console.ReadLine());
  64.             // 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
  65.             if (amount % 10 == 0 & amount != 10 & amount != 30)
  66.             {
  67.                 if ((accountBalances[whichAccount]) - amount < 10) {
  68.                     Console.WriteLine("There are insufficient funds in {0}", accountTypes[whichAccount]); WithdrawAmount(whichAccount);
  69.                 } else {
  70.                     if (accountTypes[whichAccount] == "Debit Card" || accountTypes[whichAccount] == "Credit Card") accountBalances[whichAccount] -= 0.10;
  71.                     accountBalances[whichAccount] -= amount; DispenseCash(amount, whichAccount);
  72.                 }
  73.             } else {
  74.                 //since we're calling this again we dont want the arithmatic to be calculated twice
  75.                 Console.WriteLine(" {0} cannot be dispensed.", amount); WithdrawAmount(whichAccount);
  76.                
  77.             }
  78.         }
  79.  
  80.         static void DispenseCash(double amount, int whichAccount) {
  81.             int numlargenotes = (int)amount / 50;
  82.             int numsmallnotes = 0;
  83.             //a temporary variable, as we can't declare it in an if statement
  84.             if (amount % 50 == 10 || amount % 50 == 30) {
  85.                 numlargenotes = numlargenotes - 1;
  86.                 numsmallnotes = (int)(amount - (50 * numlargenotes)) / 20;
  87.             } else
  88.                 numsmallnotes = (int)amount % 50 / 20;
  89.  
  90.             Console.WriteLine("\nPlease collect ${0} consisting of:\n{1} $50.00 notes and {2} $20.00 notes.\n",
  91.                             amount, numlargenotes, numsmallnotes);
  92.             Date();
  93.             Console.WriteLine("Balance of {0} is {1}.\n", accountTypes[whichAccount], accountBalances[whichAccount]);
  94.         }
  95.  
  96.         static void TransferMain() {
  97.             Console.WriteLine("\n       1. Savings Account\n       2. Debit Card\n       2. Debit Card");
  98.             Console.WriteLine("       3. Credit Card\n       4. Line of Credit\n");
  99.             Console.WriteLine("Transfer from which account?   ");
  100.             Console.Write("Please Enter your choice 1 ... 4 or 0 to return:  ");
  101.             int fromAccount = Convert.ToInt32(Console.ReadLine());
  102.             Console.WriteLine("\nTransfer to which account?");
  103.             Console.Write("Please Enter your choice 1 ... 4 or 0 to return:  ");
  104.             int toAccount = Convert.ToInt32(Console.ReadLine());
  105.             if (fromAccount > 0 & toAccount > 0)
  106.                 if (fromAccount == toAccount) Console.WriteLine("Must select two different accounts");
  107.                 else TransferAmount(fromAccount, toAccount);
  108.         }
  109.  
  110.         static void TransferAmount(int fromAccount, int toAccount) {
  111.             Console.Write("Please enter amount to Transfer: ");
  112.             string input = Console.ReadLine();
  113.             double amount = Convert.ToDouble(input);
  114.             if (accountBalances[fromAccount] - accountBalances[toAccount] < 10)
  115.                 Console.WriteLine("Insufficient Funds to complete transaction");
  116.             else {
  117.                 accountBalances[fromAccount] = accountBalances[fromAccount] - amount - 0.5;
  118.                 accountBalances[toAccount] = accountBalances[toAccount] + amount;
  119.                 Console.WriteLine("Transaction was successful.\n");
  120.                 Date();
  121.                 Console.WriteLine("Balance of {0} is {1}", accountTypes[fromAccount], accountBalances[fromAccount]);
  122.                 Console.WriteLine("Balance of {0} is {1}", accountTypes[toAccount], accountBalances[toAccount]);
  123.             }
  124.  
  125.         }
  126.     }
  127. }
Add Comment
Please, Sign In to add comment