Skip to main content

Question about methods

2 replies [Last post]
shuaib
Offline
Joined: 2006-08-24
Points: 0

class BankAcc {

protected static int nextAccNo = 1;
protected String firstName;
protected String surName;
protected String address;
protected int accNo;
protected long balance;
public BankAcc( String fn, String sn, String ad) {
firstName = fn;
surName = sn;
address = ad;
balance = 0;
accNo = nextAccNo;
++nextAccNo;
}
public long getBalance() {
return balance;
}
public void deposit( long amount) {
balance = balance + amount;
}
public void withdraw( long amount) {
balance = balance - amount;
}
public void display() {
System.out.println("\nCustomer " + firstName + " " + surName);
System.out.println(" Account number = " + accNo);
System.out.println(" Account balance = " + (balance));
}
// we add the next method just to test the above code
public static void main( String arg[]) {
BankAcc b1 = new BankAcc("Joe", "Smith", "homeless");
BankAcc b2 = new BankAcc("Tim", "Bloggs", "Dublin");
b2.deposit(15500);
b2.deposit(10000);
b2.withdraw(26500);
b1.display();
b2.display();
}
}
Question
Override the withdraw() method in class DepositAcc so that it first
checks to see if the amount to be withdrawn is less than or equal to the
balance. If it is, then it calls the withdraw() method of it's parent class,
otherwise it generates an error message.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
namchuai
Offline
Joined: 2012-03-15
Points: 0

public void withdraw(long amout) {
if (balance <= amount) {
super.withdraw(long amount);
} else {
System.out.println("Your balance is not enough");
}
}
Hope it helps.
-nam

vmurali674
Offline
Joined: 2012-05-29
Points: 0

Namchuai, ur are correct... well done