Click here to Skip to main content
14,236,939 members
Rate this:
Please Sign up or sign in to vote.
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment5
{
    class Program
    {
        class BankAccount
        {
            private double balance;

            public BankAccount() { balance = 0.0; }

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

            public virtual double Withdraw(double a)
            {
                if (balance >= a)
                { 
                    balance -= a;
                    return balance;
                }
                else
                    return -1.0;
            }

            public void Deposit(double a)
            {
                balance += a;
            }

            public double GetBalance() { return balance; }

            public class CheckingAccount : BankAccount
            {
                private double minBalance, charge;
                public CheckingAccount(double minA, double charge) : base()
                {
                    minBalance = minA;
                    this.charge = charge;
                }

                public double ProcessCheck(double a)
                {
                    double r;
                    if (GetBalance() >= minBalance)
                        r = base.Withdraw(a);
                    else
                        r = base.Withdraw(a + charge);

                    return 0.0;
                }

                public override double Withdraw(double a)
                {
                    return ProcessCheck(a);
                }
            }

            double x, y;

            public class SavingsAccount : BankAccount
            {
                private double ir;
                public SavingsAccount(double a, double r) : base(a)
                {
                    ir = r;
                }

                public void PostInterest()
                {
                    double balance = GetBalance();
                    double interest = balance * ir / 100;
                    Deposit(interest);
                }
            }

            static void Main(string[] args)
            {
                CheckingAccount ca = new CheckingAccount(2000.0, 2.0);
                Console.WriteLine("The initial balance is {0:c}", ca.GetBalance());
                ca.Deposit(1000.0);
                Console.WriteLine("Current balance after the deposit is {0:c}", ca.GetBalance());
                ca.ProcessCheck(300.0);
                Console.WriteLine("The balance after ProcessCheck is {0:c}", ca.GetBalance());
                ca.Withdraw(500.0);
                Console.WriteLine("The balance after withdrawl is {0:c}", ca.GetBalance());

                CheckingAccount x = new CheckingAccount(2000.0, 2.0);
                Console.WriteLine("The balance of x is {0:c}", x.GetBalance());
                x.Deposit(1000.0);
                Console.WriteLine("The balance after deposit is {0:c}", x.GetBalance());
                x.Withdraw(500.0);
                Console.WriteLine("The balance after withdrawl is {0:c}", x.GetBalance());

                SavingsAccount y = new SavingsAccount(4000.0, 5.0);
                Console.WriteLine("The balance of y is {0:c}", y.GetBalance());
                y.Deposit(1000.0);
                Console.WriteLine("The balance after the deposit is {0:c}", y.GetBalance());
                y.Withdraw(500.0);
                Console.WriteLine("The balance after withdrawl is {0:c}", y.GetBalance());
            }
        }
    }
}


What I have tried:

I've tried and completed everything. The only problem I'm facing in the main for "CheckingAccount ca" and "CheckingAccount x", it's not showing my initial balance of 2000.0, 2.0 when I run the program. Can you help?

The assignment is below.
using System;
namespace A5a_YourLastName
{
public class BankAccount {…}//this is the example 5.1 with the virtual keyword in Withdraw
   						 // method declaration (see Fig. 10.2 on Page 386)
public class ChecingAccount {…} //this is the example 10.7

public class QuizEx10_7 
{
public static void Main() 
{
      //a) Declare a local variable ca of type CheckingAccount,
              //    Creat a ChecingAccount object with two arguments 2000.0 and 2.0,
              //    assign the object to the variable ca and output the balance of ca.
     



             //b) Deposit 1000.0 dallors to the CheckingAccount ca and output the balance of ca.
     



             //c) Through the CheckingAccount ca process a check with $300 and output the balance of ca.
     



            //d) Withdraw 500.0 dallors from the CheckingAccount ca and output the balance of ca.
     



    } //end of Main
  }//end of class
}//end of namespace 

Write the output from the above Main method:
using System;
namespace A5b_YourLastName
{
public class BankAccount {…} //this is the example 5.1 with the virtual keyword in Withdraw
    // method declaration (see Fig. 10.2 on Page 386)
public class SavingsAccount {…} //this is the example 10.5
public class ChecingAccount {…} //this is the example 10.7

public class QuizEx10_11 
{
public static void Main() 
{
      //a) Declare two  local variables x and y of type BankAccount


              //b) Creat a ChecingAccount object with two arguments 2000.0 and 2.0,
              //    assign the object to the variable x and output the balance of x.
     

              //c) Creat a SavingsAccount object with two arguments 4000.0 and 5.0,
              //    assign the object to the variable y and output the balance of y.


             //d) Deposit 1000.0 dallors to each account of  x and y, and output the balance of each account.
     





            //d) Withdraw 500.0 dallors from each account, and output the balance of each account.
Posted
Updated 28-Mar-16 18:29pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

In the construction of CheckingAccount, Use:

public CheckingAccount(double minA, double charge)
                        : base(minA)


instead of

public CheckingAccount(double minA, double charge)
                        : base()


You missed to set the balance.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100