0% found this document useful (0 votes)
41 views3 pages

Lab9 (HohJiaDa)

The document defines an abstract BankAccount class with methods for getting account information and depositing or withdrawing funds. It also defines a SavingsAccount subclass that implements the withdraw and deposit methods by updating the balance and printing messages.

Uploaded by

Hoh Jia Da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

Lab9 (HohJiaDa)

The document defines an abstract BankAccount class with methods for getting account information and depositing or withdrawing funds. It also defines a SavingsAccount subclass that implements the withdraw and deposit methods by updating the balance and printing messages.

Uploaded by

Hoh Jia Da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

BankAccount.

java

1
2 abstract public class BankAccount {
3 protected String customerID;
4 protected double balance;
5
6 public BankAccount(String customerID, double balance) {
7 this.customerID = customerID;
8 this.balance = balance;
9 }
10 public String getID() {
11 return this.customerID;
12 }
13 public double getBalance() {
14 return this.balance;
15 }
16 public String toString() {
17 return "Customer ID is: " + customerID + " Balance is: " + balance;
18 }
19 public abstract boolean withdraw(double amount);
20 public abstract void deposit (double amount);
21
22 }
23

Page 1
SavingsAccount.java

1
2 public class SavingsAccount extends BankAccount{
3
4 public SavingsAccount(String customerID, double deposit) {
5 super(customerID, deposit);
6
7 }
8 public boolean withdraw (double amount) {
9 if (balance - amount < 10) {
10 System.out.println("Not enough money ;(");
11 return false;
12 }
13 balance = balance - amount;
14 System.out.println("Customer ID is: " + customerID + " \nBalance is: " + balance);
15 return true;
16 }
17
18 public void deposit (double amount) {
19 balance = balance + amount;
20 }
21
22
23 }
24

Page 1
Test.java

1
2 public class Test {
3
4 public static void main(String[] args) {
5 // TODO Auto-generated method stub
6
7 BankAccount acc1 = new SavingsAccount("J23455", 100.00);
8 boolean result = acc1.withdraw(20.00);
9 acc1.deposit(20.00);
10
11 }
12 }
13

Page 1

You might also like