Answer to Question #240046 in C# for Gayatri

Question #240046

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 a property in the interface.

//public enum BankAccountTypeEnum

{

Current=1,Saving=2

}

Task 3: Define IBankAccount interface and add the following fields to it.

double GetBalance();

void Deposit(double amount);

bool Withdraw(double amount);

bool Transfer(IBankAccount toAccount, double amount);

BankAccountTypeEnum AccountType { get; set; }

Task 4: Create an abstract class called as BankAccount and implement the class with the interface

definedabove. Add a property called Balance in this class

protected double balance;


1
Expert's answer
2021-09-21T11:02:17-0400
Task 1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace BankAccountLibrary
{
    public class BankAccount : IBankAccount
    {


        protected double balance;
        public BankAccountTypeEnum AccountType { get; set; }


        public BankAccount(double amount)
        {
            balance = amount;
        }


        public bool Deposit(double amount)
        {
            if (amount > 0) {
                balance += amount;
                Console.WriteLine("The amount has been deposited.");
                return true;
            }
            return false;
        }


        public bool Withdraw(double amount)
        {
            if (amount <= balance) {
                balance -= amount;
                Console.WriteLine("The amount has been withdrawed.");
                return true;
            }
            return false;
        }
        public bool Transfer(IBankAccount toAccount, double amount) {
            return (this.Deposit(amount) && toAccount.Withdraw(amount));
        }
        public double GetBalance()
        {
            return balance;
        }
    }
}




Task 2:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace BankAccountLibrary
{
    public enum BankAccountTypeEnum
    {


        Current = 1, Saving = 2
    }
}




Task 3:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace BankAccountLibrary
{
    public interface IBankAccount
    {
        
        double GetBalance();
        bool Withdraw(double amount);
        bool Transfer(IBankAccount toAccount, double amount);
        BankAccountTypeEnum AccountType { get; set; }
    }
}




Task 4:




using System;
using System.Collections.Generic;
using BankAccountLibrary;
namespace App
{
   




    class Program
    {
        static void Main(string[] args)
        {
            BankAccount bankAccount1 = new BankAccount(1000);
            bankAccount1.AccountType = BankAccountTypeEnum.Current;
            BankAccount bankAccount2 = new BankAccount(2000);
            bankAccount2.AccountType = BankAccountTypeEnum.Saving;
            Console.WriteLine("The current balance of the bankAccount1 is: {0}", bankAccount1.GetBalance());
            Console.WriteLine("The current balance of the bankAccount2 is: {0}", bankAccount2.GetBalance());
            bankAccount1.Deposit(400);
            Console.WriteLine("bankAccount1.Deposit(400): {0}", bankAccount1.GetBalance());
            bankAccount2.Deposit(200);
            Console.WriteLine("bankAccount2.Deposit(200): {0}", bankAccount2.GetBalance());


            bankAccount1.Transfer(bankAccount2, 400);
            Console.WriteLine("bankAccount1.Transfer(bankAccount2, 400)");
            Console.WriteLine("The current balance of the bankAccount1 is: {0}", bankAccount1.GetBalance());
            Console.WriteLine("The current balance of the bankAccount2 is: {0}", bankAccount2.GetBalance());




            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS