Question

In: Computer Science

Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...

Using C#, modify the codes below to do the following:

Develop a polymorphic banking application using the Account hierarchy created in the codes below.
Create an array of Account references to SavingsAccount and CheckingAccount objects.
For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit.
As you process each Account, determine its type. If an Account is a SavingsAccount, calculate the amount of interest owed to the Account using method

CalculateInterest, and then add the interest to the account balance using method Credit. After processing an Account, print the updated account balance obtained by using base class property Balance.

Codes to work with:

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

namespace INHERITANCE
{
public class Accounts
{

decimal bal;

public decimal Balance
{

get { return bal; }

set
{

if (value >= 0.0M)
{

bal = value;

}

else
{

bal = 0.0M;

Console.WriteLine("This account's amount is less than $0.00!");

}

}

}

public Accounts(decimal bal)
{

Balance = bal;

}

public virtual void credit(decimal amt)
{

bal += amt;

}

public virtual bool debit(decimal amt)
{

if (amt <= bal)
{

bal -= amt;

return true;

}

else
{

Console.WriteLine("Debit amount exceeded account balance!");

return false;

}

}

}

public class CheckingAccount : Accounts //derived class CheckingAccount
{

decimal fee; //instance variable that represents the fee charged per transaction.

public CheckingAccount(decimal bal, decimal fee)
: base(bal)
{

this.fee = fee;

}

public override void credit(decimal amt)
{

base.credit(amt);

Balance -= fee;

}

public override bool debit(decimal amt) //charge a fee only if money is actually withdrawn
{

if (base.debit(amt))
{

Balance -= fee;

return true;

}

else
{

return false;

}

}
}

class SavingsAccount : Accounts //derived class SavingsAccount
{

decimal interestRate;

public SavingsAccount(decimal balance, decimal interestRate) : base(balance)

{

this.interestRate = interestRate;

}

public decimal CalculateInterest() //method that returns a decimal indicating the amount of interest earned by an account.

{

return Balance * interestRate / 100m;

}

}


class AccountsTest
{
static void Main(string[] args)
{
{

Accounts Account1 = new Accounts(250);
Console.WriteLine("Initial balance of Account 1: {0:C}", Account1.Balance);
Account1.credit(354); //deposit amount
Account1.debit(200); //withdraw amount
Console.WriteLine("Final balance of Account 1: {0:C}", Account1.Balance);
Console.WriteLine();

Accounts Account2 = new Accounts(-100); //test to see if initial amount is less than or equal to $0.00
Console.WriteLine("Initial balance of Account 2: {0:C}", Account2.Balance);
Account2.credit(20); //deposit amount
Account2.debit(45); //withdraw amount
Console.WriteLine("Final balance of Account 2: {0:C}", Account2.Balance);
Console.WriteLine();

SavingsAccount Account3 = new SavingsAccount(83, 10); //test to see if interest charge for transaction
Console.WriteLine("Initial amount of Account 3: {0:C}", Account3.Balance);
Account3.credit(200); //deposit amount
Account3.debit(48); //withdraw amount
Console.WriteLine("Current balance of Account 3: {0:C}", Account3.Balance);
decimal interest = Account3.CalculateInterest();
Account3.credit(interest); //charged fee
Console.WriteLine("Final balance of Account 3: {0:C}", Account3.Balance);
Console.WriteLine();

CheckingAccount Account4 = new CheckingAccount(170, 2.5M);
Console.WriteLine("Initial balance of Account 4: {0:C}", Account4.Balance);
Account4.credit(198); //deposit amount
Account4.debit(75); //withdraw amount
Console.WriteLine("Final balance of Account 4: {0:C}", Account4.Balance);

Console.Read();

}
}
}
}

Solutions

Expert Solution

Code

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

