Advertisement
KingAesthetic

C# Example 1

Aug 13th, 2024 (edited)
122
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. // Kaizer here. I created a banking system, which allows you to create your own account, withdraw money, input account numbers, initial balance, and so on and so forth.
  5.  
  6. // class to represent a bank account
  7. class Account
  8. {
  9.     private string accountNumber;
  10.     private string accountHolder;
  11.     private double balance;
  12.  
  13.     // constructor for initializing account details
  14.     public Account(string accNumber, string accHolder, double initialBalance)
  15.     {
  16.         accountNumber = accNumber;
  17.         accountHolder = accHolder;
  18.         balance = initialBalance;
  19.     }
  20.  
  21.     // function to find an already existing account number
  22.     public string GetAccountNumber()
  23.     {
  24.         return accountNumber;
  25.     }
  26.  
  27.     // function to deposit money into the account
  28.     public void Deposit(double amount)
  29.     {
  30.         if (amount > 0)
  31.         {
  32.             balance += amount;
  33.             Console.WriteLine($"Deposited: {amount}");
  34.         }
  35.         else
  36.         {
  37.             Console.WriteLine("Invalid deposit amount.");
  38.         }
  39.     }
  40.  
  41.     // function to withdraw money from the account
  42.     public void Withdraw(double amount)
  43.     {
  44.         if (amount > 0 && amount <= balance)
  45.         {
  46.             balance -= amount;
  47.             Console.WriteLine($"Withdrew: {amount}");
  48.         }
  49.         else
  50.         {
  51.             Console.WriteLine("Invalid withdrawal amount or insufficient funds.");
  52.         }
  53.     }
  54.  
  55.     // function to check the account balance
  56.     public double GetBalance()
  57.     {
  58.         return balance;
  59.     }
  60.  
  61.     // function to display account details
  62.     public void Display()
  63.     {
  64.         Console.WriteLine($"Account Number: {accountNumber}");
  65.         Console.WriteLine($"Account Holder: {accountHolder}");
  66.         Console.WriteLine($"Balance: {balance}");
  67.     }
  68. }
  69.  
  70. // public Class to manage multiple accounts
  71. class Bank
  72. {
  73.     private List<Account> accounts = new List<Account>();
  74.  
  75.     // function to create a new account
  76.     public void CreateAccount(string accNumber, string accHolder, double initialBalance)
  77.     {
  78.         accounts.Add(new Account(accNumber, accHolder, initialBalance));
  79.         Console.WriteLine("Account created successfully.");
  80.     }
  81.  
  82.     // function to find an account by account number
  83.     public Account FindAccount(string accNumber)
  84.     {
  85.         return accounts.Find(account => account.GetAccountNumber() == accNumber);
  86.     }
  87.  
  88.     // function to display all accounts
  89.     public void DisplayAllAccounts()
  90.     {
  91.         foreach (var account in accounts)
  92.         {
  93.             account.Display();
  94.             Console.WriteLine("-------------------");
  95.         }
  96.     }
  97. }
  98.  
  99. class Program
  100. {
  101.     static void Main(string[] args)
  102.     {
  103.         Bank bank = new Bank();
  104.         int choice;
  105.         string accNumber, accHolder;
  106.         double amount;
  107.  
  108.         do
  109.         {
  110.             Console.WriteLine("1. Create Account");
  111.             Console.WriteLine("2. Deposit");
  112.             Console.WriteLine("3. Withdraw");
  113.             Console.WriteLine("4. Check Balance");
  114.             Console.WriteLine("5. Display All Accounts");
  115.             Console.WriteLine("6. Exit");
  116.             Console.Write("Enter your choice: ");
  117.             choice = int.Parse(Console.ReadLine());
  118.  
  119.             switch (choice)
  120.             {
  121.                 case 1:
  122.                     Console.Write("Enter account number: ");
  123.                     accNumber = Console.ReadLine();
  124.                     Console.Write("Enter account holder name: ");
  125.                     accHolder = Console.ReadLine();
  126.                     Console.Write("Enter initial balance: ");
  127.                     amount = double.Parse(Console.ReadLine());
  128.                     bank.CreateAccount(accNumber, accHolder, amount);
  129.                     break;
  130.                 case 2:
  131.                     Console.Write("Enter account number: ");
  132.                     accNumber = Console.ReadLine();
  133.                     Account account = bank.FindAccount(accNumber);
  134.                     if (account != null)
  135.                     {
  136.                         Console.Write("Enter amount to deposit: ");
  137.                         amount = double.Parse(Console.ReadLine());
  138.                         account.Deposit(amount);
  139.                     }
  140.                     else
  141.                     {
  142.                         Console.WriteLine("Account not found.");
  143.                     }
  144.                     break;
  145.                 case 3:
  146.                     Console.Write("Enter account number: ");
  147.                     accNumber = Console.ReadLine();
  148.                     account = bank.FindAccount(accNumber);
  149.                     if (account != null)
  150.                     {
  151.                         Console.Write("Enter amount to withdraw: ");
  152.                         amount = double.Parse(Console.ReadLine());
  153.                         account.Withdraw(amount);
  154.                     }
  155.                     else
  156.                     {
  157.                         Console.WriteLine("Account not found.");
  158.                     }
  159.                     break;
  160.                 case 4:
  161.                     Console.Write("Enter account number: ");
  162.                     accNumber = Console.ReadLine();
  163.                     account = bank.FindAccount(accNumber);
  164.                     if (account != null)
  165.                     {
  166.                         Console.WriteLine($"Balance: {account.GetBalance()}");
  167.                     }
  168.                     else
  169.                     {
  170.                         Console.WriteLine("Account not found.");
  171.                     }
  172.                     break;
  173.                 case 5:
  174.                     bank.DisplayAllAccounts();
  175.                     break;
  176.                 case 6:
  177.                     Console.WriteLine("Exiting...");
  178.                     break;
  179.                 default:
  180.                     Console.WriteLine("Invalid choice. Please try again.");
  181.                     break;
  182.             }
  183.         } while (choice != 6);
  184.     }
  185. }
  186.  
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement