From c6575859a1fbd4b2ff60bc32a1c71c05c2b5fd66 Mon Sep 17 00:00:00 2001 From: Yvonne Gatwiri Date: Thu, 28 Nov 2024 21:19:34 +0300 Subject: [PATCH 1/4] initial commit --- src/Assignment-SCT212-0139/BankAccount.class | Bin 0 -> 437 bytes src/Assignment-SCT212-0139/BankAccount.java | 19 +++++++ .../BaseTransaction.class | Bin 0 -> 1854 bytes .../BaseTransaction.java | 47 ++++++++++++++++++ .../ConcreteTransaction.class | Bin 0 -> 1541 bytes .../ConcreteTransaction.java | 26 ++++++++++ .../DepositTransaction.class | Bin 0 -> 1116 bytes .../DepositTransaction.java | 20 ++++++++ .../InsufficientFundsException.class | Bin 0 -> 252 bytes .../InsufficientFundsException.java | 6 +++ src/Assignment-SCT212-0139/Main.class | Bin 0 -> 1386 bytes src/Assignment-SCT212-0139/Main.java | 26 ++++++++++ .../TransactionInterface.class | Bin 0 -> 352 bytes .../TransactionInterface.java | 10 ++++ .../WithdrawalTransaction.class | Bin 0 -> 1421 bytes .../WithdrawalTransaction.java | 28 +++++++++++ src/Lecture1_adt/Transaction1.java | 1 + src/Lecture1_adt/Transaction2.java | 1 + src/Lecture1_adt/Transaction3.java | 1 + src/Lecture1_adt/Transaction4.java | 1 + 20 files changed, 186 insertions(+) create mode 100644 src/Assignment-SCT212-0139/BankAccount.class create mode 100644 src/Assignment-SCT212-0139/BankAccount.java create mode 100644 src/Assignment-SCT212-0139/BaseTransaction.class create mode 100644 src/Assignment-SCT212-0139/BaseTransaction.java create mode 100644 src/Assignment-SCT212-0139/ConcreteTransaction.class create mode 100644 src/Assignment-SCT212-0139/ConcreteTransaction.java create mode 100644 src/Assignment-SCT212-0139/DepositTransaction.class create mode 100644 src/Assignment-SCT212-0139/DepositTransaction.java create mode 100644 src/Assignment-SCT212-0139/InsufficientFundsException.class create mode 100644 src/Assignment-SCT212-0139/InsufficientFundsException.java create mode 100644 src/Assignment-SCT212-0139/Main.class create mode 100644 src/Assignment-SCT212-0139/Main.java create mode 100644 src/Assignment-SCT212-0139/TransactionInterface.class create mode 100644 src/Assignment-SCT212-0139/TransactionInterface.java create mode 100644 src/Assignment-SCT212-0139/WithdrawalTransaction.class create mode 100644 src/Assignment-SCT212-0139/WithdrawalTransaction.java diff --git a/src/Assignment-SCT212-0139/BankAccount.class b/src/Assignment-SCT212-0139/BankAccount.class new file mode 100644 index 0000000000000000000000000000000000000000..81dad378f8f031d0b6c9f101ff7e76c03a8fe4c7 GIT binary patch literal 437 zcmZ{g%}N6?6opSZKWS%b|NPmxa5e2L>hY}`LjCPo(m{(;SGaVX$) zd!rhvs7VBMgs2OI1C`wzX`RnADrKTbY9kJ h<11<-{mY~LU{D&+O_WMhLUp)DOa-ag^e zxMJZA8#8#bC*!T-P^%^r_1jEfDmHce@?+X*S}WPCWLfq#8`m)_aHS~^)vE8+rGDz@ z1O5uN^mHh2t1uXMaquXOJZ@OHY2$6o30#hf>kTMiwM=-I+)NGgx|0RtnT!?;qBbFNKRSm=2{9LlEsx)%ROdi@?E_vl}agmEU@%M9jPFcZr<~CK5VyIexOw&zvl;e zec*&WHWuzuB3*J?_p}Q3WSvzN3$>X4UhNF_WO0XTTlE{Xnb>eV^{Cz4Rl&C0b$K#Q zmR33frJN}+!<*qqV7iC5Gj)UHxb#N@E$+&*rpA@FBi_#wjVxM1VlEr+5Pu-83L z1*@(MLls&eo$~`V4NKL+j{>v*59Oqb>%t9=uMxiCPcVvv8H=1S2{ZMgFQ*#67Vnd& zaJ|Rfr$i?CohtqXQCv8IRa`znrZ|&0!Q>yj5QKnZ&Va`0EY0--F4A=l8Q$K<4szZfgF(fCV<8K=7A%&@s8yF-aSfk7zRbD{(QivCR@~mYM!K&qweTzUDW_ Y{S%@#qV-X+fvg|6kUe`X=AidK)?&4q860a`>o!f6-BEkRCM&|&>k_8CY|Jz8UDnN@I}W_ zhX+5vk8)f|JJQmOFDKdi?6vm&{Qk513&0}QP4ppQpx?p(1_jcKGEfy?x`C`}+j9j5 z@7S)b?+MIhOXZJ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new file mode 100644 index 0000000..bea2e69 --- /dev/null +++ b/src/Assignment-SCT212-0139/DepositTransaction.java @@ -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; + } +} diff --git a/src/Assignment-SCT212-0139/InsufficientFundsException.class b/src/Assignment-SCT212-0139/InsufficientFundsException.class new file mode 100644 index 0000000000000000000000000000000000000000..6087aa3d3e08d9f708904ae4e70c755e91c5070c GIT binary patch literal 252 zcmX^0Z`VEs1_mbvUM>bE24;2!79Ivx1~x_p;jF~6ME#t^ymWonisaOSlFa-(Mg}&U z%)HDJJ4Oak4IhMp;F6-uymV{LFm?tGMg}R*yyDWdw9Mqp)Vvb6(!7*nBm-ES^HWk8 z8TfrN^HTjvbCXhwLK2g5fFfMM`K3k4scxAdjy!H7^gz}!C^E1EoeKhtKnQd&kmLmN llz^fjaW*aO?F@_?!SY-{6684s24+foxj5IqA)HY}@PxF{-!2v)d8P`nT?F^LEo@Dd~f@-QS*xU!i|?M|?G@)LaZ z)lble#F8q@XFtlaCrOpTq)b)j($mv@&gq{0_51q|07>kc=s>4|fQ2AJ488C8A&)s+ z?#G_(y%TxG5V~tiTis{qj81ME2s3mixjcB7&%0%*LWz$FiUgQ2VCg8Q7y{As)UF>D=Oa7D{>wnR6Zv809Kw&NR^U>L~R>TSW}@40gVt@l&wgfJ!vZSKb6O%vm| zpobAXjOk&{LJZd!3{M;i&nHp-#j&ly&UN8BZWvg!a1*yqlr4MYD^a9sxn(NeK%K7b z#-3}$#8~iRT-WN3g(bupOe#T=lODPPRN{1mFy6Cp9}kFUkspYZEAw1wQnb>wQa%Ny z61Pfe;vtd-QWny9#L!o(=E>y5ttOr$b&%E&y2l=qKx^D4k3h;T5U!q#Eya4dxF@^~ zzUPo7P^_H~Mt9nUAX-_s?B&I>t)07cusIEeq)NK3@|DL+YeKzs3w{{S$+~Py?u<5s zSQ=$LA-pt^#fE`bT8vGG@fw?2=h*VlJrJ>a1J(6erlxc~YMf8aq#BO(R4ZH-9N~{= zT=$?{inju9vYfcMaa0m*+?%P_Uu(R%CXEpE+MKMd1l8JFl~Q;qhx@*uUfp7tXyMcj zs(~Gbk^j5Whoa64m$6JA75(b~y(ffdT%p}C&5Y*YR2A$CEf`j5)W<+4y6^-~X{}v9 zlQl^5?lFw5esopQo%x2-YscuP?chx0TovcnXQwL|o~_{WF~-S!btV$6VrnTc6rh9Y zp+E&QwdHJ1d(P*+Vu9w{-*FeHAn_N%0xU`vL^q}FAt1x_H5;SdRh-5mS>otL0)2Rl jL9F2%-e8CTXXs7`?bm4z6Yv-Ge})|GOj^Chw)Xo2Y&KMl literal 0 HcmV?d00001 diff --git a/src/Assignment-SCT212-0139/Main.java b/src/Assignment-SCT212-0139/Main.java new file mode 100644 index 0000000..314d389 --- /dev/null +++ b/src/Assignment-SCT212-0139/Main.java @@ -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()); + } +} diff --git a/src/Assignment-SCT212-0139/TransactionInterface.class b/src/Assignment-SCT212-0139/TransactionInterface.class new file mode 100644 index 0000000000000000000000000000000000000000..57aba573533f90036ba444bff5b0f039e6902b8a GIT binary patch literal 352 zcmZ8d%TB{E5F8g8)ADMe0`UjQg?vCyEfEq@r5=hna9fOtT#OSrP9=Vg10TRgAvTsO zMO-{9@9fO%{``J@16blxVjwVk^yC^+!BpxTpy%aAt3pupr8e=(rgtc+F)#cM^?L6@^jWe_y?V!B$AdtWHkMVV*Zl3wq$Z#c*Q=CiTW=Fb6#gc0vX0rr!OqPo4JHJz6A}ZxP;kn{Hi5J@ASD6;Pm}dHEMCv9c4kBK z2l^X$;GGvGhGYh#!|+QOubqsTB!-*@%nX2_p%?=TSF zi36~jg(-&lO{t<*%XM5K^+qI{>c(G=2n^~ZW*8Rx_4$@D$hZ73|MW}vwG^_LGt6_8 zw(x&Uez0*2KQbggdom;nwc@=o97o>52^)(z$uQY1PF#q6rA3F>`4JJGjsv@X=`Phr zQ$u%1#oQrGU5K8e~s{K@9VCW<+*!am1wwfaFm8&ygI7OtZev{nNbytepQD;Yl z+kD3($p{YuZ=WhqsP}c6=73>*OLM2);6cyS3dNoX6*>La_#vU&@}tlZ8?FgDKX44z z3{`Tj_`X&;1!ulx#zbb~EOf&o{guce0^xAy}v z!2D64{hggB!qMga*7ZWrmGrh;Ey@h`rj#PAdR!@?Ec{9R`-amXs1_Ow3*R@TCn4sA z6LfKhXpDhjnh<(FX#K^EHSI^}Kel{kHJ5k;_KFS+ztU=+8<1fV7D+DRx1P)2NKg=9 zedPew@;?}V_8jBykbRJueuLS6SB&TY^RKaBc5kr+9H8(56AcobJ-{lRUQsMy73XN+ zhq~DoDGy-NH;x&kv4Ba^XUXCy&Le{|rf`WYk{F(OU?H2L2$xA3)3`*44BA{T`}r@~ yf9_^ayd-5zYoZH&jsoQ{i+MV&lWqvV;|l#vm8xWUwYy%&Cdo{yKTyYQ Date: Thu, 28 Nov 2024 21:26:44 +0300 Subject: [PATCH 2/4] initial commit --- .../ConcreteTransaction.class | Bin 1541 -> 0 bytes .../ConcreteTransaction.java | 26 ------------------ 2 files changed, 26 deletions(-) delete mode 100644 src/Assignment-SCT212-0139/ConcreteTransaction.class delete mode 100644 src/Assignment-SCT212-0139/ConcreteTransaction.java diff --git a/src/Assignment-SCT212-0139/ConcreteTransaction.class b/src/Assignment-SCT212-0139/ConcreteTransaction.class deleted file mode 100644 index ca03dcf68465c40dd4c5b5316674e5109bdcea62..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1541 zcmaJ>+fvg|6kUe`X=AidK)?&4q860a`>o!f6-BEkRCM&|&>k_8CY|Jz8UDnN@I}W_ zhX+5vk8)f|JJQmOFDKdi?6vm&{Qk513&0}QP4ppQpx?p(1_jcKGEfy?x`C`}+j9j5 z@7S)b?+MIhOXZJpTjrbEcJf8(D7Pm4LQCgJcQe1HecRnE%&w=9M9P3^;Q%awp$OJ8 z*$K3&3naXz78q-RZ7=_VNKL$w^+FPd1jZK{4QGdR(|RwOTrOu~2*U<47Dg~CFcMLB z69^cMkj`QBW7)X)k~l0dv8uL}AN0CNVFHr|j#xN~V{}pU+^Vm1cUca0gtc4}$n^Tj zu)ks@3v~+bKa5gT&Ua~N#Qx;BRhAokGuj#UWqLcv{LLcYC zTuZa@z*!4(I4{s&lUfOkwye7QXks2Y19=M1WjK;yf(*M^)WAS5$G$QU7s&->gA5i?Bd`*_gCyg`D0z*S=AzF~Y9#{oiH z;>lfxz9(kCmsrCqO#TJP Date: Wed, 4 Dec 2024 13:17:52 +0300 Subject: [PATCH 3/4] corrections --- .gitignore | 5 +- src/Assignment-SCT212-0139/BankAccount.java | 42 ++++++++++++- .../BaseTransaction.class | Bin 1854 -> 1723 bytes .../BaseTransaction.java | 59 ++++++++++++++---- .../DepositTransaction.class | Bin 1116 -> 1001 bytes .../DepositTransaction.java | 37 +++++++++-- src/Assignment-SCT212-0139/Main.class | Bin 1386 -> 1371 bytes src/Assignment-SCT212-0139/Main.java | 3 - .../WithdrawalTransaction.java | 34 ++++++++++ 9 files changed, 155 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index f68d109..1638c37 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,7 @@ bin/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + +## compiled class files ## +*.class \ No newline at end of file diff --git a/src/Assignment-SCT212-0139/BankAccount.java b/src/Assignment-SCT212-0139/BankAccount.java index 27573a3..d208d11 100644 --- a/src/Assignment-SCT212-0139/BankAccount.java +++ b/src/Assignment-SCT212-0139/BankAccount.java @@ -1,19 +1,57 @@ public class BankAccount { private double balance; - public BankAccount(double balance) { - this.balance = balance; + /** + * Constructor to initialize the bank account with an initial balance. + * @param initialBalance The initial balance of the account. + * @throws IllegalArgumentException if initialBalance is negative. + * Precondition: initialBalance >= 0 + * Postcondition: The balance of the account is set to initialBalance. + */ + public BankAccount(double initialBalance) { + if (initialBalance < 0) { + throw new IllegalArgumentException("Initial balance cannot be negative."); + } + this.balance = initialBalance; } + /** + * Returns the current balance of the account. + * Precondition: None + * Postcondition: The current balance is returned, which should be >= 0. + * @return The current balance. + */ public double getBalance() { return balance; } + /** + * @param amount The amount to deposit. Must be positive. + * Precondition: amount > 0 + * Postcondition: The balance is increased by the deposit amount (balance = balance + amount). + * @throws IllegalArgumentException if the amount is not positive. + */ public void deposit(double amount) { + if (amount <= 0) { + throw new IllegalArgumentException("Deposit amount must be positive."); + } balance += amount; } + /** + * @param amount The amount to withdraw. Must be positive and less than or equal to the current balance. + * Precondition: amount > 0 and amount <= balance + * Postcondition: The balance is decreased by the withdrawal amount (balance = balance - amount). + * @throws IllegalArgumentException if the amount is not positive or exceeds the current balance. + * @throws InsufficientFundsException if there are insufficient funds for the withdrawal. + */ public void withdraw(double amount) { + if (amount <= 0) { + throw new IllegalArgumentException("Withdrawal amount must be positive."); + } + if (amount > balance) { + throw new IllegalArgumentException("Insufficient funds for withdrawal."); + } balance -= amount; } } diff --git a/src/Assignment-SCT212-0139/BaseTransaction.class b/src/Assignment-SCT212-0139/BaseTransaction.class index d2b060cd5cc75ff9ed02bf1b6c897270482da6c4..18d58384377354606adcab771017964c9de7af6d 100644 GIT binary patch delta 386 zcmZ9Hxk>|36o$V$YckmkhJh#n6_S9K7SV}IR^uAuuIVf+)h1RxgVn8k1Pf8YN+Azm z>l640VlSQ<5W(jE&prQg?;WOJQ{LP6%MfVLfAac<=`yEpjW2y?R@9pQF#VYgMZA7~ zd37-eCRB_qh1)tEoXH{MuTw{@8 zB;rwW!VU_XdBH$m+ofJrcD`&NE?!SCL0XbIQsP-)F?5Prz>o>->1W&r85ChOtb?rJ z63c>t{Y|Cu0LPt0!=dO5HBV3Pgll>^8IN*pA@;^*HE|35K delta 558 zcmZ8dOD_Xa6#j0fovESKR!I=drY%x-f_k>|C^@j)$k2K*ReNUj@KhF#ImG}Cgl%H_p{ zWjn^9c4XO_b5gEoj;)&&z2uZFvtL1qAA3IA726FN459ADjBcK$OC{^lbVhm#JTDDr zmKegD|2Zv}O=--J6^6R>xiGrp8iSJ69fO8O85=4#v87Q@j_7_(Ht3kMwfI;Q+4fX lCP|q-`l}rA9?WBbYLw=6axJ>J?BXi&;v8akv4;b+{sK;vRb2o8 diff --git a/src/Assignment-SCT212-0139/BaseTransaction.java b/src/Assignment-SCT212-0139/BaseTransaction.java index 6001e33..6fb188a 100644 --- a/src/Assignment-SCT212-0139/BaseTransaction.java +++ b/src/Assignment-SCT212-0139/BaseTransaction.java @@ -1,47 +1,80 @@ import java.util.Calendar; +/** + * BaseTransaction represents a financial transaction with details such as amount, date, and a unique transaction ID. + * This class provides methods to retrieve transaction details and apply the transaction to a bank account. + * + * - The amount of the transaction must be a valid positive number (>= 0). + * - The date must not be null and should accurately reflect the time of the transaction. + * - The transaction ID must be unique for each transaction. + */ public class BaseTransaction implements TransactionInterface { - protected double amount; - protected Calendar date; - protected String transactionID; + protected double amount; // The amount for this transaction + protected Calendar date; // The date this transaction occurred + protected String transactionID; // Unique identifier for the transaction - // Constructor + /** + * Constructs a new BaseTransaction object. + * @param amount the amount for the transaction, should be >= 0 + * @param date the date of the transaction, must not be null + * @param transactionID the unique ID for the transaction, must not be null + */ public BaseTransaction(double amount, Calendar date, String transactionID) { + if (amount < 0) { + throw new IllegalArgumentException("Amount must be greater than or equal to 0"); + } + if (date == null || transactionID == null) { + throw new IllegalArgumentException("Date and transaction ID must not be null"); + } this.amount = amount; this.date = date; this.transactionID = transactionID; } + /** + * Returns the amount of the transaction. + * @return the amount of the transaction + */ @Override public double getAmount() { return amount; } + /** + * Returns the date of the transaction. + * @return the date of the transaction as a Calendar object + */ @Override public Calendar getDate() { - return date; + return (Calendar) date.clone(); } + /** + * Returns the unique transaction ID. + * @return the transaction ID as a String + */ @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) + /** + * Applies the transaction to the specified bank account. + * - The bank account balance should be updated according to the transaction (e.g., deposit or withdrawal). + * + * @param ba the BankAccount to apply the transaction to + */ public void apply(BankAccount ba) { + if (ba == null) { + throw new IllegalArgumentException("BankAccount must not be null"); + } 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; - } } diff --git a/src/Assignment-SCT212-0139/DepositTransaction.class b/src/Assignment-SCT212-0139/DepositTransaction.class index 1b18c1e090d9349b258b2cdb3ee089ec1eb52121..e5759c9f46518298793ab456acf158cd6512f5eb 100644 GIT binary patch delta 175 zcmW-ZJr==G5QXo1kG!0hNb5vOg7}S}ghOWsY{U-i!I{!3vi*4;msD}W7_XSY{!Ql-4rO7$hvNi=jAVQi6yNHCF|epKcPsT_1_EK1p$BG6E% z!ech&Xc}>g-yAg4gAdDniuT{$Ot${n2!X0?M$k=_}4p==*NsiMD1r~_E D8Q2#a delta 318 zcmYjLOHRU26g{_vwpJtxCWcf=qZTL%eo*{CG!lodz!{bfj7e)~HE{v1;B7+(IMBp_ z3vlYx4d^0_r-=gx@7(jb=jMHxpO*dp^ZEv0fd0geW!b{2VDU7(j+4tYaf4wL#i@G{ zy5sOR94FzZt?s1jn150*+c+BxqpOo3h;O2_=bx)b>C1-t7N@vnV;db4I|Vq{Rb}lX zw=eM2vE~^j4h2=xs_N+TM>+&~7(|c1@yPR>Q_Xs-l6!$nC2MpjYx zx*un05jt_2X#oqEVdkTmZP2Z;nRV?aluIkmSkfP;{$HJAufz*$^fj!b#c6S4ldp|# M)+O7+0eYzX0-7f 0 + * @param date the date of the deposit, must not be null + * @param transactionID the unique ID for the transaction, must not be null + */ public DepositTransaction(double amount, Calendar date, String transactionID) { super(amount, date, transactionID); + if (amount <= 0) { + throw new IllegalArgumentException("Deposit amount must be greater than 0"); + } } + /** + * Applies the deposit transaction to the specified bank account by increasing the balance. + * + * Preconditions: + * - The BankAccount provided must not be null. + * - The amount to be deposited must be greater than 0 (checked in the constructor). + * + * Postcondition: + * - The bank account balance is increased by the amount of the deposit. + * - A message is printed confirming the deposit and the amount. + * + * @param ba the BankAccount to apply the deposit to + */ @Override public void apply(BankAccount ba) { + if (ba == null) { + throw new IllegalArgumentException("BankAccount must not be null"); + } ba.deposit(amount); System.out.println("Deposit successful: " + amount); } - - @Override - public boolean reverse(BankAccount ba) { - System.out.println("Deposits cannot be reversed."); - return false; - } } diff --git a/src/Assignment-SCT212-0139/Main.class b/src/Assignment-SCT212-0139/Main.class index 26834dca53da045deb7e36d8d05d11972fa2d8a2..4d8e5e861112ffce76f0a06ff11d316c7e3be00a 100644 GIT binary patch delta 320 zcmYL?xlRIM6otQkhGFDq)PzJ56Cp7M*Jz@E3+{;Gf{GisC5D)Zg$ow?B!6LRYY7D= z6h453jiqm3=L;AwmBl^h+~u76t-rK>|82ejS<)(6X=_~BPp#;)4_*O=(nP;o0Zq1JsQ@f_qj&|&6 z_Bn7lY?+Qs(*6t_D-!0yP6S;}6>*XAk|J2BR<9rL>17p5z96V@2nyzv)gSp2t+5jGKLV6=-8QHj_Ri$#HL T;WmjXO6|xFw5T#CoYDOUJt#6n delta 344 zcmYL?OG^S#7>3{P%#4$#fkGl`Vc12wF_NZTru$@Cre#;V2@N3;iMFU;a+a-v{(vqF zT(qxk%l<+18=`L@XmQ@>eb4#6_k7APDc^tEuK-+>8?s3ui5|p)m`2h>%EAn0)f=UT z=Pfu`Ajs>=z3WxI+Es<1jg6;6yt`y!87pc~Bs 0 + * @param date the date of the withdrawal, must not be null + * @param transactionID the unique ID for the transaction, must not be null + */ public WithdrawalTransaction(double amount, Calendar date, String transactionID) { super(amount, date, transactionID); + if (amount <= 0) { + throw new IllegalArgumentException("Withdrawal amount must be greater than 0"); + } } + /** + * Applies the withdrawal transaction to the specified bank account by decreasing the balance. + * + * Preconditions: + * - The BankAccount provided must not be null. + * - The amount to be withdrawn must be greater than 0 (checked in the constructor). + * - The account must have sufficient funds for the withdrawal. + * @param ba the BankAccount to apply the withdrawal to + */ @Override public void apply(BankAccount ba) { + if (ba == null) { + throw new IllegalArgumentException("BankAccount must not be null"); + } try { if (ba.getBalance() < amount) { throw new InsufficientFundsException("Insufficient funds for withdrawal."); @@ -19,8 +45,16 @@ public void apply(BankAccount ba) { } } + /** + * Reverses the withdrawal by depositing the same amount back to the bank account. + * @param ba the BankAccount to reverse the withdrawal on + * @return true if the reversal is successful + */ @Override public boolean reverse(BankAccount ba) { + if (ba == null) { + throw new IllegalArgumentException("BankAccount must not be null"); + } ba.deposit(amount); System.out.println("Withdrawal reversed: " + amount); return true; From 41b156a673f76b016b59dd206c12ee0ee69cd9b3 Mon Sep 17 00:00:00 2001 From: Fidelity-Kathure Date: Sun, 12 Jan 2025 09:43:13 +0300 Subject: [PATCH 4/4] initial commit: Assignment File --- Transactions/Transactioninterface | 151 ++++++++++++++++++ src/Assignment-SCT212-0139/BankAccount.class | Bin 437 -> 0 bytes .../BaseTransaction.class | Bin 1723 -> 0 bytes .../DepositTransaction.class | Bin 1001 -> 0 bytes .../InsufficientFundsException.class | Bin 252 -> 0 bytes src/Assignment-SCT212-0139/Main.class | Bin 1371 -> 0 bytes .../TransactionInterface.class | Bin 352 -> 0 bytes .../WithdrawalTransaction.class | Bin 1421 -> 0 bytes .../2022 Fidelity kathure}/BankAccount.java | 0 .../BaseTransaction.java | 10 +- .../DepositTransaction.java | 9 +- .../InsufficientFundsException.java | 2 +- .../2022 Fidelity kathure}/Main.java | 0 .../TransactionInterface.java | 0 .../WithdrawalTransaction.java | 5 - 15 files changed, 157 insertions(+), 20 deletions(-) create mode 100644 Transactions/Transactioninterface delete mode 100644 src/Assignment-SCT212-0139/BankAccount.class delete mode 100644 src/Assignment-SCT212-0139/BaseTransaction.class delete mode 100644 src/Assignment-SCT212-0139/DepositTransaction.class delete mode 100644 src/Assignment-SCT212-0139/InsufficientFundsException.class delete mode 100644 src/Assignment-SCT212-0139/Main.class delete mode 100644 src/Assignment-SCT212-0139/TransactionInterface.class delete mode 100644 src/Assignment-SCT212-0139/WithdrawalTransaction.class rename src/{Assignment-SCT212-0139 => SCT 212-0718/2022 Fidelity kathure}/BankAccount.java (100%) rename src/{Assignment-SCT212-0139 => SCT 212-0718/2022 Fidelity kathure}/BaseTransaction.java (83%) rename src/{Assignment-SCT212-0139 => SCT 212-0718/2022 Fidelity kathure}/DepositTransaction.java (84%) rename src/{Assignment-SCT212-0139 => SCT 212-0718/2022 Fidelity kathure}/InsufficientFundsException.java (87%) rename src/{Assignment-SCT212-0139 => SCT 212-0718/2022 Fidelity kathure}/Main.java (100%) rename src/{Assignment-SCT212-0139 => SCT 212-0718/2022 Fidelity kathure}/TransactionInterface.java (100%) rename src/{Assignment-SCT212-0139 => SCT 212-0718/2022 Fidelity kathure}/WithdrawalTransaction.java (89%) diff --git a/Transactions/Transactioninterface b/Transactions/Transactioninterface new file mode 100644 index 0000000..57c46a2 --- /dev/null +++ b/Transactions/Transactioninterface @@ -0,0 +1,151 @@ +import java.util.Calendar; + +public interface TransactionInterface { + double getAmount(); + Calendar getDate(); + String getTransactionID(); + void printTransactionDetails(); + void apply(BankAccount bankAccount); +} +// Basetransaction +import java.util.Calendar; + +public abstract class BaseTransaction implements TransactionInterface { + private double amount; + private Calendar date; + private String transactionID; + + 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; + } + + @Override + public void printTransactionDetails() { + System.out.println("Transaction ID: " + transactionID); + System.out.println("Amount: " + amount); + System.out.println("Date: " + date.getTime()); + } + + @Override + public abstract void apply(BankAccount bankAccount); +} +//Deposittransaction +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 bankAccount) { + bankAccount.deposit(getAmount()); + System.out.println("Deposit successful. New balance: " + bankAccount.getBalance()); + } +} +//Withdrawaltransaction +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 bankAccount) throws InsufficientFundsException { + if (bankAccount.getBalance() >= getAmount()) { + bankAccount.withdraw(getAmount()); + System.out.println("Withdrawal successful. New balance: " + bankAccount.getBalance()); + } else { + throw new InsufficientFundsException("Insufficient funds for withdrawal!"); + } + } + + public boolean reverse(BankAccount bankAccount) { + bankAccount.deposit(getAmount()); + System.out.println("Withdrawal reversed. New balance: " + bankAccount.getBalance()); + return true; + } +} +//InsufficientFundsException +public class InsufficientFundsException extends Exception { + public InsufficientFundsException(String message) { + super(message); + } +} +//BankAccount +public class BankAccount { + private double balance; + + public BankAccount(double initialBalance) { + this.balance = initialBalance; + } + + public double getBalance() { + return balance; + } + + public void deposit(double amount) { + balance += amount; + } + + public void withdraw(double amount) { + balance -= amount; + } +} +// Main +import java.util.Calendar; + +public class Main { + public static void main(String[] args) { + BankAccount account = new BankAccount(1000); + + // Deposit Transaction + Calendar depositDate = Calendar.getInstance(); + DepositTransaction deposit = new DepositTransaction(500, depositDate, "D001"); + deposit.apply(account); + deposit.printTransactionDetails(); + + // Withdrawal Transaction + Calendar withdrawalDate = Calendar.getInstance(); + WithdrawalTransaction withdrawal = new WithdrawalTransaction(700, withdrawalDate, "W001"); + try { + withdrawal.apply(account); + } catch (InsufficientFundsException e) { + System.out.println(e.getMessage()); + } + withdrawal.printTransactionDetails(); + + // Attempt to reverse withdrawal + withdrawal.reverse(account); + + // Testing insufficient funds + WithdrawalTransaction insufficientWithdrawal = new WithdrawalTransaction(2000, withdrawalDate, "W002"); + try { + insufficientWithdrawal.apply(account); + } catch (InsufficientFundsException e) { + System.out.println(e.getMessage()); + } + } +} + + + + diff --git a/src/Assignment-SCT212-0139/BankAccount.class b/src/Assignment-SCT212-0139/BankAccount.class deleted file mode 100644 index 81dad378f8f031d0b6c9f101ff7e76c03a8fe4c7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 437 zcmZ{g%}N6?6opSZKWS%b|NPmxa5e2L>hY}`LjCPo(m{(;SGaVX$) zd!rhvs7VBMgs2OI1C`wzX`RnADrKTbY9kJ h<11<-{mY~LU{D&+OA~HsDeUC8@D(Ol#5*w4k=WmD(D>Sd=zZFs=G&0@HMxWtYPaIVbLb@b^E*e*viB$24XzYawAHi8+CbFXe%( zxYFCJZ128QT`e$o&+#06UtqSlv6I3)QWnxS-heHTt;s-jhSCdUS3AB(wCwvMk7lA_ zuorA(aZw=AlUfNBTA}}ucHBx`y2|UxVb$RBHZGwckkMz(nhk+`WDq9Q)N8P)!~_;1Q^&77p`E6+lKpC)w6EH@hGl`veYvmdzSoudsiR-; zSD>Y*1A&{x$+#PnS81%^x`i7y-odKCrI6fsKmluD!n>@^!Zfctc`%;IXx$(hQ^Rba zWZ_*K@8K3}GFd}L*;BgX^jYJ4@x(<~yi>ZQPEU-9Mg;TAOyeZVU$O`{q<1!<$ zuyHYLLuu)_lt)q;%Jfxi)ugw-+3gz5S530%wm%wn)s|xtES&p1%BFKPtNFeTSgpY$ zrC<2HU>;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OdGo^ZI_ zO|0&`7X`%-dSpvmJz;2#k8T(UGqk0++@CKL+=^78xleNx2{2*6(orrj1mc;|4HNC? zFmTC26rBvwTI7ndokW^DLKeA4k#>bzlD^`yAQ)ot(QLC<(m*#I&xo??+iKn8(&q(b zyD~KQv7S1NUdoxdedn%;E?m_ht}%?oGv`IC+iDDR%CqHeQbX-RzlG}_Jf*>qR4Lb0zVdi^S*Sg?=!daN)&*N~XW&$b*#Xub z!YdQ6ku$Ka#dyOoRAWzEN2d`AC|>A?v2#z7d6>flhY7%(VU#EM3TW;l2UkShx@*uHhs%5+{EcTs0Owe z`v31rZ$zCJZr~ZcC;Ha{x=IMq_?*7_Xl67A$EsjoX~D2aquvHu5y28(&|15GCTo!9 z{0WTBE<~zm%YMhj@me6AY1gWFi``Vr(|h8=#Hx-oOzi zYRk#|H{2%E{bMA7BRu_^dRXHpmd diff --git a/src/Assignment-SCT212-0139/TransactionInterface.class b/src/Assignment-SCT212-0139/TransactionInterface.class deleted file mode 100644 index 57aba573533f90036ba444bff5b0f039e6902b8a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 352 zcmZ8d%TB{E5F8g8)ADMe0`UjQg?vCyEfEq@r5=hna9fOtT#OSrP9=Vg10TRgAvTsO zMO-{9@9fO%{``J@16blxVjwVk^yC^+!BpxTpy%aAt3pupr8e=(rgtc+F)#cM^?L6@^jWe_y?V!B$AdtWHkMVV*Zl3wq$Z#c*Q=CiTW=Fb6#gc0vX0rr!OqPo4JHJz6A}ZxP;kn{Hi5J@ASD6;Pm}dHEMCv9c4kBK z2l^X$;GGvGhGYh#!|+QOubqsTB!-*@%nX2_p%?=TSF zi36~jg(-&lO{t<*%XM5K^+qI{>c(G=2n^~ZW*8Rx_4$@D$hZ73|MW}vwG^_LGt6_8 zw(x&Uez0*2KQbggdom;nwc@=o97o>52^)(z$uQY1PF#q6rA3F>`4JJGjsv@X=`Phr zQ$u%1#oQrGU5K8e~s{K@9VCW<+*!am1wwfaFm8&ygI7OtZev{nNbytepQD;Yl z+kD3($p{YuZ=WhqsP}c6=73>*OLM2);6cyS3dNoX6*>La_#vU&@}tlZ8?FgDKX44z z3{`Tj_`X&;1!ulx#zbb~EOf&o{guce0^xAy}v z!2D64{hggB!qMga*7ZWrmGrh;Ey@h`rj#PAdR!@?Ec{9R`-amXs1_Ow3*R@TCn4sA z6LfKhXpDhjnh<(FX#K^EHSI^}Kel{kHJ5k;_KFS+ztU=+8<1fV7D+DRx1P)2NKg=9 zedPew@;?}V_8jBykbRJueuLS6SB&TY^RKaBc5kr+9H8(56AcobJ-{lRUQsMy73XN+ zhq~DoDGy-NH;x&kv4Ba^XUXCy&Le{|rf`WYk{F(OU?H2L2$xA3)3`*44BA{T`}r@~ yf9_^ayd-5zYoZH&jsoQ{i+MV&lWqvV;|l#vm8xWUwYy%&Cdo{yKTyYQ= 0). - * - The date must not be null and should accurately reflect the time of the transaction. - * - The transaction ID must be unique for each transaction. - */ + + public class BaseTransaction implements TransactionInterface { protected double amount; // The amount for this transaction protected Calendar date; // The date this transaction occurred diff --git a/src/Assignment-SCT212-0139/DepositTransaction.java b/src/SCT 212-0718/2022 Fidelity kathure/DepositTransaction.java similarity index 84% rename from src/Assignment-SCT212-0139/DepositTransaction.java rename to src/SCT 212-0718/2022 Fidelity kathure/DepositTransaction.java index 9060e5e..9ba3956 100644 --- a/src/Assignment-SCT212-0139/DepositTransaction.java +++ b/src/SCT 212-0718/2022 Fidelity kathure/DepositTransaction.java @@ -1,11 +1,8 @@ import java.util.Calendar; -/** - * DepositTransaction represents a transaction where an amount is deposited into a bank account. - * This class extends BaseTransaction to provide specific behavior for deposit transactions. - * - * - The amount for the deposit must be greater than 0. - */ + + + public class DepositTransaction extends BaseTransaction { /** diff --git a/src/Assignment-SCT212-0139/InsufficientFundsException.java b/src/SCT 212-0718/2022 Fidelity kathure/InsufficientFundsException.java similarity index 87% rename from src/Assignment-SCT212-0139/InsufficientFundsException.java rename to src/SCT 212-0718/2022 Fidelity kathure/InsufficientFundsException.java index 494041d..845512f 100644 --- a/src/Assignment-SCT212-0139/InsufficientFundsException.java +++ b/src/SCT 212-0718/2022 Fidelity kathure/InsufficientFundsException.java @@ -3,4 +3,4 @@ public InsufficientFundsException(String message) { super(message); } } -//insufficient Funds + diff --git a/src/Assignment-SCT212-0139/Main.java b/src/SCT 212-0718/2022 Fidelity kathure/Main.java similarity index 100% rename from src/Assignment-SCT212-0139/Main.java rename to src/SCT 212-0718/2022 Fidelity kathure/Main.java diff --git a/src/Assignment-SCT212-0139/TransactionInterface.java b/src/SCT 212-0718/2022 Fidelity kathure/TransactionInterface.java similarity index 100% rename from src/Assignment-SCT212-0139/TransactionInterface.java rename to src/SCT 212-0718/2022 Fidelity kathure/TransactionInterface.java diff --git a/src/Assignment-SCT212-0139/WithdrawalTransaction.java b/src/SCT 212-0718/2022 Fidelity kathure/WithdrawalTransaction.java similarity index 89% rename from src/Assignment-SCT212-0139/WithdrawalTransaction.java rename to src/SCT 212-0718/2022 Fidelity kathure/WithdrawalTransaction.java index 78b53ad..358e444 100644 --- a/src/Assignment-SCT212-0139/WithdrawalTransaction.java +++ b/src/SCT 212-0718/2022 Fidelity kathure/WithdrawalTransaction.java @@ -1,10 +1,5 @@ import java.util.Calendar; -/** - * WithdrawalTransaction represents a transaction where an amount is withdrawn from a bank account. - * - The amount for the withdrawal must be greater than 0. - * - The transaction should only be applied if sufficient funds are available. - */ public class WithdrawalTransaction extends BaseTransaction { /**