diff --git a/.gitignore b/.gitignore index 3c8b8dd5..7c5067c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,96 +1,98 @@ -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/**/usage.statistics.xml -.idea/**/dictionaries -.idea/**/shelf - -# Generated files -.idea/**/contentModel.xml - -# Sensitive or high-churn files -.idea/**/dataSources/ -.idea/**/dataSources.ids -.idea/**/dataSources.local.xml -.idea/**/sqlDataSources.xml -.idea/**/dynamic.xml -.idea/**/uiDesigner.xml -.idea/**/dbnavigator.xml - -# Gradle -.idea/**/gradle.xml -.idea/**/libraries - -# Gradle and Maven with auto-import -# When using Gradle or Maven with auto-import, you should exclude module files, -# since they will be recreated, and may cause churn. Uncomment if using -# auto-import. -# .idea/modules.xml -# .idea/*.iml -# .idea/modules - -# CMake -cmake-build-*/ - -# Mongo Explorer plugin -.idea/**/mongoSettings.xml - -# File-based project format -*.iws - -# IntelliJ -out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ - -# JIRA plugin -atlassian-ide-plugin.xml - -# Cursive Clojure plugin -.idea/replstate.xml - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties - -# Editor-based Rest Client -.idea/httpRequests - -# Android studio 3.1+ serialized cache file -.idea/caches/build_file_checksums.ser - -##### mac specific - -# General -.DS_Store -.AppleDouble -.LSOverride - -# Icon must end with two \r -Icon - - -# Thumbnails -._* - -# Files that might appear in the root of a volume -.DocumentRevisions-V100 -.fseventsd -.Spotlight-V100 -.TemporaryItems -.Trashes -.VolumeIcon.icns -.com.apple.timemachine.donotpresent - -# Directories potentially created on remote AFP share -.AppleDB -.AppleDesktop -Network Trash Folder -Temporary Items +target/ + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +##### mac specific + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items .apdisk \ No newline at end of file diff --git a/SoftwareEngineeringPractice.iml b/.idea/SoftwareEngineeringPractice.iml similarity index 100% rename from SoftwareEngineeringPractice.iml rename to .idea/SoftwareEngineeringPractice.iml diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 28c6362f..693bf3b1 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,13 +1,14 @@ - - - - - - - - - - - - + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml index b26911bd..11e485db 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,7 @@ - - - - - + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index d87af9ac..8f7dab3a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,9 @@ + + - + \ No newline at end of file diff --git a/.idea/sbt.xml b/.idea/sbt.xml index 20187435..8c004fbf 100644 --- a/.idea/sbt.xml +++ b/.idea/sbt.xml @@ -1,6 +1,6 @@ - - - - + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..9661ac71 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 87c23b2d..16904e5c 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# SoftwareEngineeringPractice -## grading - -To Do | correct ----|--- -at least 8 commits| -isEmailValid| -withdraw| -isamountValid| -constructor & withdraw fix| +# SoftwareEngineeringPractice +## grading + +To Do | correct +---|--- +at least 8 commits| +isEmailValid| +withdraw| +isamountValid| +constructor & withdraw fix| diff --git a/pom.xml b/pom.xml index 416ce09a..edb078b9 100644 --- a/pom.xml +++ b/pom.xml @@ -1,64 +1,64 @@ - - - 4.0.0 - - edu.ithaca.dragon - SoftwareEngineeringPractice - 1.0-SNAPSHOT - - - UTF-8 - 1.10 - - 4.12 - 5.0.0 - ${junit.version}.0 - 5.0.0 - 1.0.0 - - - - - - maven-compiler-plugin - 3.1 - - ${java.version} - ${java.version} - - - - - - - - - org.junit.jupiter - junit-jupiter-engine - ${junit.jupiter.version} - test - - - - junit - junit - ${junit.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - org.junit.vintage - junit-vintage-engine - ${junit.vintage.version} - test - - - + + + 4.0.0 + + edu.ithaca.dragon + SoftwareEngineeringPractice + 1.0-SNAPSHOT + + + UTF-8 + 1.10 + + 4.12 + 5.0.0 + ${junit.version}.0 + 5.0.0 + 1.0.0 + + + + + + maven-compiler-plugin + 3.1 + + ${java.version} + ${java.version} + + + + + + + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + + junit + junit + ${junit.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + org.junit.vintage + junit-vintage-engine + ${junit.vintage.version} + test + + + \ No newline at end of file diff --git a/src/main/java/edu/ithaca/dragon/bank/Account.java b/src/main/java/edu/ithaca/dragon/bank/Account.java new file mode 100644 index 00000000..8ff0a3d6 --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/bank/Account.java @@ -0,0 +1,25 @@ +package edu.ithaca.dragon.bank; + + +public abstract class Account { + protected String ID; + protected double balance; + protected boolean frozen; + + + + public Account(String IDIn, double startingBalance){ + ID = IDIn; + balance = startingBalance; + frozen = false; + } + public double getBalance(){return balance;} + public String getID(){return ID;} + public boolean getFrozen(){return frozen;} + public void toggleFrozen(){frozen = !frozen;} + public abstract void deposit(double amount); + public abstract void withdraw(double amount) throws InsufficientFundsException, IllegalArgumentException; + public abstract void transfer(Account transferTo, double amount) throws InsufficientFundsException; + + +} diff --git a/src/main/java/edu/ithaca/dragon/bank/AdminAPI.java b/src/main/java/edu/ithaca/dragon/bank/AdminAPI.java new file mode 100644 index 00000000..b11e2b4e --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/bank/AdminAPI.java @@ -0,0 +1,15 @@ +package edu.ithaca.dragon.bank; + +import java.util.Collection; + +public interface AdminAPI { + + public double calcTotalAssets(); + + public Collection findAcctIdsWithSuspiciousActivity(); + + public void freezeAccount(String acctId); + + public void unfreezeAcct(String acctId); + +} diff --git a/src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java b/src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java new file mode 100644 index 00000000..26253541 --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java @@ -0,0 +1,9 @@ +package edu.ithaca.dragon.bank; + +//API to be used by Teller systems +public interface AdvancedAPI extends BasicAPI { + + public void createAccount(String acctId, double startingBalance); + + public void closeAccount(String acctId); +} diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index e340e0ea..fe897779 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -1,46 +1,136 @@ -package edu.ithaca.dragon.bank; - -public class BankAccount { - - private String email; - private double balance; - - /** - * @throws IllegalArgumentException if email is invalid - */ - public BankAccount(String email, double startingBalance){ - if (isEmailValid(email)){ - this.email = email; - this.balance = startingBalance; - } - else { - throw new IllegalArgumentException("Email address: " + email + " is invalid, cannot create account"); - } - } - - public double getBalance(){ - return balance; - } - - public String getEmail(){ - return email; - } - - /** - * @post reduces the balance by amount if amount is non-negative and smaller than balance - */ - public void withdraw (double amount) { - balance -= amount; - - } - - - public static boolean isEmailValid(String email){ - if (email.indexOf('@') == -1){ - return false; - } - else { - return true; - } - } -} +package edu.ithaca.dragon.bank; + +import java.util.ArrayList; + +public class BankAccount { + + private String email; + private double balance; + + /** + * @throws IllegalArgumentException if email is invalid + */ + public BankAccount(String email, double startingBalance){ + if (isEmailValid(email)) this.email = email; + else throw new IllegalArgumentException("Email address: " + email + " is invalid, cannot create account"); + if (isAmountValid(startingBalance)) this.balance = startingBalance; + else throw new IllegalArgumentException(Double.toString(startingBalance)+" is an invalid starting Balance"); + } + + public double getBalance(){ + return balance; + } + + public String getEmail(){ + return email; + } + + /** + * @post reduces the balance by amount if amount is non-negative and smaller than balance + if the amount is less than 0 + print out "Amount wanted is less than 0" error + Don't change the original balance + + if the amount is 0 + return the balance as is/do nothing and have the subtraction be 0 + + if the amount is greater than the current balance + print out "Amount wanted is greater than the current balance" error + Don't change the original balance + + if all past if statements failed/was not used + subtract the amount from the current balance regularly + */ + public void withdraw (double amount) throws InsufficientFundsException, IllegalArgumentException{ + if (Double.compare(amount, 0.0)==0) throw new IllegalArgumentException("Cannot withdraw zero dollars"); + if (!isAmountValid(amount)) throw new IllegalArgumentException(Double.toString(amount)+" is not a valid withdraw amount"); + if(amount > balance) throw new InsufficientFundsException("Not enough Money"); + else balance -= amount; + } + + /** + * Returns true if amountIn is positive and has 2 or less decimal places. Returns false otherwise. + * @param amountIn + * @return + */ + public static boolean isAmountValid(double amountIn){ + if (amountIn < 0) return false; + double scale = Math.pow(10, 9); + amountIn = Math.round(amountIn*scale)/scale; + if(Double.compare(amountIn, Math.round(amountIn*100)/100.0)!= 0) return false; + else return true; + } + + /** + * returns true if email is valid and false if not + * @param email + * @return if email is valid + */ + public static boolean isEmailValid(String email){ + ArrayList validChar = validCharsList(); + + + int atId = email.indexOf('@'); //gets the index of the first @ + if(atId== -1) return false; //if no @ invalid email + if(email.lastIndexOf('@')!=atId) return false; // if last @ is not the first @ invalid email + if(atId == 0||atId == email.length()-1) return false; // if the @ is the first or the last char the email is invalid + + int lastPeriod = email.lastIndexOf('.'); + if(lastPeriod==-1)return false;//if there is no period there is no extension + if(lastPeriod validCharsList (){ + ArrayList validChar = new ArrayList(); + for(int x = 0; x < 26; x++){ + validChar.add((char)(x+65)); + validChar.add((char)(x+97)); + } + for(int x = 0; x < 10; x++){ + validChar.add((char)(x+'0')); + } + validChar.add('-'); + validChar.add('_'); + validChar.add('@'); + validChar.add('.'); + return validChar; + } + + /** + * If non zero valid amount add amount ot balance + * @param amount + * @throws IllegalArgumentException + */ + public void deposit(double amount) throws IllegalArgumentException{ + if(Double.compare(amount, 0)==0)throw new IllegalArgumentException("Cannot deposit zero dollars"); + if(!isAmountValid(amount))throw new IllegalArgumentException(Double.toString(amount)+" is not a valid deposit amount"); + else balance+=amount; + } + + /** + * transfers amount from this to transferTo if amount is non zero and valid + * @param amount + * @param transferTo + * @throws IllegalArgumentException + */ + public void transfer(double amount, BankAccount transferTo) throws IllegalArgumentException, InsufficientFundsException { + this.withdraw(amount); + transferTo.deposit(amount); + } +} diff --git a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java new file mode 100644 index 00000000..bd65b941 --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java @@ -0,0 +1,18 @@ +package edu.ithaca.dragon.bank; + +//API to be used by ATMs +public interface BasicAPI { + + boolean confirmCredentials(String acctId, String password); + + double checkBalance(String acctId); + + void withdraw(String acctId, double amount) throws InsufficientFundsException; + + void deposit(String acctId, double amount); + + void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, double amount) throws InsufficientFundsException; + + String transactionHistory(String acctId); + +} diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java new file mode 100644 index 00000000..7f6eba4f --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -0,0 +1,63 @@ +package edu.ithaca.dragon.bank; + +import java.util.Collection; + +public class CentralBank implements AdvancedAPI, AdminAPI { + + //----------------- BasicAPI methods -------------------------// + + public boolean confirmCredentials(String acctId, String password) { + return false; + } + + public double checkBalance(String acctId) { + return 0; + } + + public void withdraw(String acctId, double amount) throws InsufficientFundsException { + + } + + public void deposit(String acctId, double amount) { + + } + + public void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, double amount) throws InsufficientFundsException { + + } + + public String transactionHistory(String acctId) { + return null; + } + + + //----------------- AdvancedAPI methods -------------------------// + + public void createAccount(String acctId, double startingBalance) { + + } + + public void closeAccount(String acctId) { + + } + + + //------------------ AdminAPI methods -------------------------// + + public double calcTotalAssets() { + return 0; + } + + public Collection findAcctIdsWithSuspiciousActivity() { + return null; + } + + public void freezeAccount(String acctId) { + + } + + public void unfreezeAcct(String acctId) { + + } + +} diff --git a/src/main/java/edu/ithaca/dragon/bank/CheckingAccount.java b/src/main/java/edu/ithaca/dragon/bank/CheckingAccount.java new file mode 100644 index 00000000..57ba72f3 --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/bank/CheckingAccount.java @@ -0,0 +1,29 @@ +package edu.ithaca.dragon.bank; + +import java.math.BigDecimal; + +public class CheckingAccount extends Account { + + + public CheckingAccount(String ID, double balance){ + super(ID, balance); + } + public void deposit(double amount){} + public void withdraw(double amount) throws InsufficientFundsException, IllegalArgumentException{ + if(!CheckingAccount.isAmountValid(amount))throw new IllegalArgumentException("Not a valid Amount"); + if(Double.compare(amount, 0.0)==0)throw new IllegalArgumentException("Cannot Withdraw zero dollars"); + if(Double.compare(this.balance-amount, 0.0)==-1)throw new InsufficientFundsException("Not enough Funds"); + else this.balance -= amount; + + + } + public void transfer(Account transferTo, double amount) throws InsufficientFundsException{} + + public static boolean isAmountValid(double amountIn){ + if (amountIn < 0) return false; + double scale = Math.pow(10, 9); + amountIn = Math.round(amountIn*scale)/scale; + if(Double.compare(amountIn, Math.round(amountIn*100)/100.0)!= 0) return false; + else return true; + } +} diff --git a/src/main/java/edu/ithaca/dragon/bank/InsufficientFundsException.java b/src/main/java/edu/ithaca/dragon/bank/InsufficientFundsException.java index 8c794be8..eabc6863 100644 --- a/src/main/java/edu/ithaca/dragon/bank/InsufficientFundsException.java +++ b/src/main/java/edu/ithaca/dragon/bank/InsufficientFundsException.java @@ -1,9 +1,9 @@ -package edu.ithaca.dragon.bank; - -public class InsufficientFundsException extends Exception{ - - public InsufficientFundsException(String s){ - super(s); - } - +package edu.ithaca.dragon.bank; + +public class InsufficientFundsException extends Exception{ + + public InsufficientFundsException(String s){ + super(s); + } + } \ No newline at end of file diff --git a/src/test/java/edu/ithaca/dragon/bank/AccountTest.java b/src/test/java/edu/ithaca/dragon/bank/AccountTest.java new file mode 100644 index 00000000..cd6743f7 --- /dev/null +++ b/src/test/java/edu/ithaca/dragon/bank/AccountTest.java @@ -0,0 +1,61 @@ +package edu.ithaca.dragon.bank; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class AccountTest { + @Test + void withdrawTest() throws InsufficientFundsException { + + CheckingAccount account1 = new CheckingAccount("a@b.com", 200); + account1.withdraw(100); + + assertEquals(100, account1.getBalance()); + + + //Equivalence Class-InsufficientFundsException + assertThrows(IllegalArgumentException.class, ()-> account1.withdraw(0)); + + assertThrows(InsufficientFundsException.class, () -> account1.withdraw(101)); + + + //Equivalence Class-IllegalArgumentException more than 2 decimal places + assertThrows(IllegalArgumentException.class, () -> account1.withdraw(1045.078)); + //Equivalence Class-IllegalArgumentException more than 2 decimal places + assertThrows(IllegalArgumentException.class, () -> account1.withdraw(1.02839)); + + + //Equivalence Class-Invalid withdraw amount 0 + assertThrows(IllegalArgumentException.class, () -> account1.withdraw(0)); + + //Equivalence Class-IllegalArgumentException negative amount + assertThrows(IllegalArgumentException.class, () -> account1.withdraw(-1)); + + //Equivalence Class-IllegalArgumentException negative amount + assertThrows(IllegalArgumentException.class, () -> account1.withdraw(-10)); + //Equivalence Class-IllegalArgumentException exceeds balance + assertThrows(InsufficientFundsException.class, () -> account1.withdraw(1000)); + + + //Equivalence Class- withdraw $1 + account1.withdraw(1); + assertEquals(99, account1.getBalance()); + + //Equivalence Class- withdraw $.1 + account1.withdraw(.1); + assertEquals(98.9, account1.getBalance()); + + //Equivalence Class- withdraw $.01 + account1.withdraw(.01); + assertEquals(98.89, account1.getBalance()); + + //Equivalence Class No balance + CheckingAccount account2 = new CheckingAccount("a@c.cm", 0); + assertThrows(InsufficientFundsException.class, ()-> account2.withdraw(1)); + assertEquals(0, account2.getBalance()); + + + + } +} diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index d19ecb02..8371d7ac 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -1,40 +1,190 @@ -package edu.ithaca.dragon.bank; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -class BankAccountTest { - - @Test - void getBalanceTest() { - BankAccount bankAccount = new BankAccount("a@b.com", 200); - - assertEquals(200, bankAccount.getBalance()); - } - - @Test - void withdrawTest() { - BankAccount bankAccount = new BankAccount("a@b.com", 200); - bankAccount.withdraw(100); - - assertEquals(100, bankAccount.getBalance()); - } - - @Test - void isEmailValidTest(){ - assertTrue(BankAccount.isEmailValid( "a@b.com")); - assertFalse( BankAccount.isEmailValid("")); - } - - @Test - void constructorTest() { - BankAccount bankAccount = new BankAccount("a@b.com", 200); - - assertEquals("a@b.com", bankAccount.getEmail()); - assertEquals(200, bankAccount.getBalance()); - //check for exception thrown correctly - assertThrows(IllegalArgumentException.class, ()-> new BankAccount("", 100)); - } - +package edu.ithaca.dragon.bank; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class BankAccountTest { + + @Test + void getBalanceTest() { + //Equivalence Class starting positive balance + BankAccount bankAccount = new BankAccount("a@b.com", 200); + assertEquals(200, bankAccount.getBalance()); + bankAccount = new BankAccount("a@b.com", 0); + assertEquals(0, bankAccount.getBalance()); + } + + @Test + void withdrawTest() throws InsufficientFundsException{ + BankAccount bankAccount = new BankAccount("a@b.com", 200); + bankAccount.withdraw(100); + + assertEquals(100, bankAccount.getBalance()); + + //Equivalence Class Has balance + BankAccount bankAccount1 = new BankAccount("a@c.com", 300); + bankAccount1.withdraw(99);//valid withdraw + assertEquals(201, bankAccount1.getBalance()); + bankAccount1.withdraw(1);//valid withdraw edgecase + assertEquals(200, bankAccount1.getBalance()); + bankAccount1.withdraw(0.1); + assertEquals(199.9, bankAccount1.getBalance(),10); + bankAccount1.withdraw(0.01); + assertEquals(199.89, bankAccount1.getBalance(), 10); + assertThrows(InsufficientFundsException.class, ()-> bankAccount1.withdraw(201)); + assertEquals(199.89, bankAccount1.getBalance(), 10); + assertThrows(InsufficientFundsException.class, ()-> bankAccount1.withdraw(20100)); + assertEquals(199.89, bankAccount1.getBalance(), 10); + bankAccount1.withdraw(199.89);//perfect withdraw edgecase + assertEquals(0, bankAccount1.getBalance(), 10); + + //Equivalence Class No balance + BankAccount ba2 = new BankAccount("a@c.cm", 0); + assertThrows(InsufficientFundsException.class, ()-> ba2.withdraw(1)); + assertEquals(0, ba2.getBalance()); + + BankAccount ba3 = new BankAccount("a@c.cm", 200); + assertThrows(IllegalArgumentException.class, ()-> ba3.withdraw(0)); + assertThrows(IllegalArgumentException.class, ()-> ba3.withdraw(1.001)); + assertThrows(IllegalArgumentException.class, ()-> ba3.withdraw(-14)); + assertThrows(IllegalArgumentException.class, ()-> ba3.withdraw(-0.001)); + + } + + @Test + void isEmailValidTest(){ + assertTrue(BankAccount.isEmailValid( "a@b.com")); + assertFalse( BankAccount.isEmailValid("")); + + //Partitions of ""=invalid partition, address first, domain second, extension third + + //Equivalence class being in first partitions, with boundary value to index[0]->indexOf(@) + //Multiple periods Partition tests + assertFalse(BankAccount.isEmailValid("a..b@male.com")); + assertTrue(BankAccount.isEmailValid("a.f.a@tah.com")); + assertTrue(BankAccount.isEmailValid("a.ffff.a@gasd.asd")); + assertFalse(BankAccount.isEmailValid("a...f@asdf.asd")); + assertFalse(BankAccount.isEmailValid("abc.def@mail..com")); //Only one is allowed in the second half regardless + + //Slash partitionTest + assertFalse(BankAccount.isEmailValid("abc-@mail.com")); + assertTrue(BankAccount.isEmailValid("abc-d@mail.com")); + + //Invalid period Location + assertFalse(BankAccount.isEmailValid(".abc@mail.com")); + assertTrue(BankAccount.isEmailValid("a.a@gasd.com")); + assertTrue(BankAccount.isEmailValid("abc.def@mail.com")); + + //Invalid Character Partitions + assertFalse(BankAccount.isEmailValid("abc#def@mail.com")); + assertFalse(BankAccount.isEmailValid("abc.def@mail#archive.com")); + assertTrue(BankAccount.isEmailValid("abc@mail.com")); + assertTrue(BankAccount.isEmailValid("abc_def@mail.com")); + + //Equivalence class Short or missing extentions + assertFalse(BankAccount.isEmailValid("abc.def@mail.c")); + assertFalse(BankAccount.isEmailValid("bc.def@mail")); + assertFalse(BankAccount.isEmailValid("asd@mail.")); + assertTrue(BankAccount.isEmailValid("asd@asd.cc")); + assertTrue(BankAccount.isEmailValid("asd@asd.ccc")); + + //Equivalence Class different extentions + assertTrue(BankAccount.isEmailValid("abc.def@mail-archive.com")); + assertTrue(BankAccount.isEmailValid("abc.def@mail.org")); + assertTrue(BankAccount.isEmailValid("abc.def@mail.com")); + + //Equivalence Class @ symbols + assertFalse(BankAccount.isEmailValid("as@as@asd.com")); + assertFalse(BankAccount.isEmailValid("ASDF@ADSA.F@ASCOM")); + assertFalse(BankAccount.isEmailValid("asdfasdfsadf.sad")); + assertFalse(BankAccount.isEmailValid("asdfasdfasdf")); + + assertFalse(BankAccount.isEmailValid("asd%@asddff.coc")); + assertFalse(BankAccount.isEmailValid("asd@asddff.coco")); + } + + @Test + void constructorTest() { + BankAccount bankAccount = new BankAccount("a@b.com", 200); + assertEquals("a@b.com", bankAccount.getEmail()); + assertEquals(200, bankAccount.getBalance()); + //check for exception thrown correctly + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("", 100)); + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a@basd.com", 100.001)); + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a@basd.com", -100)); + } + + @Test + void isAmountValidTest(){ + assertTrue(BankAccount.isAmountValid(10)); + assertTrue(BankAccount.isAmountValid(10.0)); + assertTrue(BankAccount.isAmountValid(10.00)); + assertTrue(BankAccount.isAmountValid(10.000000000)); + assertTrue(BankAccount.isAmountValid(10.1)); + assertTrue(BankAccount.isAmountValid(10.11)); + assertTrue(BankAccount.isAmountValid(0.0000000)); + assertTrue(BankAccount.isAmountValid(0.1)); + assertTrue(BankAccount.isAmountValid(0.01)); + assertTrue(BankAccount.isAmountValid(0.00000000001)); + + assertFalse(BankAccount.isAmountValid(10.001)); + assertFalse(BankAccount.isAmountValid(10.0001)); + assertFalse(BankAccount.isAmountValid(10.00000001)); + assertFalse(BankAccount.isAmountValid(-10)); + assertFalse(BankAccount.isAmountValid(-10.0)); + assertFalse(BankAccount.isAmountValid(-10.00)); + assertFalse(BankAccount.isAmountValid(-10.00000)); + assertFalse(BankAccount.isAmountValid(-10.1)); + assertFalse(BankAccount.isAmountValid(-10.01)); + assertFalse(BankAccount.isAmountValid(-10.001)); + assertFalse(BankAccount.isAmountValid(-0.1)); + assertFalse(BankAccount.isAmountValid(-0.01)); + assertFalse(BankAccount.isAmountValid(-0.001)); + } + + @Test + void depositTest(){ + BankAccount bankAccount = new BankAccount("a@a.cc", 0); + + bankAccount.deposit(100); + assertEquals(100, bankAccount.getBalance(), 10); + bankAccount.deposit(0.1); + assertEquals(100.1, bankAccount.getBalance(), 10); + bankAccount.deposit(0.01); + assertEquals(100.11, bankAccount.getBalance(), 10); + + assertThrows(IllegalArgumentException.class, ()-> bankAccount.deposit(-1)); + assertThrows(IllegalArgumentException.class, ()-> bankAccount.deposit(0)); + assertThrows(IllegalArgumentException.class, ()-> bankAccount.deposit(-1.01)); + assertThrows(IllegalArgumentException.class, ()-> bankAccount.deposit(1.001)); + assertThrows(IllegalArgumentException.class, ()-> bankAccount.deposit(-1.001)); + } + + @Test + void transferTest() throws InsufficientFundsException{ + BankAccount bankAccount = new BankAccount("a@a.cc", 300); + BankAccount bankAccount1 = new BankAccount("A@AA.CC", 0); + + bankAccount.transfer(100, bankAccount1); + assertEquals(200, bankAccount.getBalance(), 10); + assertEquals(100, bankAccount1.getBalance(), 10); + bankAccount.transfer(0.1, bankAccount1); + assertEquals(199.9, bankAccount.getBalance(), 10); + assertEquals(100.1, bankAccount1.getBalance(), 10); + bankAccount.transfer(0.01, bankAccount1); + assertEquals(199.89, bankAccount.getBalance(), 10); + assertEquals(100.11, bankAccount1.getBalance(), 10); + + assertThrows(IllegalArgumentException.class, ()-> bankAccount.transfer(0, bankAccount1)); + assertThrows(IllegalArgumentException.class, ()-> bankAccount.transfer(-1, bankAccount1)); + assertThrows(IllegalArgumentException.class, ()-> bankAccount.transfer(-1.01, bankAccount1)); + assertThrows(IllegalArgumentException.class, ()-> bankAccount.transfer(-1.0001, bankAccount1)); + assertThrows(IllegalArgumentException.class, ()-> bankAccount.transfer(0.001, bankAccount1)); + + assertThrows(InsufficientFundsException.class, ()->bankAccount.transfer(1000, bankAccount1)); + } + + + } \ No newline at end of file diff --git a/src/test/java/edu/ithaca/dragon/bank/CheckingAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/CheckingAccountTest.java new file mode 100644 index 00000000..2ec0375b --- /dev/null +++ b/src/test/java/edu/ithaca/dragon/bank/CheckingAccountTest.java @@ -0,0 +1,45 @@ +package edu.ithaca.dragon.bank; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class CheckingAccountTest { + @Test + void withdrawCheckingTest() throws InsufficientFundsException{ + CheckingAccount bankAccount = new CheckingAccount("a@b.com", 200); + bankAccount.withdraw(100); + + assertEquals(100, bankAccount.getBalance()); + + //Equivalence Class Has balance + BankAccount bankAccount1 = new BankAccount("a@c.com", 300); + bankAccount1.withdraw(99);//valid withdraw + assertEquals(201, bankAccount1.getBalance()); + bankAccount1.withdraw(1);//valid withdraw edgecase + assertEquals(200, bankAccount1.getBalance()); + bankAccount1.withdraw(0.1); + assertEquals(199.9, bankAccount1.getBalance(),10); + bankAccount1.withdraw(0.01); + assertEquals(199.89, bankAccount1.getBalance(), 10); + assertThrows(InsufficientFundsException.class, ()-> bankAccount1.withdraw(201)); + assertEquals(199.89, bankAccount1.getBalance(), 10); + assertThrows(InsufficientFundsException.class, ()-> bankAccount1.withdraw(20100)); + assertEquals(199.89, bankAccount1.getBalance(), 10); + bankAccount1.withdraw(199.89);//perfect withdraw edgecase + assertEquals(0, bankAccount1.getBalance(), 10); + + //Equivalence Class No balance + CheckingAccount ba2 = new CheckingAccount("a@c.cm", 0); + assertThrows(InsufficientFundsException.class, ()-> ba2.withdraw(1)); + assertEquals(0, ba2.getBalance()); + + CheckingAccount ba3 = new CheckingAccount("a@c.cm", 200); + assertThrows(IllegalArgumentException.class, ()-> ba3.withdraw(0)); + assertThrows(IllegalArgumentException.class, ()-> ba3.withdraw(1.001)); + assertThrows(IllegalArgumentException.class, ()-> ba3.withdraw(-14)); + assertThrows(IllegalArgumentException.class, ()-> ba3.withdraw(-0.001)); + + } +}