diff --git a/.gitignore b/.gitignore index 3c8b8dd5..fd0406a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 +target/ # User-specific stuff .idea/**/workspace.xml diff --git a/.idea/SCentralBankFunctioning.iml b/.idea/SCentralBankFunctioning.iml new file mode 100644 index 00000000..78b2cc53 --- /dev/null +++ b/.idea/SCentralBankFunctioning.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/SoftwareEngineeringPractice.iml b/.idea/SoftwareEngineeringPractice.iml new file mode 100644 index 00000000..78b2cc53 --- /dev/null +++ b/.idea/SoftwareEngineeringPractice.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/TobySoftwareEngineeringPractice.iml b/.idea/TobySoftwareEngineeringPractice.iml new file mode 100644 index 00000000..78b2cc53 --- /dev/null +++ b/.idea/TobySoftwareEngineeringPractice.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 28c6362f..a2eb181d 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,6 +6,8 @@ + + diff --git a/.idea/encodings.xml b/.idea/encodings.xml index b26911bd..fade66b8 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index d87af9ac..dd62e066 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,9 @@ + + - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..f9bd5ad9 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 87c23b2d..281acb71 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,15 @@ # SoftwareEngineeringPractice ## grading +UML: +https://www.draw.io/#G1ClJfvlx-DafIY19BRQ7PtKhMV5oUpCB4 -To Do | correct ----|--- -at least 8 commits| -isEmailValid| -withdraw| -isamountValid| -constructor & withdraw fix| +Use Case: +https://www.draw.io/#G16QeH0AdywvUHLzUHYaT50bWsE6ocJg3M + +Sequence: + + TransHistory: + https://www.draw.io/#G1QVD4o9ISDGrVABXhoTN2-LIzO8AcCr5z + + CheckBal: + https://www.draw.io/#G1Hd51PWDOxilkn22_zhSnwhkEnVxUM0fU diff --git a/SoftwareEngineeringPractice b/SoftwareEngineeringPractice new file mode 160000 index 00000000..0e66053d --- /dev/null +++ b/SoftwareEngineeringPractice @@ -0,0 +1 @@ +Subproject commit 0e66053d631a7d2f8c3ca50e454ec1b7616c31fb diff --git a/pom.xml b/pom.xml index 416ce09a..bb6d3249 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 - 1.10 + 1.8 4.12 5.0.0 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..647a8c5d --- /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, String password); + + 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..0845dd70 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -1,46 +1,330 @@ package edu.ithaca.dragon.bank; +import java.util.HashSet; + public class BankAccount { - private String email; - private double balance; + public String email; + public double balance; + private String password; + private int depositCount; + private int withdrawCount; + private int transferCount; + + + /** + * @throws IllegalArgumentException if amount to be withdrawn is invalid + * Returns a boolean + */ + private boolean isAmountValid(double amount) throws IllegalArgumentException{ + String numString = Double.toString(amount); + int length = numString.length(); + + if (length > 3){ //only runs this test if amount has more than 3 chars + int period = numString.lastIndexOf("."); + if (length > (period + 3)) throw new IllegalArgumentException("The amount you entered " + amount + " is invalid because it has more than three decimal places."); + } + + else if (amount < 0){ //checks ifd the number is negative + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid because it is negative"); + } + + return true; // returns true if amount is valid + } /** * @throws IllegalArgumentException if email is invalid */ - public BankAccount(String email, double startingBalance){ + public BankAccount(String email, double startingBalance, String password){ + + if (isAmountValid(startingBalance) == false){ + throw new IllegalArgumentException("Starting balance is an invalid balance because it is negative or has too many decimal places"); + } if (isEmailValid(email)){ this.email = email; this.balance = startingBalance; + this.password = password; + this.depositCount = 0; + this.withdrawCount = 0; + this.transferCount = 0; } else { throw new IllegalArgumentException("Email address: " + email + " is invalid, cannot create account"); } + } public double getBalance(){ return balance; } + public String getPassword(){ return password; } + public String getEmail(){ return email; } + public int getDepositCount(){ + return depositCount; + } + public int getWithdrawCount(){ + return withdrawCount; + } + public int getTransferCount(){ + return transferCount; + } + + /** + * @post transfers an amount from one account to the other. + * parameters: bankaccount1, bankaccount2, transfer amount + * @throws IllegalArgumentException if deposit amount is invalid + * + */ + public void transfer ( BankAccount bankAccountTranferringTo, double amount) throws InsufficientFundsException, IllegalArgumentException{ + if (isAmountValid(amount) != true){ + throw new IllegalArgumentException ("The amount you entered: "+ amount + " is not a valid amount"); + } + + else if (balance < amount){ + throw new InsufficientFundsException("You do not have enough money in your account to make this transfer"); + } + else if (amount == 0 || amount < 0){ + throw new IllegalArgumentException("You cannot transfer $0 or less"); + } + + else { + balance -= amount; + transferCount ++; + bankAccountTranferringTo.balance += amount; + bankAccountTranferringTo.transferCount++; + } + } + + /** + * @post increases the balance by the amount deposited + * parameters: deposit amount + * @throws IllegalArgumentException if deposit amount is invalid + * + */ + + public void deposit (double amount) throws IllegalArgumentException { + if (isAmountValid(amount) == false){ + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); + } + else if (amount == 0){ + throw new IllegalArgumentException("You cannot deposit $0"); + } + else { + balance += amount; + depositCount ++; + } + } /** * @post reduces the balance by amount if amount is non-negative and smaller than balance + * throws IllegalArgument if the amount is more than the balance + * for negative numbers return the unchanged balance + * returns the balance if the withdraw amount is less than your balance and a positive number. */ - public void withdraw (double amount) { - balance -= amount; + public void withdraw (double amount) throws IllegalArgumentException, InsufficientFundsException { + if (isAmountValid(amount) == false){ + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); + } + if (amount < .01) { + throw new IllegalArgumentException("Cannot withdraw $0 or less"); + } + else if (balance >= amount) { + balance -= amount; + withdrawCount++; + } + else if (balance < amount) { + throw new InsufficientFundsException("Cannot draw more than account balance."); + } } + /** + * returns true if email follows correct conventions. + */ + + public static boolean isEmailValid(String email) { + + //declares a variable with the locations of the @ signs in the email. + int atSign = email.indexOf('@'); + int lastAtSign = email.lastIndexOf('@'); + + //makes sure that an @ sign is in the email. + if (atSign == -1) + return false; + + //makes sure that there is only one @ sign. + if (atSign != lastAtSign) + return false; + + //Makes sure that a special character is not followed by a "@" + if (email.charAt(lastAtSign-1) == '.') + return false; + + if (email.charAt(lastAtSign-1) == '_') + return false; + + if (email.charAt(lastAtSign-1) == '-') + return false; + + //declares a variable with the location of the last "." in the email + int lastPeriodSign = email.lastIndexOf('.'); + + + //makes sure that there is a '.' in the email. + if (lastPeriodSign == -1) + return false; + + //makes sure that there is a period after @ + if (email.charAt(lastAtSign) < email.charAt(lastPeriodSign)) + return false; + + //makes sure that there is not two periods, dashes, or underscores back to back in the email + int x = 0; + int lengthOfEmail = email.length(); + + int locationOfLastPeriod = -1000; + int locationOfCurrentPeriod; - public static boolean isEmailValid(String email){ - if (email.indexOf('@') == -1){ + int locationOfLastDash = -2; + int locationOfCurrentDash; + + int locationOfLastUnderscore = -2; + int locationOfCurrentUnderscore; + + //makes sure the period follows a .com or .cc or .org or .edu etc... + if (lastPeriodSign != lengthOfEmail - 3 && lastPeriodSign != lengthOfEmail - 4) return false; + + while (x < (email.length()-1)){ + + //checks to make sure the email does not start with a period + if(email.charAt(x) == '.' && x == 0) + return false; + //checks for double periods + if (email.charAt(x) =='.' && x == (locationOfLastPeriod+1)) + return false; + if(email.charAt(x) =='.') + locationOfLastPeriod = x; + + //checks for double underscores + if(email.charAt(x) == '_' && x == 0) + return false; + if (email.charAt(x) == locationOfLastUnderscore) + return false; + else if(email.charAt(x) =='_') + locationOfLastUnderscore = x; + + //checks for double dashes + if(email.charAt(x) == '-' && x == 0) + return false; + if (email.charAt(x) == locationOfLastDash) + return false; + else if(email.charAt(x) =='-') + locationOfLastDash = x; + + x++; } - else { - return true; + + + + //makes sure there is not an invalid character + int j = 0; + HashSet validCharSet = getValidCharSet(); + while (j < lengthOfEmail) { + + + if (!validCharSet.contains(email.charAt(j))) return false; + + + if (isSpecialChar(email.charAt(j))){ + //if leading or last character is special, return false + if (j == 0 || j == lengthOfEmail - 1) return false; + //makes sure theres no double special characters + if (isSpecialChar(email.charAt(j + 1))) return false; + } + //go back one letter + j++; } + + return true; + } -} + private static boolean isSpecialChar(char c) { return c == '.' || c == '-' || c == '_' || c == '@'; } + + private static HashSet getValidCharSet(){ + HashSet validCharSet = new HashSet<>(); + validCharSet.add('@'); + validCharSet.add('.'); + validCharSet.add('_'); + validCharSet.add('-'); + validCharSet.add('q'); + validCharSet.add('w'); + validCharSet.add('e'); + validCharSet.add('r'); + validCharSet.add('t'); + validCharSet.add('y'); + validCharSet.add('u'); + validCharSet.add('i'); + validCharSet.add('o'); + validCharSet.add('p'); + validCharSet.add('a'); + validCharSet.add('s'); + validCharSet.add('d'); + validCharSet.add('f'); + validCharSet.add('g'); + validCharSet.add('h'); + validCharSet.add('j'); + validCharSet.add('k'); + validCharSet.add('l'); + validCharSet.add('z'); + validCharSet.add('x'); + validCharSet.add('c'); + validCharSet.add('v'); + validCharSet.add('b'); + validCharSet.add('n'); + validCharSet.add('m'); + validCharSet.add('Q'); + validCharSet.add('W'); + validCharSet.add('E'); + validCharSet.add('R'); + validCharSet.add('T'); + validCharSet.add('Y'); + validCharSet.add('U'); + validCharSet.add('I'); + validCharSet.add('O'); + validCharSet.add('P'); + validCharSet.add('A'); + validCharSet.add('S'); + validCharSet.add('D'); + validCharSet.add('F'); + validCharSet.add('G'); + validCharSet.add('H'); + validCharSet.add('J'); + validCharSet.add('K'); + validCharSet.add('L'); + validCharSet.add('Z'); + validCharSet.add('X'); + validCharSet.add('C'); + validCharSet.add('V'); + validCharSet.add('B'); + validCharSet.add('N'); + validCharSet.add('M'); + validCharSet.add('1'); + validCharSet.add('2'); + validCharSet.add('3'); + validCharSet.add('4'); + validCharSet.add('5'); + validCharSet.add('6'); + validCharSet.add('7'); + validCharSet.add('8'); + validCharSet.add('9'); + validCharSet.add('0'); + + return validCharSet; + } + +} \ No newline at end of file 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..b54a74bb --- /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) throws IllegalArgumentException; + + 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..96176cd0 --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -0,0 +1,234 @@ +package edu.ithaca.dragon.bank; + +import java.util.Collection; +import java.util.HashMap; + + +public class CentralBank implements AdvancedAPI, AdminAPI { + + private HashMap customerCollection = new HashMap(); + + public CentralBank (){ + + } + + private boolean isAmountValid(double amount) throws IllegalArgumentException{ + String numString = Double.toString(amount); + int length = numString.length(); + + if (length > 3){ //only runs this test if amount has more than 3 chars + int period = numString.lastIndexOf("."); + if (length > (period + 3)){ + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid because it has more than three decimal places."); + } + } + else if (amount < 0){ //checks ifd the number is negative + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid because it is negative"); + } + + return true; // returns true if amount is valid + + } + + //----------------- BasicAPI methods -------------------------// + + public boolean confirmCredentials(String acctId, String password) { + for (int i = 0; i < customerCollection.size(); i++) { + if (customerCollection.containsKey(acctId)) { + BankAccount bankAccount = customerCollection.get(acctId); + if (bankAccount.getPassword() == password){ + return true; + } + } + } + return false; + } + + @Override + public double checkBalance(String acctId) { + if (customerCollection.containsKey(acctId)) { + BankAccount bankAccount = customerCollection.get(acctId); + return bankAccount.getBalance(); + } + throw new IllegalArgumentException("please provide valid accout ID"); + } + + @Override + public void withdraw(String acctId, double amount) throws InsufficientFundsException { + BankAccount bankAccount = customerCollection.get(acctId); + + if (isAmountValid(amount) != true) { + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); + } + if (amount < .01){ //checks that withdraw amount isnt 0 + throw new IllegalArgumentException("Cannot withdraw $0 or less"); + } + if (bankAccount.getBalance() < amount){ //checks that you have sufficient funds for the withdraw. + throw new InsufficientFundsException("Cannot draw more than account balance."); + } + else { + bankAccount.withdraw(amount); + } + + } + + @Override + public void deposit(String acctId, double amount) throws IllegalArgumentException { + BankAccount bankAccount = customerCollection.get(acctId); + if (isAmountValid(amount) != true) { + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); + } + if (amount <= 0){ //checks that deposit amount isn't 0 + throw new IllegalArgumentException("Cannot deposit $0"); + } + if (amount < .01){ //checks that deposit amount isnt 0 + throw new IllegalArgumentException("Cannot deposit less than $0.01"); + } + else { + bankAccount.deposit(amount); + } + + } + + @Override + public void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, double amount) throws InsufficientFundsException { + BankAccount withdrawBankAccount = customerCollection.get(acctIdToWithdrawFrom); + BankAccount depositBankAccount = customerCollection.get(acctIdToDepositTo); + if (isAmountValid(amount) != true) { + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); + } + if (amount == 0) { //checks that amount isn't 0 + throw new IllegalArgumentException("Cannot deposit $0"); + } + if (withdrawBankAccount.balance < amount){ + throw new InsufficientFundsException("amount you wish to withdraw exceeds balance"); + } + else{ + withdrawBankAccount.transfer(depositBankAccount,amount); + } + } + + @Override + public String transactionHistory(String acctId) { + if(checkCustomerCollection(acctId) == false) { + BankAccount bankAccount = customerCollection.get(acctId); + int transCount = bankAccount.getTransferCount(); + int depoCount = bankAccount.getDepositCount(); + int withCount = bankAccount.getWithdrawCount(); + int total = bankAccount.getDepositCount()+bankAccount.getTransferCount()+bankAccount.getWithdrawCount(); + String transHistory = "Total number of transactions done on account: "+total + " (Deposits: " + depoCount+ " Withdraws: "+withCount+ " Transfers: "+transCount+")"; + return transHistory; + } + return null; + } + + @Override + public double calcTotalAssets() { + return 0; + } + + @Override + public Collection findAcctIdsWithSuspiciousActivity() { + return null; + } + + @Override + public void freezeAccount(String acctId) { + + } + + @Override + public void unfreezeAcct(String acctId) { + + } + + @Override + public void createAccount(String acctId, double startingBalance, String password) { + BankAccount bankAccount = new BankAccount(acctId,startingBalance, password); + customerCollection.put(acctId,bankAccount); + } + + @Override + public void closeAccount(String acctId) throws IllegalArgumentException { + if (customerCollection.containsKey(acctId)) { + BankAccount bankAccount = customerCollection.get(acctId); + customerCollection.remove(acctId); + bankAccount = null; + } + else{ + throw new IllegalArgumentException(acctId+ " does not exist"); + } + } + + public boolean checkCustomerCollection(String acctId){ + if (customerCollection.containsKey(acctId)){ + return false; + } + return true; + } + + + /*public double checkBalance(String acctId, BankAccount[] customerCollection) throws IllegalArgumentException { + int length = customerCollection.length; + for(int i =0; i < length; i++){ + if (acctId == customerCollection[i].email) + return customerCollection[i].getBalance(); + } + throw new IllegalArgumentException("The account ID you entered is not in the system"); + } + public void withdraw(String acctId, double amount, BankAccount[] customerCollection) throws InsufficientFundsException { + int length = customerCollection.length; + if (isAmountValid(amount) == false){ + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); + } + if (amount < .01) //checks that withdraw amount isnt 0 + throw new IllegalArgumentException("Cannot withdraw $0 or less"); + for(int i =0; i < length; i++){ + if (acctId == customerCollection[i].email) { + if (customerCollection[i].balance < amount) //checks that you have sufficient funds for the withdraw. + throw new InsufficientFundsException("Cannot draw more than account balance."); + else + customerCollection[i].balance -= amount; //takes out money if everything is good + } + } + } + public void deposit(String acctId, double amount, BankAccount[] customerCollection) { + int length = customerCollection.length; + if (isAmountValid(amount) == false){ + throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); + } + else if (amount == 0){ + throw new IllegalArgumentException("You cannot deposit $0"); + } + else + for(int i =0; i < length; i++){ + if (acctId == customerCollection[i].email) + customerCollection[i].balance += 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 checkTotalAssets() { + return 0; + } + public double calcTotalAssets() { + return 0; + } + public Collection findAcctIdsWithSuspiciousActivity() { + return null; + } + public void freezeAccount(String acctId) { + } + public void unfreezeAcct(String acctId) { + } +*/ +} \ No newline at end of file diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index d19ecb02..1ea6ac00 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -1,3 +1,4 @@ + package edu.ithaca.dragon.bank; import org.junit.jupiter.api.Test; @@ -6,35 +7,233 @@ class BankAccountTest { + @Test + void transferTest() throws InsufficientFundsException { + BankAccount bankAccount = new BankAccount("abg@g.com",100, "ABCD1234"); //tests for a valid transfer + BankAccount bankAccount1 = new BankAccount("a@b.com",100, "ABCD1234"); + bankAccount.transfer(bankAccount1,80); + assertEquals(180,bankAccount1.getBalance()); + assertEquals(20, bankAccount.getBalance()); + + BankAccount bankAccount2 = new BankAccount("abg@g.com",100, "ABCD1234"); // tests for a valid withdraw at the boundary of your balance + BankAccount bankAccount3 = new BankAccount("a@b.com",100, "ABCD1234"); + bankAccount2.transfer(bankAccount3,100); + assertEquals(200,bankAccount3.getBalance()); + assertEquals(0, bankAccount2.getBalance()); + + BankAccount bankAccount4 = new BankAccount("abg@g.com",30, "ABCD1234"); //tests for a valid transfer at the boundary of the smallest amount + BankAccount bankAccount5 = new BankAccount("a@b.com",50, "ABCD1234"); + bankAccount4.transfer(bankAccount5,0.01); + assertEquals(29.99,bankAccount4.getBalance()); + assertEquals(50.01, bankAccount5.getBalance()); + + BankAccount bankAccount6 = new BankAccount("abg@g.com",900, "ABCD1234"); //tests for an invalid transfer of 0 + BankAccount bankAccount7 = new BankAccount("a@b.com",1000, "ABCD1234"); + assertThrows(IllegalArgumentException.class, () -> bankAccount6.transfer(bankAccount7,0)); + assertEquals(900,bankAccount6.getBalance()); + assertEquals(1000, bankAccount7.getBalance()); + + BankAccount bankAccount8 = new BankAccount("abg@g.com",700, "ABCD1234"); //tests for an invalid transfer of an amount larger than your balance + BankAccount bankAccount9 = new BankAccount("a@b.com",654.60, "ABCD1234"); + assertThrows(InsufficientFundsException.class, () -> bankAccount8.transfer(bankAccount9,701)); + assertEquals(700,bankAccount8.getBalance()); + assertEquals(654.60, bankAccount9.getBalance()); + + BankAccount bankAccount10 = new BankAccount("abg@g.com",600, "ABCD1234"); //tests for an invalid transfer of a negative number + BankAccount bankAccount11 = new BankAccount("a@b.com",600, "ABCD1234"); + assertThrows(IllegalArgumentException.class, () -> bankAccount10.transfer(bankAccount11,-600)); + assertEquals(600,bankAccount10.getBalance()); + assertEquals(600, bankAccount11.getBalance()); + + BankAccount bankAccount12 = new BankAccount("abg@g.com",600, "ABCD1234"); //tests for an invalid transfer of a negative number and an amount too large + BankAccount bankAccount13 = new BankAccount("a@b.com",600, "ABCD1234"); + assertThrows(IllegalArgumentException.class, () -> bankAccount12.transfer(bankAccount13,-700)); + assertEquals(600,bankAccount12.getBalance()); + assertEquals(600, bankAccount13.getBalance()); + + + + + + + + } + + //need to tests for deposit of 0 + + @Test + void depositTest() throws IllegalArgumentException{ + + BankAccount bankAccount = new BankAccount("abg@g.com",0, "ABCD1234"); //tests for a valid deposit + bankAccount.deposit(100); + assertEquals(100,bankAccount.getBalance()); + + BankAccount bankAccount2 = new BankAccount("abg@g.com",5, "ABCD1234"); //tests for a valid deposit with two decimal places + bankAccount2.deposit(100.50); + assertEquals(105.50,bankAccount2.getBalance()); + + BankAccount bankAccount3 = new BankAccount("abg@g.com",20, "ABCD1234"); //tests for an invalid deposit with more than two decimal places + assertThrows(IllegalArgumentException.class, () -> bankAccount3.deposit(100.5080)); + assertEquals(20,bankAccount3.getBalance()); + + BankAccount bankAccount4 = new BankAccount("abg@g.com",50, "ABCD1234"); //tests for an invalid deposit with a negative number + assertThrows(IllegalArgumentException.class, () -> bankAccount4.deposit(-10000000)); + assertEquals(50,bankAccount4.getBalance()); + + BankAccount bankAccount5 = new BankAccount("abg@g.com",75, "ABCD1234"); //tests for an invalid deposit with a negative number and more than 2 decimal places + assertThrows(IllegalArgumentException.class, () -> bankAccount5.deposit(-10000000.9874)); + assertEquals(75,bankAccount5.getBalance()); + + BankAccount bankAccount6 = new BankAccount("abg@g.com",75, "ABCD1234"); //tests for an invalid deposit of 0. + assertThrows(IllegalArgumentException.class, () -> bankAccount5.deposit(0)); + assertEquals(75,bankAccount5.getBalance()); + + + + + } + + @Test + void isAmountValidTest() throws InsufficientFundsException { + + BankAccount bankAccount = new BankAccount("ak3@g.com",9000, "ABCD1234");//gets a postive number with 2 decimal points. Should pass + bankAccount.withdraw(123.45); + assertEquals(8876.55, bankAccount.getBalance()); + + BankAccount bankAccount3 = new BankAccount("ak@g.com",45, "ABCD1234");//gets a postive number with 1 decimal point. Should pass + bankAccount3.withdraw(20.5); + assertEquals(24.5, bankAccount3.getBalance()); + + BankAccount bankAccount2 = new BankAccount("a@b.com", 200, "ABCD1234"); //checks to make sure a positive number with 3 decimal places is invalid + assertThrows(IllegalArgumentException.class, () -> bankAccount2.withdraw(123.546)); + assertEquals(200,bankAccount2.getBalance()); + + BankAccount bankAccount1 = new BankAccount("a@b.com", 300, "ABCD1234"); //checks to make sure a negative number with 4 decimal places is invalid + assertThrows(IllegalArgumentException.class, () -> bankAccount1.withdraw(25.5566)); + assertEquals(300,bankAccount1.getBalance()); + + } + @Test void getBalanceTest() { - BankAccount bankAccount = new BankAccount("a@b.com", 200); + //correctly gives you the balance when you have a valid email + BankAccount bankAccount = new BankAccount("a@b.com", 200, "ABCD1234"); assertEquals(200, bankAccount.getBalance()); + + //gives you your balance when your balance is 0. Valid email. + BankAccount bankAccount1 = new BankAccount("abc-d@mail.com", 0, "ABCD1234"); + assertEquals(0, bankAccount1.getBalance()); + + //returns your balance when it is negative. Valid email. + BankAccount bankAccount2 = new BankAccount("abc-d@mail.com", -10, "ABCD1234"); + assertEquals(-10, bankAccount2.getBalance()); + + //correctly gives you the balance when you have a valid email + BankAccount bankAccount3 = new BankAccount("a@b.com", 800.67, "ABCD1234"); + assertEquals(800.67, bankAccount3.getBalance()); + + + } @Test - void withdrawTest() { - BankAccount bankAccount = new BankAccount("a@b.com", 200); - bankAccount.withdraw(100); + void withdrawTest() throws InsufficientFundsException { + //tests for a valid withdrawal that will leave you with a half balance. Valid email is provided. + BankAccount bankAccount = new BankAccount("a@b.com", 200, "ABCD1234"); + bankAccount.withdraw(100); assertEquals(100, bankAccount.getBalance()); + + //tests for a valid withdrawal that will leave you with no money left in the account. Valid Email. + BankAccount bankAccount1 = new BankAccount("abc-d@mail.com", 100, "ABCD1234"); + bankAccount.withdraw(100); + assertEquals(0, bankAccount.getBalance()); + + //tests for an invalid withdrawal of $0 that will leave you with the same balance no matter what it is. Valid email. + BankAccount bankAccount2 = new BankAccount("abc-d@mail.com", 100, "ABCD1234"); + assertThrows(IllegalArgumentException.class, ()-> bankAccount2.withdraw(0)); + assertEquals(100,bankAccount2.getBalance()); + + //tests for an invalid withdrawal that is more than your current balance. valid email. + BankAccount bankAccount3 = new BankAccount("abc-d@mail.com", 348.08, "ABCD1234"); + assertThrows(InsufficientFundsException.class, ()-> bankAccount3.withdraw(349)); + assertEquals(348.08,bankAccount3.getBalance()); + + //tests for an invalid withdrawal that is a negative number. Valid email. + BankAccount bankAccount4 = new BankAccount("abc-d@mail.com", 590.02, "ABCD1234"); + assertThrows(IllegalArgumentException.class, ()-> bankAccount4.withdraw(-20)); + assertEquals(590.02,bankAccount4.getBalance()); + + //tests for an invalid withdrawal that is a fraction of a penny. Should return an illegal argument exception + BankAccount bankAccount5 = new BankAccount("abc-d@mail.com", 300.50, "ABCD1234"); + assertThrows(IllegalArgumentException.class, ()-> bankAccount4.withdraw(20.567)); + assertEquals(300.50,bankAccount5.getBalance()); + } @Test void isEmailValidTest(){ - assertTrue(BankAccount.isEmailValid( "a@b.com")); - assertFalse( BankAccount.isEmailValid("")); + + + assertTrue(BankAccount.isEmailValid("a@b.com")); + assertTrue(BankAccount.isEmailValid("abc-d@mail.com")); + assertTrue(BankAccount.isEmailValid("abc.def@mail.com")); + assertTrue(BankAccount.isEmailValid("abc@mail.com")); + assertTrue(BankAccount.isEmailValid("abc_def@mail.com")); + assertTrue(BankAccount.isEmailValid("abc.def@mail.cc")); + assertTrue(BankAccount.isEmailValid("abc.def@mail-archive.com")); + assertTrue(BankAccount.isEmailValid("abc.def@mail.org")); + assertTrue(BankAccount.isEmailValid("abc.def@mail.com")); + assertTrue(BankAccount.isEmailValid("abc.def@mail.archive.com")); + + //tests the equivalence class for no period. + assertFalse(BankAccount.isEmailValid("a@b")); + //tests for no domain after @ sign + assertFalse(BankAccount.isEmailValid("ab.com@j")); + //Tests for an invalid domain because it is too short + assertFalse(BankAccount.isEmailValid("ab@j.c")); + //tests for a domain that is invalid because it is too long. + assertFalse(BankAccount.isEmailValid("ab@domain.c")); + //Tests for a domain that comes before the @ sign and not after + assertFalse(BankAccount.isEmailValid("ab.com@j")); + //Tests for an email without an @ sign + assertFalse(BankAccount.isEmailValid("ab#c#domain.com")); + //Tests for an email that has a special character before the @ sign. + assertFalse(BankAccount.isEmailValid("abc-@mail.com")); + //tests for a an email that has double special characters + assertFalse(BankAccount.isEmailValid("abc..def@mail.com")); + //tests for an email that starts with a special character + assertFalse(BankAccount.isEmailValid(".abc@mail.com")); + //tests for an email that has an invalid character. + assertFalse(BankAccount.isEmailValid("abc$def@mail.com")); + //tests for an email that has invalid domain after the period because it is too short + assertFalse(BankAccount.isEmailValid("abc.def@mail.c")); + //tests for an email that has an invalid character after the @ sign. + assertFalse(BankAccount.isEmailValid("abc.def@mail#archive.com")); + //tests for an email that has no period after the @ sign + assertFalse(BankAccount.isEmailValid("abc.def@mail")); + //tets for an email that has double special characters after the @ sign + assertFalse(BankAccount.isEmailValid("abc.def@mail..com")); + //tests for a special character at the end of the email. + assertFalse(BankAccount.isEmailValid("abc@def.co-")); + + } @Test void constructorTest() { - BankAccount bankAccount = new BankAccount("a@b.com", 200); - + BankAccount bankAccount = new BankAccount("a@b.com", 200, "ABCD1234"); 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("", 100, "ABCD1234")); + + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("", 100.908, "ABCD1234")); //no email inputed and invalid starting balance + + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a@b.com", 10.804, "ABCD1234")); //throws exception for illegal starting balance + } -} \ No newline at end of file +} + diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java new file mode 100644 index 00000000..0f2eff07 --- /dev/null +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -0,0 +1,110 @@ +package edu.ithaca.dragon.bank; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class CentralBankTest { + + + @Test + void checkBalance() { + CentralBank cb = new CentralBank(); + cb.createAccount("a@b.com", 305, "abcd1234"); + //checks the balance of an account in the collection + assertEquals(305, cb.checkBalance("a@b.com")); + //asks for the balance of an account not in the collection + assertThrows(IllegalArgumentException.class, ()-> cb.checkBalance("ab@c.com")); + } + @Test + void depositTest() { + CentralBank cb = new CentralBank(); + cb.createAccount("a@b.com",305,"abcd1234"); + //deposits a valid amount into a valid bank account + cb.deposit("a@b.com",50); + assertEquals(355, cb.checkBalance("a@b.com")); + //attempts to deposit an invalid amount + assertThrows(IllegalArgumentException.class, ()-> cb.deposit("a@b.com",5000.608)); + //attempts to deposit 0 + assertThrows(IllegalArgumentException.class, ()-> cb.deposit("a@b.com",0)); + } + @Test + void confirmCredentialsTest(){ + CentralBank cB = new CentralBank(); + cB.createAccount("ppatel@ithaca.edu",500,"ITH19"); + cB.createAccount("mdad@ithaca.edu",500,"ITH20"); + cB.createAccount("kweal@ithaca.edu",500,"ITH21"); + + assertTrue(cB.confirmCredentials("ppatel@ithaca.edu","ITH19")); + assertTrue(cB.confirmCredentials("mdad@ithaca.edu","ITH20")); + assertTrue(cB.confirmCredentials("kweal@ithaca.edu","ITH21")); + + assertFalse(cB.confirmCredentials("Beefstew@rolling.org","WER1")); + assertFalse(cB.confirmCredentials("Cornbeef@rocks.com","DANCER23")); + assertFalse(cB.confirmCredentials("sloppyJO@beverages.net","HollY23!")); + + + } + @Test + void withdrawTest() throws InsufficientFundsException { + CentralBank cb = new CentralBank(); + + cb.createAccount("a@b.com", 400, "ABCD1234"); + + //withdraws a valid amount with sufficient funds + cb.withdraw("a@b.com",200); + assertEquals(200,cb.checkBalance("a@b.com")); + + //withdraws an invalid amount with sufficient funds + assertThrows(IllegalArgumentException.class, ()-> cb.withdraw("a@b.com",-20.9088)); + + //withdraws a valid amount with insufficient funds + assertThrows(InsufficientFundsException.class, ()-> cb.withdraw("a@b.com",560)); + + } + + @Test + void transferTest() throws InsufficientFundsException { + CentralBank cb = new CentralBank(); + cb.createAccount("ppatel@ithaca.edu", 500, "PurpleNurple"); + cb.createAccount("prav15@cornell.edu",500, "REdBed12"); + + cb.transfer("ppatel@ithaca.edu", "prav15@cornell.edu", 100); + assertEquals(400, cb.checkBalance("ppatel@ithaca.edu")); + assertEquals(600, cb.checkBalance("prav15@cornell.edu")); + + assertThrows(IllegalArgumentException.class, ()-> cb.transfer("ppatel@ithaca.edu", "prav15@cornell.edu", 0)); + + assertThrows(InsufficientFundsException.class, ()-> cb.transfer("prav15@cornell.edu", "ppatel@ithaca.edu",700)); + assertThrows(InsufficientFundsException.class, ()-> cb.transfer("ppatel@ithaca.edu", "prav15@cornell.edu",500)); + } + + @Test + void removeAccountTest(){ + CentralBank cb = new CentralBank(); + cb.createAccount("ppatel@ithaca.edu", 300, "PopTartK1NG"); + cb.createAccount("mark.davis12@federal.gov", 1400, "PopTartsAreAmazing"); + cb.createAccount("lolAntonioBrown@dramaqueen.cc", 1600, "Steelersarebetterwithoutyou"); + cb.createAccount("ABshouldFIGHTLoganPaul@DaZn.com", 500, "ABizweak84"); + cb.createAccount("ABisaWORSETO@nevergonnahappen.net", 30, "81isgreaterthan84"); + + cb.closeAccount("ABshouldFIGHTLoganPaul@DaZn.com"); + assertTrue(cb.checkCustomerCollection("ABshouldFIGHTLoganPaul@DaZn.com")); + } + + @Test + void transactionHistoryTest() throws InsufficientFundsException { + CentralBank cb = new CentralBank(); + cb.createAccount("LOLKrisHumphries@72daymarriage.com", 400, "CantKeepAKardashian"); + cb.createAccount("LilYoungHova@WeRtheWorld.net", 200, "RapisBack2k2k"); + + cb.deposit("LOLKrisHumphries@72daymarriage.com",250); + cb.withdraw("LOLKrisHumphries@72daymarriage.com",300); + cb.transfer("LOLKrisHumphries@72daymarriage.com","LilYoungHova@WeRtheWorld.net",50); + + cb.transactionHistory("LOLKrisHumphries@72daymarriage.com"); + cb.transactionHistory("LilYoungHova@WeRtheWorld.net"); + + + } +}