Click here to Skip to main content
15,885,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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.
C#
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:
C#
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

In the construction of CheckingAccount, Use:

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


instead of

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


You missed to set the balance.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900