Friday, July 3, 2015

Account Class with overloaded constructors, deposit, withdraw and print facility

class Account{
int accountNo;
String holderName;
double balance;

Account(){
accountNo=0;
holderName="Unnamed";
balance=0;
}

Account(int accountNo, String h, double b){
this.accountNo = accountNo;
this.holderName = h;
this.balance = b;
}

Account(int accountNo, String h){
this.accountNo = accountNo;
this.holderName = h;
this.balance = 0;
}

void printDetails(){
System.out.println("\n\nInfo of Account:"+ this.accountNo);
System.out.println("----------------------");
System.out.println("Balance of "+ this.accountNo +" is " +this.balance);
System.out.println("Name of object is "+this.holderName);
System.out.println("No of object is "+this.accountNo);
}

void deposit(double amt){
if(amt>0){
this.balance +=  amt;
}
else{
System.out.println("Negative Amount cannot be deposited");
}
}
void withdraw(double amt){
if(amt <= this.balance){
this.balance -= amt;
}
else{
System.out.println("Insufficient balance. Cannot do withdrawal");
}
}
}
class TestAccount{
public static void main(String[] args){
int i = 6;
Account a1 = new Account();
Account a2 = new Account(1001, "Abcd");
Account a3 = new Account(1001, "Efgh", 200);

a2.deposit(100);
a2.withdraw(500);

a2.printDetails();

}
}










No comments:

Post a Comment