Let’s build a sample banking program to perform the common tasks like Withdraw and Deposit. Task 1: Create a class library project and Add a Class called BankAccount. This class needs to implement the IBankAccount Interface. Task 2: Define a enum as follows. This enum will be used as
The file IBankAccount.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BankAccountLibrary
{
public abstract class IBankAccount
{
public abstract void Withdraw(double amount);
public abstract void Deposit(double amount);
}
}
The file BankAccount.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BankAccountLibrary
{
public class BankAccount : IBankAccount
{
private double balance;
public BankAccount(double amount)
{
balance = amount;
}
public override void Deposit(double amount)
{
balance += amount;
Console.WriteLine("The amount has been deposited.");
}
public override void Withdraw(double amount)
{
if (amount <= balance) {
balance -= amount;
Console.WriteLine("The amount has been withdrawed.");
}
}
public void DisplayCurrentBalance()
{
Console.WriteLine("The current balance of the account is: {0}", balance);
}
}
}
The file Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BankAccountLibrary;
namespace Q212610
{
enum Option {DEPOSIT=1,WITHDRAW=2,CURRENT_BALANCE=3,EXIT=4}
class Program
{
static void Main(string[] args)
{
Option option = Option.CURRENT_BALANCE;
double amount;
BankAccount userAccount = new BankAccount(0);
while (option != Option.EXIT)
{
//Main menu
Console.WriteLine("1. Deposit an amount into the account.");
Console.WriteLine("2. Withdraw an amount from the account.");
Console.WriteLine("3. View the current balance of the account.");
Console.WriteLine("4. Exit.");
Console.Write("Your choice: ");
option = (Option)int.Parse(Console.ReadLine());
//Deposit an amount into the account
if (option == Option.DEPOSIT)
{
Console.Write("Enter the amount to deposit: ");
double.TryParse(Console.ReadLine(), out amount);
userAccount.Deposit(amount);
}
else if (option == Option.WITHDRAW)//Withdraw an amount from the account.
{
Console.Write("Enter the amount to withdraw: ");
double.TryParse(Console.ReadLine(), out amount);
userAccount.Withdraw(amount);
}
else if (option == Option.CURRENT_BALANCE )//View the current balance of the account
{
userAccount.DisplayCurrentBalance();
}
else if (option == Option.EXIT)
{
//Exit
}
else
{
Console.WriteLine("Wrong menu item.");
}
}
}
}
}
Comments
Leave a comment