Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 1 #12

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9b3169f
Add files via upload
MwaririDaniel Nov 30, 2024
1ccff9a
Delete AP-Assignment-main.zip
MwaririDaniel Nov 30, 2024
4267a92
Add files via upload
MwaririDaniel Nov 30, 2024
c151127
Delete src/AP-Assignment-main directory
MwaririDaniel Nov 30, 2024
fa224e2
Add files via upload
MwaririDaniel Nov 30, 2024
33a935c
Delete src/assignment 1 directory
MwaririDaniel Nov 30, 2024
3a24b1d
Add files via upload
MwaririDaniel Nov 30, 2024
a70de4d
Delete src/sct212-0717-2022 directory
MwaririDaniel Nov 30, 2024
fd76bbd
Add files via upload
MwaririDaniel Nov 30, 2024
17a52be
Update README.md
MwaririDaniel Nov 30, 2024
c82591a
Delete src/AP-Assignment-main directory
MwaririDaniel Dec 1, 2024
ed295f9
Add files via upload
MwaririDaniel Dec 1, 2024
3986d97
Rename BankAccountClass to BankAccount.java
MwaririDaniel Dec 1, 2024
b64b3b0
Rename BaseTransactionClass Implementing TransactionInterface to Base…
MwaririDaniel Dec 1, 2024
7796896
Rename DepositTransactionClass to DepositTransaction.java
MwaririDaniel Dec 1, 2024
4859fd4
Rename InsufficientFundsExceptionClass to InsufficientFundsException.…
MwaririDaniel Dec 1, 2024
22a5acd
Rename TransactionInterface to TransactionInterface.java
MwaririDaniel Dec 1, 2024
3327787
Rename WithdrawalTransactionClass to WithdrawalTransaction.java
MwaririDaniel Dec 1, 2024
6f25083
Rename MainClass to Main.java
MwaririDaniel Dec 1, 2024
3b6a8bc
Delete src/AP-Assignment-main directory
MwaririDaniel Dec 3, 2024
e38a497
Add files via upload
MwaririDaniel Dec 3, 2024
4ebc9df
this is my assignment
MwaririDaniel Dec 11, 2024
aa65509
Merge pull request #5 from MwaririDaniel/mwariri
MwaririDaniel Dec 11, 2024
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Advanced-Programming
A Repository for Programming Exercises and Assignments in Advanced Programming
SCT212-0717/2022

19 changes: 19 additions & 0 deletions src/assignment1/BankAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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) {
balance -= amount;
}
}
47 changes: 47 additions & 0 deletions src/assignment1/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Calendar;

public class BaseTransaction implements TransactionInterface {
protected double amount;
protected Calendar date;
protected String transactionID;

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

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

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

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

// Common method to print transaction details
public void printTransactionDetails() {
System.out.println("Transaction ID: " + transactionID);
System.out.println("Amount: " + amount);
System.out.println("Date: " + date.getTime());
}

// Method to apply the transaction (meant to be overridden by subclasses)
public void apply(BankAccount ba) {
System.out.println("Applying transaction...");
}

// Default reverse method (can be overridden if needed)
public boolean reverse(BankAccount ba) {
System.out.println("Reversal not supported for this transaction.");
return false;
}
}
20 changes: 20 additions & 0 deletions src/assignment1/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.Calendar;

public class DepositTransaction extends BaseTransaction {

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

@Override
public void apply(BankAccount ba) {
ba.deposit(amount);
System.out.println("Deposit successful: " + amount);
}

@Override
public boolean reverse(BankAccount ba) {
System.out.println("Deposits cannot be reversed.");
return false;
}
}
6 changes: 6 additions & 0 deletions src/assignment1/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);
}
}
//insufficient Funds
26 changes: 26 additions & 0 deletions src/assignment1/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Calendar;

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.0);
Calendar date = Calendar.getInstance();

// Create DepositTransaction
DepositTransaction deposit = new DepositTransaction(500.0, date, "D123");
deposit.apply(account);
deposit.printTransactionDetails();

