ICSE Class 10 Computer Applications Question 19 of 19

Constructors — Question 19

Back to all questions
19
Question

Question 19

Design a class to represent a bank account. Include the following members:

Data members

  1. Name of the depositor
  2. Account number
  3. Type of account
  4. Balance amount in the account

Methods

  1. To assign initial values
  2. To deposit an amount
  3. To withdraw an amount after checking balance
  4. To display the name and balance
  5. Do write proper constructor functions
Answer
class BankAccount
{
    private String name; 
    private long accno;
    private char type;
    private double bal;

    BankAccount()   {
        name = "";
        accno = 0;
        type = '\0';
        bal = 0.0d;
    }
    BankAccount(String n, long accnumber, char t, double b)   {
        name = n;
        accno = accnumber;
        type = t;
        bal = b;
    }

    public void deposit(double amt)   {
        bal = bal + amt;
        System.out.println("Amount deposited: Rs. " + amt);
        System.out.println("Balance: Rs. " + bal);
    }

    public void withdraw(double amt)    {
        if(amt <= bal)  {
            bal = bal - amt;
            System.out.println("Amount withdrawn: Rs. " + amt);
            System.out.println("Balance: Rs. " + bal);
        }
        else {
            System.out.println("Insufficient Balance");
        }
    }
    
    public void display()
    {
        System.out.println("Name: " + name);
        System.out.println("Balance: Rs. " + bal);
    }
}