namespace INHERITANCE
{
public class Accounts
{
decimal bal;
public decimal Balance
{
get { return bal; }
set
{
if (value >= 0.0M)
{
bal = value;
}
else
{
bal = 0.0M;
Console.WriteLine("This account's amount is less than $0.00!");
}
}
}
public Accounts(decimal bal)
{
Balance = bal;
}
public virtual void credit(decimal amt)
{
bal += amt;
}
public virtual bool debit(decimal amt)
{
if (amt <= bal)
{
bal -= amt;
return true;
}
else
{
Console.WriteLine("Debit amount exceeded account balance!");
return false;
}
}
}
public class CheckingAccount : Accounts //derived class CheckingAccount
{
decimal fee; //instance variable that represents the fee charged per transaction.
public CheckingAccount(decimal bal, decimal fee)
: base(bal)
{
this.fee = fee;
}
public override void credit(decimal amt)
{
base.credit(amt);
Balance -= fee;
}
public override bool debit(decimal amt) //charge a fee only if money is actually withdrawn
{
if (base.debit(amt))
{
Balance -= fee;
return true;
}
else
{
return false;
}
}
}
class SavingsAccount : Accounts //derived class SavingsAccount
{
decimal interestRate;
public SavingsAccount(decimal balance, decimal interestRate) : base(balance)
{
this.interestRate = interestRate;
}
public decimal CalculateInterest() //method that returns a decimal indicating the amount of interest earned by an account.
{
return Balance * interestRate / 100m;
}
}
class AccountsTest
{
static void Main(string[] args)
{
{   
Accounts[] account = new Accounts[3];
decimal amount,intrest;
account[0] = new Accounts(250); ;
account[1] = new SavingsAccount(83, 10);
account[2] = new CheckingAccount(170, 2.5M);

for(int i=0;i<account.Length;i++)
{
Console.WriteLine("\nInitial amount of Account {0:D}: {1:C}", (i + 1), account[i].Balance);
Console.Write("Enter amount to withdraw: ");
Decimal.TryParse(Console.ReadLine(), out amount);
account[i].debit(amount);
Console.Write("Enter amount to deposite: ");
Decimal.TryParse(Console.ReadLine(), out amount);
account[i].credit(amount);
if (account[i].GetType() == typeof(SavingsAccount))
{
intrest = (account[i] as SavingsAccount).CalculateInterest();
account[i].credit(intrest);
}
Console.WriteLine("Current amount of Account {0:D}: {1:C}",(i+1), account[i].Balance);
}

}
}
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should create an array of the superclass (Employee10A) to hold the subclass (HourlyEmployee10A, SalariedEmployee10A, & CommissionEmployee10A) objects, then load the array with the objects you create. Create one object of each subclass. The three subclasses inherited from the abstract superclass print the results using the overridden abstract method. Below is the source code for the driver class: public class EmployeeTest10A { public static void main(String[]...
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
Develop an application that searches for files on a wild card basis (a*.c, .c, *a, etc)...
Develop an application that searches for files on a wild card basis (a*.c, .c, *a, etc) and then displays the resultant file names in all CAPITALS using pipes in a "multiprocess setup" C language must be used
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create...
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types...
Develop a Java application that plays a "guess the number" game as described below. a) The...
Develop a Java application that plays a "guess the number" game as described below. a) The user interface is displayed and the user clicks the “Start Game” button to begin the game. b) Your application then gets a random number in the range 1-1000 inclusive (you might want to use Math.random or the Random class). c) The application then displays the following prompt (probably via a JLabel): I have a number between 1 and 1000 can you guess my number?...
Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are...
Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are created. Complete the following:     Create a 2-item array of type Account.     Store each account object created into the array.     For each element in this array, call the calculateSales() method, and use the toString() method to display the results.     Code should be fully commented.     Program flow should be logical. Code before revision: /* * To change this license header, choose...
How to make an application for windows using c# ?
How to make an application for windows using c# ?
Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify...
Problem Write in drjava is fine. Using the classes from Assignment #2, do the following: Modify the parent class (Plant) by adding the following abstract methods: a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the...
Implement the following socket programming in C (b) Chat Application using TCP
Implement the following socket programming in C (b) Chat Application using TCP
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT