Download as pdf or txt
Download as pdf or txt
You are on page 1of 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