Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- // 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.
- // class to represent a bank account
- class Account
- {
- private string accountNumber;
- private string accountHolder;
- private double balance;
- // constructor for initializing account details
- public Account(string accNumber, string accHolder, double initialBalance)
- {
- accountNumber = accNumber;
- accountHolder = accHolder;
- balance = initialBalance;
- }
- // function to find an already existing account number
- public string GetAccountNumber()
- {
- return accountNumber;
- }
- // function to deposit money into the account
- public void Deposit(double amount)
- {
- if (amount > 0)
- {
- balance += amount;
- Console.WriteLine($"Deposited: {amount}");
- }
- else
- {
- Console.WriteLine("Invalid deposit amount.");
- }
- }
- // function to withdraw money from the account
- public void Withdraw(double amount)
- {
- if (amount > 0 && amount <= balance)
- {
- balance -= amount;
- Console.WriteLine($"Withdrew: {amount}");
- }
- else
- {
- Console.WriteLine("Invalid withdrawal amount or insufficient funds.");
- }
- }
- // function to check the account balance
- public double GetBalance()
- {
- return balance;
- }
- // function to display account details
- public void Display()
- {
- Console.WriteLine($"Account Number: {accountNumber}");
- Console.WriteLine($"Account Holder: {accountHolder}");
- Console.WriteLine($"Balance: {balance}");
- }
- }
- // public Class to manage multiple accounts
- class Bank
- {
- private List<Account> accounts = new List<Account>();
- // function to create a new account
- public void CreateAccount(string accNumber, string accHolder, double initialBalance)
- {
- accounts.Add(new Account(accNumber, accHolder, initialBalance));
- Console.WriteLine("Account created successfully.");
- }
- // function to find an account by account number
- public Account FindAccount(string accNumber)
- {
- return accounts.Find(account => account.GetAccountNumber() == accNumber);
- }
- // function to display all accounts
- public void DisplayAllAccounts()
- {
- foreach (var account in accounts)
- {
- account.Display();
- Console.WriteLine("-------------------");
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Bank bank = new Bank();
- int choice;
- string accNumber, accHolder;
- double amount;
- do
- {
- Console.WriteLine("1. Create Account");
- Console.WriteLine("2. Deposit");
- Console.WriteLine("3. Withdraw");
- Console.WriteLine("4. Check Balance");
- Console.WriteLine("5. Display All Accounts");
- Console.WriteLine("6. Exit");
- Console.Write("Enter your choice: ");
- choice = int.Parse(Console.ReadLine());
- switch (choice)
- {
- case 1:
- Console.Write("Enter account number: ");
- accNumber = Console.ReadLine();
- Console.Write("Enter account holder name: ");
- accHolder = Console.ReadLine();
- Console.Write("Enter initial balance: ");
- amount = double.Parse(Console.ReadLine());
- bank.CreateAccount(accNumber, accHolder, amount);
- break;
- case 2:
- Console.Write("Enter account number: ");
- accNumber = Console.ReadLine();
- Account account = bank.FindAccount(accNumber);
- if (account != null)
- {
- Console.Write("Enter amount to deposit: ");
- amount = double.Parse(Console.ReadLine());
- account.Deposit(amount);
- }
- else
- {
- Console.WriteLine("Account not found.");
- }
- break;
- case 3:
- Console.Write("Enter account number: ");
- accNumber = Console.ReadLine();
- account = bank.FindAccount(accNumber);
- if (account != null)
- {
- Console.Write("Enter amount to withdraw: ");
- amount = double.Parse(Console.ReadLine());
- account.Withdraw(amount);
- }
- else
- {
- Console.WriteLine("Account not found.");
- }
- break;
- case 4:
- Console.Write("Enter account number: ");
- accNumber = Console.ReadLine();
- account = bank.FindAccount(accNumber);
- if (account != null)
- {
- Console.WriteLine($"Balance: {account.GetBalance()}");
- }
- else
- {
- Console.WriteLine("Account not found.");
- }
- break;
- case 5:
- bank.DisplayAllAccounts();
- break;
- case 6:
- Console.WriteLine("Exiting...");
- break;
- default:
- Console.WriteLine("Invalid choice. Please try again.");
- break;
- }
- } while (choice != 6);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement