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
1,895 changes: 1,895 additions & 0 deletions ml_learning.ipynb

Large diffs are not rendered by default.

423 changes: 423 additions & 0 deletions notebooks/initial.ipynb

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions src/Lecture4_interfaces_abstract_classes/BankAccount.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package Lecture4_interfaces_abstract_classes;

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

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

public void setBalance(double balance) {
this.balance = balance;
public void deposit(double amount){
balance+= amount;
}
public void withdraw(double amount) throws InsufficientFundsException{
if(balance < amount) throw new InsufficientFundsException("insufficient funds in the account");
balance -= amount;
}
}
66 changes: 25 additions & 41 deletions src/Lecture4_interfaces_abstract_classes/BaseTransaction.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public abstract class BaseTransaction implements TransactionInterface {
private final int amount;
private final Calendar date;
private final String transactionID;

/**
* Lecture1_adt.TransactionInterface Constructor
* @param amount in an integer
* @param date: Not null, and must be a Calendar object
* @return void
* Instialises the field, attributes of a transaction
* Creates a object of this
*/
public BaseTransaction(int amount, @NotNull Calendar date) {
public class BaseTransaction implements TransactionInterface {
protected double amount;
protected Calendar date;
protected String transactionId;

public BaseTransaction( double amount, Calendar date, String transactionId){
this.amount = amount;
this.date = (Calendar) date.clone();
int uniq = (int) Math.random()*10000;
transactionID = date.toString()+uniq;
}
this.date = date;
this.transactionId = transactionId;

/**
* getAmount()
* @return integer
*/
public double getAmount() {
return amount; // Because we are dealing with Value types we need not worry about what we return
}

/**
* getDate()
* @return Calendar Object
*/
public Calendar getDate() {
// return date; // Because we are dealing with Reference types we need to judiciously copy what our getters return
return (Calendar) date.clone(); // Defensive copying or Judicious Copying
public double getAmount(){
return amount;
}

// Method to get a unique identifier for the transaction
public String getTransactionID(){
return transactionID;
public Calendar getDate(){
return date;
}
// Method to print a transaction receipt or details
public abstract void printTransactionDetails();
public abstract void apply(BankAccount ba);
public String getTransactionId(){
return transactionId;
}
public void printTransactionDetails(){
System.out.println("Transaction ID: " + transactionId);
System.out.println("Amount: " + amount);
System.out.println("Date: " + date.getTime());
}
public void apply(BankAccount ba) {
System.out.println("Applying base transaction to account");
}


}
13 changes: 13 additions & 0 deletions src/Lecture4_interfaces_abstract_classes/DepositTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Calendar;
public class DepositTransaction extends BaseTransaction {
public DepositTransaction(double amount ,Calendar date, String transactionId){
super( amount, date, transactionId);
}


public void apply( BankAccount ba){
ba.deposit(amount);
System.out.println("Deposited"+ amount + "to account");

}
}
30 changes: 0 additions & 30 deletions src/Lecture4_interfaces_abstract_classes/DepositTrasaction.java

This file was deleted.

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);
}

}
20 changes: 4 additions & 16 deletions src/Lecture4_interfaces_abstract_classes/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
package Lecture4_interfaces_abstract_classes;
import java.util.Calendar;

/**
* Interface for Transactions
* Any class that defines a transaction is expected to implement this Interface
*/
public interface TransactionInterface {

// Method to get the transaction amount
public interface TransactionInterface {
double getAmount();

// Method to get the transaction date
Calendar getDate();

// Method to get a unique identifier for the transaction
String getTransactionID();

String getTransactionId();
void printTransactionDetails();
void apply( BankAccount ba);
}


71 changes: 36 additions & 35 deletions src/Lecture4_interfaces_abstract_classes/WithdrawalTransaction.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
package Lecture4_interfaces_abstract_classes;

import org.jetbrains.annotations.NotNull;

import java.util.Calendar;

public class WithdrawalTransaction extends BaseTransaction {
public WithdrawalTransaction(int amount, @NotNull Calendar date) {
super(amount, date);
}

private boolean checkDepositAmount(int amt) {
if (amt < 0) {
return false;
} else {
return true;
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("withdrew" + amount + " from account");
}
}
catch(InsufficientFundsException e){
System.err.println(e.getMessage());

// Method to reverse the transaction
public boolean reverse() {
return true;
} // return true if reversal was successful
}

// Method to print a transaction receipt or details
public void printTransactionDetails() {
System.out.println("Deposit Trasaction: " + this.toString());
}
public void apply(BankAccount ba , boolean allowPartial){
try{
if(ba.getBalance() < amount){
if( allowPartial){
double available = ba.getBalance();
ba.withdraw(available);
System.out.println("withdrew partial amount" + available);
}
else {
throw new InsufficientFundsException("Insufficient funds for withdrawal.");
}
}else{
ba.withdraw(amount);
}
}catch(InsufficientFundsException e){
System.err.println(e.getMessage());

/*
Oportunity for assignment: implementing different form of withdrawal
*/
public void apply(BankAccount ba) {
double curr_balance = ba.getBalance();
if (curr_balance > getAmount()) {
double new_balance = curr_balance - getAmount();
ba.setBalance(new_balance);
}
}

/*
Assignment 1 Q3: Write the Reverse method - a method unique to the WithdrawalTransaction Class
*/
}

public boolean reverse(BankAccount ba){
ba.deposit(amount);
return true;
}
};