// Create WithdrawalTransaction
WithdrawalTransaction withdrawal = new WithdrawalTransaction(300.0, date, "W456");
withdrawal.apply(account);
withdrawal.printTransactionDetails();

// Test reversal of withdrawal
withdrawal.reverse(account);

// Try to reverse deposit
deposit.reverse(account);

System.out.println("Final Balance: " + account.getBalance());
}
}
10 changes: 10 additions & 0 deletions src/assignment1/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import java.util.Calendar;

public interface TransactionInterface {
double getAmount();
Calendar getDate();
String getTransactionID();
void printTransactionDetails();
void apply(BankAccount ba);
boolean reverse(BankAccount ba);
}
28 changes: 28 additions & 0 deletions src/assignment1/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Calendar;

public class WithdrawalTransaction extends BaseTransaction {

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

@Override
public void apply(BankAccount ba) {
try {
if (ba.getBalance() < amount) {
throw new InsufficientFundsException("Insufficient funds for withdrawal.");
}
ba.withdraw(amount);
System.out.println("Withdrawal successful: " + amount);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
}

@Override
public boolean reverse(BankAccount ba) {
ba.deposit(amount);
System.out.println("Withdrawal reversed: " + amount);
return true;
}
}
19 changes: 19 additions & 0 deletions src/mwariri/BankAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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) {
balance -= amount;
}
}
47 changes: 47 additions & 0 deletions src/mwariri/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Calendar;

public class BaseTransaction implements TransactionInterface {
protected double amount;
protected Calendar date;
protected String transactionID;

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

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

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

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

// Common method to print transaction details
public void printTransactionDetails() {
System.out.println("Transaction ID: " + transactionID);
System.out.println("Amount: " + amount);
System.out.println("Date: " + date.getTime());
}

// Method to apply the transaction (meant to be overridden by subclasses)
public void apply(BankAccount ba) {
System.out.println("Applying transaction...");
}

// Default reverse method (can be overridden if needed)
public boolean reverse(BankAccount ba) {
System.out.println("Reversal not supported for this transaction.");
return false;
}
}
20 changes: 20 additions & 0 deletions src/mwariri/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.Calendar;

public class DepositTransaction extends BaseTransaction {

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

@Override
public void apply(BankAccount ba) {
ba.deposit(amount);
System.out.println("Deposit successful: " + amount);
}

@Override
public boolean reverse(BankAccount ba) {
System.out.println("Deposits cannot be reversed.");
return false;
}
}
6 changes: 6 additions & 0 deletions src/mwariri/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);
}
}
//insufficient Funds
26 changes: 26 additions & 0 deletions src/mwariri/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Calendar;

public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.0);
Calendar date = Calendar.getInstance();

// Create DepositTransaction
DepositTransaction deposit = new DepositTransaction(500.0, date, "D123");
deposit.apply(account);
deposit.printTransactionDetails();

// Create WithdrawalTransaction
WithdrawalTransaction withdrawal = new WithdrawalTransaction(300.0, date, "W456");
withdrawal.apply(account);
withdrawal.printTransactionDetails();

// Test reversal of withdrawal
withdrawal.reverse(account);

// Try to reverse deposit
deposit.reverse(account);

System.out.println("Final Balance: " + account.getBalance());
}
}
10 changes: 10 additions & 0 deletions src/mwariri/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import java.util.Calendar;

public interface TransactionInterface {
double getAmount();
Calendar getDate();
String getTransactionID();
void printTransactionDetails();
void apply(BankAccount ba);
boolean reverse(BankAccount ba);
}
28 changes: 28 additions & 0 deletions src/mwariri/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Calendar;

public class WithdrawalTransaction extends BaseTransaction {

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

@Override
public void apply(BankAccount ba) {
try {
if (ba.getBalance() < amount) {
throw new InsufficientFundsException("Insufficient funds for withdrawal.");
}
ba.withdraw(amount);
System.out.println("Withdrawal successful: " + amount);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
}

@Override
public boolean reverse(BankAccount ba) {
ba.deposit(amount);
System.out.println("Withdrawal reversed: " + amount);
return true;
}
}