Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 1 addition & 29 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
*.class
27 changes: 27 additions & 0 deletions BankAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class BankAccount {

private double balance;

public BankAccount(double balance) {
this.balance = balance;
}

public double getBalance() {
return balance;
}

public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount)
throws InsufficientFundsException {

if (amount > balance) {
throw new InsufficientFundsException(
"Insufficient Funds.");
}

balance -= amount;
}
}
41 changes: 41 additions & 0 deletions BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.Calendar;

public class BaseTransaction implements TransactionInterface {

protected double amount;
protected Calendar date;
protected String transactionID;

public BaseTransaction(double amount, String transactionID) {
this.amount = amount;
this.transactionID = transactionID;
this.date = Calendar.getInstance();
}

@Override
public double getAmount() {
return amount;
}

@Override
public Calendar getDate() {
return date;
}

@Override
public String getTransactionID() {
return transactionID;
}

@Override
public void printTransactionDetails() {
System.out.println("Transaction ID: " + transactionID);
System.out.println("Amount: " + amount);
System.out.println("Date: " + date.getTime());
}

@Override
public void apply(BankAccount ba) {
System.out.println("Generic transaction applied.");
}
}
15 changes: 15 additions & 0 deletions DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class DepositTransaction extends BaseTransaction {

public DepositTransaction(double amount,
String transactionID) {
super(amount, transactionID);
}

@Override
public void apply(BankAccount ba) {
ba.deposit(amount);
System.out.println(
"Deposit of " + amount +
" completed.");
}
}
6 changes: 6 additions & 0 deletions InsufficientFundsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class InsufficientFundsException extends Exception {

public InsufficientFundsException(String message) {
super(message);
}
}
14 changes: 14 additions & 0 deletions TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.Calendar;

public interface TransactionInterface {

double getAmount();

Calendar getDate();

String getTransactionID();

void printTransactionDetails();

void apply(BankAccount ba);
}
93 changes: 93 additions & 0 deletions WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
public class WithdrawalTransaction extends BaseTransaction {

private BankAccount accountUsed;
private boolean reversed = false;
private double amountNotWithdrawn;

public WithdrawalTransaction(double amount,
String transactionID) {
super(amount, transactionID);
}

@Override
public void apply(BankAccount ba) {

try {

ba.withdraw(amount);
accountUsed = ba;

System.out.println(
"Withdrawal of " +
amount +
" completed.");

} catch (InsufficientFundsException e) {

System.out.println(
e.getMessage());
}
}

public void apply(BankAccount ba,
boolean useAvailableBalance) {

try {

double balance =
ba.getBalance();

if (balance <= 0) {

throw new
InsufficientFundsException(
"Account balance is zero.");
}

if (balance < amount) {

amountNotWithdrawn =
amount - balance;

ba.withdraw(balance);

System.out.println(
"Partial withdrawal completed.");
} else {

ba.withdraw(amount);

amountNotWithdrawn = 0;
}

} catch (InsufficientFundsException e) {

System.out.println(
e.getMessage());

} finally {

System.out.println(
"Transaction processing finished.");
}
}

public boolean reverse() {

if (accountUsed != null &&
!reversed) {

accountUsed.deposit(amount);

reversed = true;

return true;
}

return false;
}

public double getAmountNotWithdrawn() {
return amountNotWithdrawn;
}
}