Advertisement
cska1312

BANK ACCOUNT

Apr 17th, 2022 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class BankAccount
  5. {
  6.     public string name;
  7.     public double balance = 0;
  8.     public BankAccount(string namеe, double firstDep)
  9.     {
  10.         name = namеe;
  11.         balance += firstDep;
  12.     }
  13.  
  14.     public double getBalance()
  15.     {
  16.         return balance;
  17.     }
  18.  
  19.     public string getName()
  20.     {
  21.         return this.name;
  22.     }
  23.  
  24.     public void deposit(double addSum)
  25.     {
  26.         balance += addSum;
  27.     }
  28.  
  29.     public bool withdraw(double outSum)
  30.     {
  31.         bool chk = true;
  32.         if (outSum <= balance)
  33.         {
  34.             balance -= outSum;
  35.         }
  36.         else if (outSum > balance)
  37.         {
  38.             chk = false;
  39.         }
  40.         return chk;
  41.     }
  42. }
  43.  
  44. class Program
  45. {
  46.     static void Main(string[] args)
  47.     {
  48.         List<BankAccount> bank = new List<BankAccount>();//създаваме лист
  49.  
  50.         while (true)
  51.         {
  52.           Console.WriteLine("Please select a function!");
  53.             Console.Write("1. Create an account\n2. Check balance\n3. Deposit\n" +
  54.                           "4. Withdraw\n0. Exit\n\nSelect Function: ");
  55.  
  56.             string function = Console.ReadLine().ToUpper();
  57.  
  58.             Console.WriteLine("\n");
  59.  
  60.             if (function == "1")//ако функцията е 1 въведи име и първоначален баланс
  61.             {
  62.                 Console.Write("Enter name: ");
  63.                 string name = Console.ReadLine();
  64.                 Console.Write("Enter initial balance: ");
  65.                 double balance = double.Parse(Console.ReadLine());
  66.                 if(balance < 0)//ако баланса е < от 0 върни грешка и приключи
  67.                 {
  68.                     Console.WriteLine("Error");
  69.                     break;
  70.                 }
  71.                 bank.Add(new BankAccount(name, balance));// добави акаунта с баланса и върни съобщение
  72.  
  73.                 Console.WriteLine("Bank Account Added!");
  74.               continue;
  75.             }
  76.  
  77.             else if (function == "2")//ако функцията е 2 въведи име
  78.             {
  79.                 Console.WriteLine("Enter name: ");
  80.                 string nameCheck = Console.ReadLine();
  81.  
  82.                 for (int y = 0; y < bank.Count; y++)
  83.                 {
  84.                     if (bank[y].name == nameCheck)//ако името съвпада с текущите запазени върни името с баланса
  85.                     {
  86.                         Console.WriteLine("Account Found! \nName: {0} \nBalance: {1}", bank[y].name, bank[y].balance);
  87.                        
  88.                     }
  89.                 }
  90.                 continue;
  91.             }
  92.  
  93.             else if (function == "3")//ако функцията е 3 въведи име
  94.             {
  95.                 Console.Write("Enter name: ");
  96.                 string PersonName = Console.ReadLine(), nmNow = null;
  97.                 int accNum = -1;
  98.  
  99.                 for (int y = 0; y < bank.Count; y++)
  100.                 {
  101.                     if (bank[y].name == PersonName)//ако името съвпада с текущите запазени добави сумата за депозит
  102.                     {
  103.                         nmNow = PersonName;
  104.                         accNum = y;
  105.                     }
  106.                 }
  107.                 if (accNum != -1)
  108.                 {
  109.                     Console.Write("Amount to Deposit: ");
  110.                     bank[accNum].deposit(double.Parse(Console.ReadLine()));//добави въведената сума към текущата и прибави
  111.                     Console.WriteLine("Amount successfully deposited!");
  112.                   continue;
  113.                 }
  114.                 else
  115.                 {
  116.                     Console.WriteLine("Account not found!");
  117.                 }
  118.  
  119.                 Console.ReadLine();
  120.             }
  121.  
  122.  
  123.             else if (function == "4")//ако командата е 4 прочети име
  124.             {
  125.                 Console.Write("Enter name: ");
  126.                 string nameCheck = Console.ReadLine(), nameNow = null;
  127.                 int accNum = -1;
  128.  
  129.                 for (int y = 0; y < bank.Count; y++)
  130.                 {
  131.                     if (bank[y].name == nameCheck)//ако името съвпада с текущите запазени имена попитай за сума за теглене
  132.                     {
  133.                         nameNow = nameCheck;
  134.                         accNum = y;
  135.                     }
  136.                 }
  137.  
  138.                  if (accNum != -1)
  139.                 {
  140.                     Console.Write("Amount to Withdraw: ");
  141.                     bool ok = bank[accNum].withdraw(double.Parse(Console.ReadLine()));//извади текущата сума от въведената и запази
  142.                    if (bank[accNum].balance == 0)
  143.                         {
  144.                             bank[accNum] = null;
  145.                             Console.WriteLine("Account closed!");
  146.                           break;
  147.                         }
  148.                     if (ok)
  149.                     {
  150.                         Console.WriteLine("Amount successfully withdrawn!");
  151.                         continue;
  152.                     }
  153.                     else
  154.                     {
  155.                         Console.WriteLine("Insufficient funds!");
  156.                       break;
  157.                     }
  158.                 }
  159.                 else
  160.                 {
  161.                     Console.WriteLine("Account not found!");
  162.                 }
  163.                 Console.ReadLine();
  164.             }
  165.             else if (function == "0")
  166.             {
  167.                 break;
  168.             }
  169.             else
  170.             {
  171.                 Console.WriteLine("Invalid Syntax!");
  172.                 Console.ReadLine();
  173.             }
  174.             Console.Clear();
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement