Computer Applications

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

Java Constructors

73 Likes

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);
    }
}

Answered By

37 Likes


Related Questions