From 74d49996afd7febcda24c4504ec3f4671b17584f Mon Sep 17 00:00:00 2001 From: thegoat Date: Wed, 22 Jan 2020 14:44:57 -0500 Subject: [PATCH 01/12] first tests --- .idea/misc.xml | 5 ++++- .../edu/ithaca/dragon/bank/BankAccountTest.java | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index d87af9ac..5d282d4d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,9 @@ + + - + \ 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..8aa06ac5 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -25,6 +25,23 @@ void withdrawTest() { void isEmailValidTest(){ assertTrue(BankAccount.isEmailValid( "a@b.com")); assertFalse( BankAccount.isEmailValid("")); + //Email should be true if starts with character or number, includes @ and ends with .'character' + assertTrue(BankAccount.isEmailValid( "a@b.com")); + assertFalse( BankAccount.isEmailValid("")); + assertTrue(BankAccount.isEmailValid("23s@b.com")); + assertTrue(BankAccount.isEmailValid("a1@b.com")); + assertFalse(BankAccount.isEmailValid("@n.com")); + assertFalse(BankAccount.isEmailValid("n.com")); + assertFalse(BankAccount.isEmailValid("n@Gcom")); + assertTrue(BankAccount.isEmailValid("n@gmail.com")); + assertTrue(BankAccount.isEmailValid("n@gyi.org")); + assertFalse(BankAccount.isEmailValid(".n@Gcom")); + assertFalse(BankAccount.isEmailValid(".#Gcom")); + assertFalse(BankAccount.isEmailValid("n$@@G$$com")); + assertTrue(BankAccount.isEmailValid("n122@gyi.g")); + assertTrue(BankAccount.isEmailValid("n122@gyi.g")); + + } @Test From 026bf3063d2169368aed419effa786f07cb8ba48 Mon Sep 17 00:00:00 2001 From: thegoat Date: Thu, 23 Jan 2020 17:17:15 -0500 Subject: [PATCH 02/12] fixed tests --- .gitignore | 1 + src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 3c8b8dd5..946b0332 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ .idea/**/dictionaries .idea/**/shelf +target/ # Generated files .idea/**/contentModel.xml diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 8aa06ac5..c83481d8 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -23,8 +23,6 @@ void withdrawTest() { @Test void isEmailValidTest(){ - assertTrue(BankAccount.isEmailValid( "a@b.com")); - assertFalse( BankAccount.isEmailValid("")); //Email should be true if starts with character or number, includes @ and ends with .'character' assertTrue(BankAccount.isEmailValid( "a@b.com")); assertFalse( BankAccount.isEmailValid("")); @@ -38,8 +36,7 @@ void isEmailValidTest(){ assertFalse(BankAccount.isEmailValid(".n@Gcom")); assertFalse(BankAccount.isEmailValid(".#Gcom")); assertFalse(BankAccount.isEmailValid("n$@@G$$com")); - assertTrue(BankAccount.isEmailValid("n122@gyi.g")); - assertTrue(BankAccount.isEmailValid("n122@gyi.g")); + assertTrue(BankAccount.isEmailValid("n122@gyi.guo")); } From b7b756f24e6814789006fe2a62f2ec034dae4572 Mon Sep 17 00:00:00 2001 From: thegoat Date: Sat, 25 Jan 2020 18:54:29 -0500 Subject: [PATCH 03/12] withdraw tests --- .../edu/ithaca/dragon/bank/BankAccount.java | 92 ++++++++++++++++++- .../ithaca/dragon/bank/BankAccountTest.java | 16 +++- 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index e340e0ea..c4d54bf9 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -1,5 +1,8 @@ package edu.ithaca.dragon.bank; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class BankAccount { private String email; @@ -27,18 +30,101 @@ public String getEmail(){ } /** + * @throws InsufficientFundsException if withdraw amount is greater than balance * @post reduces the balance by amount if amount is non-negative and smaller than balance */ - public void withdraw (double amount) { - balance -= amount; + public void withdraw (double amount) throws InsufficientFundsException { + if (amount <= balance){ + balance -= amount; + } + else { + throw new InsufficientFundsException("Not enough money"); + } + } public static boolean isEmailValid(String email){ - if (email.indexOf('@') == -1){ +// String emailRegex = "^[a-z 0-9]+@[a-z 0-9]+\\.[a-z]{0,9}"; +// Pattern emailPat = Pattern.compile(emailRegex, Pattern.CASE_INSENSITIVE); +// Matcher matcher = emailPat.matcher(email); +// +// return matcher.find(); +// if (email.indexOf('@') == -1 && email.indexOf('.') ==-1 ){ +// return false; +// } +// else { +// return true; +// } + if (email.indexOf('@') == -1 || email.indexOf('@') == 0) { + return false; + } + if (email.indexOf('.') == -1) { + return false; + } + + int count = 0; + + for (int i = email.indexOf('@'); i < email.length(); i++) { + if (email.charAt(i) == ('.')) { + count++; + } + } + if (count != 1) { + return false; + } + + for (int i = 0; i < email.indexOf('@'); i++) { + if (email.charAt(i) == ('.')) { + if (email.charAt(i + 1) == ('.')) { + return false; + } + } + } + + int count3 = 0; + for (int j = email.indexOf('.'); j < email.length(); j++) { + if (email.charAt(j) != (' ')) { + count3++; + } + } + + + if (count3 <= 1) { return false; } + + + int count2=0; + for(int i=0; i bankAccount.withdraw(300)); //check if withdraw amount greater than amount in bank account/ test exception + bankAccount.withdraw(100); + assertEquals(0, bankAccount.getBalance()); //check if amount of total withdraw equal to balance + bankAccount.withdraw(-100); //check if withdraw negative number + //assertEquals(0, bankAccount.getBalance()); //shouldn't equal 100 + + + - assertEquals(100, bankAccount.getBalance()); } @Test @@ -30,13 +38,13 @@ void isEmailValidTest(){ assertTrue(BankAccount.isEmailValid("a1@b.com")); assertFalse(BankAccount.isEmailValid("@n.com")); assertFalse(BankAccount.isEmailValid("n.com")); - assertFalse(BankAccount.isEmailValid("n@Gcom")); + assertFalse(BankAccount.isEmailValid("n!!@Gcom")); assertTrue(BankAccount.isEmailValid("n@gmail.com")); assertTrue(BankAccount.isEmailValid("n@gyi.org")); assertFalse(BankAccount.isEmailValid(".n@Gcom")); assertFalse(BankAccount.isEmailValid(".#Gcom")); assertFalse(BankAccount.isEmailValid("n$@@G$$com")); - assertTrue(BankAccount.isEmailValid("n122@gyi.guo")); + assertTrue(BankAccount.isEmailValid("n122@gyi.g")); } From 47d1f72a89881eb7763b58f9fa5baa73884c3206 Mon Sep 17 00:00:00 2001 From: thegoat Date: Sun, 26 Jan 2020 11:23:19 -0500 Subject: [PATCH 04/12] getbalance & withdraw test with comments --- .../edu/ithaca/dragon/bank/BankAccount.java | 4 +++- .../ithaca/dragon/bank/BankAccountTest.java | 20 +++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index c4d54bf9..7bd4880f 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -34,9 +34,11 @@ public String getEmail(){ * @post reduces the balance by amount if amount is non-negative and smaller than balance */ public void withdraw (double amount) throws InsufficientFundsException { - if (amount <= balance){ + if (amount <= balance && amount >=0){ balance -= amount; } + + else { throw new InsufficientFundsException("Not enough money"); } diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index faf8a23d..64971389 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -7,10 +7,17 @@ class BankAccountTest { @Test - void getBalanceTest() { + void getBalanceTest() throws InsufficientFundsException{ BankAccount bankAccount = new BankAccount("a@b.com", 200); - - assertEquals(200, bankAccount.getBalance()); + assertEquals(200, bankAccount.getBalance()); //check get balance == starting balance + bankAccount.withdraw(50); + assertEquals(150,bankAccount.getBalance()); //check balance after withdraw + assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(200)); //check if throws exception for invalid withfraw + assertEquals(150, bankAccount.getBalance()); //check if balance remains same + assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(-200)); + assertEquals(150, bankAccount.getBalance()); //check for withdraw negative amount + bankAccount.withdraw(0); + assertEquals(150, bankAccount.getBalance()); //balance after withdraw 0 } @Test @@ -21,11 +28,8 @@ void withdrawTest() throws InsufficientFundsException { assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(300)); //check if withdraw amount greater than amount in bank account/ test exception bankAccount.withdraw(100); assertEquals(0, bankAccount.getBalance()); //check if amount of total withdraw equal to balance - bankAccount.withdraw(-100); //check if withdraw negative number - //assertEquals(0, bankAccount.getBalance()); //shouldn't equal 100 - - - + assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(-100)); //check if withdraw negative number + assertEquals(0, bankAccount.getBalance()); //shouldn't equal 100 } From 04fb614c9b33cc8a0446b6c339c87eb899ba7b8a Mon Sep 17 00:00:00 2001 From: thegoat Date: Tue, 28 Jan 2020 11:38:08 -0500 Subject: [PATCH 05/12] made isAmountValid tests (all fail no implementation) --- .../edu/ithaca/dragon/bank/BankAccount.java | 30 ++++++++----------- .../ithaca/dragon/bank/BankAccountTest.java | 29 +++++++++++++++++- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 7bd4880f..7c5e69ea 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -46,19 +46,21 @@ public void withdraw (double amount) throws InsufficientFundsException { } + /** + * @param amount + * @return true if amount is not negative and two decimals or less + */ + + public static boolean isAmountValid(double amount) { + return false; + + + } + + public static boolean isEmailValid(String email){ -// String emailRegex = "^[a-z 0-9]+@[a-z 0-9]+\\.[a-z]{0,9}"; -// Pattern emailPat = Pattern.compile(emailRegex, Pattern.CASE_INSENSITIVE); -// Matcher matcher = emailPat.matcher(email); -// -// return matcher.find(); -// if (email.indexOf('@') == -1 && email.indexOf('.') ==-1 ){ -// return false; -// } -// else { -// return true; -// } + if (email.indexOf('@') == -1 || email.indexOf('@') == 0) { return false; } @@ -121,12 +123,6 @@ public static boolean isEmailValid(String email){ return false; } - - - - - - else { return true; } diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 64971389..92d6af07 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -48,7 +48,34 @@ void isEmailValidTest(){ assertFalse(BankAccount.isEmailValid(".n@Gcom")); assertFalse(BankAccount.isEmailValid(".#Gcom")); assertFalse(BankAccount.isEmailValid("n$@@G$$com")); - assertTrue(BankAccount.isEmailValid("n122@gyi.g")); + assertTrue(BankAccount.isEmailValid("n12.2@gyi.g")); + } + + @Test + void isAmountValidTest(){ + + /*Equivalence tests would : test negative number no decimals, test positive number no decimals, test negative float, test positive float + test float with more than two decimals positive and negative, test float with one decimal positive and negative, test 0 */ + + assertTrue(BankAccount.isAmountValid(10)); //positive int + assertTrue(BankAccount.isAmountValid(0.01)); //positive float + assertTrue(BankAccount.isAmountValid(.01)); //positive float without leading number + assertTrue(BankAccount.isAmountValid(1.1)); //positive float with one decimal + assertTrue(BankAccount.isAmountValid(0)); //testing 0 + + + + assertFalse(BankAccount.isAmountValid(-10)); //negative int + assertFalse(BankAccount.isAmountValid(-0.01)); //negative float + assertFalse(BankAccount.isAmountValid(-.01)); //negative float without leading number + assertFalse(BankAccount.isAmountValid(0.009)); //positive float with more than 2 decimals + assertFalse(BankAccount.isAmountValid(-0.00242423)); //negative float with more than 2 decimals + + + + + + } From c5846c0bae6b318f847dcfd9ec564692a63c8efd Mon Sep 17 00:00:00 2001 From: thegoat Date: Tue, 28 Jan 2020 11:39:35 -0500 Subject: [PATCH 06/12] passing isAmountValid Tests --- .../java/edu/ithaca/dragon/bank/BankAccount.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 7c5e69ea..d60d6ff2 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -52,13 +52,27 @@ public void withdraw (double amount) throws InsufficientFundsException { */ public static boolean isAmountValid(double amount) { - return false; + if(amount < 0) { + return false; + } + + String s = ""+amount; + + int i = s.indexOf('.'); + + if(s.length()-i-1 > 2){ + return false; + } + return true; } + + + public static boolean isEmailValid(String email){ if (email.indexOf('@') == -1 || email.indexOf('@') == 0) { From 16386068e7f0565be8d243505ca173dc8ef30d3e Mon Sep 17 00:00:00 2001 From: thegoat Date: Tue, 28 Jan 2020 12:59:05 -0500 Subject: [PATCH 07/12] wrote withdraw and constructor tests (fail, check for illegal arg exception) --- .../edu/ithaca/dragon/bank/BankAccount.java | 11 ++++--- .../ithaca/dragon/bank/BankAccountTest.java | 29 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index d60d6ff2..cbffb9ab 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -11,13 +11,14 @@ public class BankAccount { /** * @throws IllegalArgumentException if email is invalid */ - public BankAccount(String email, double startingBalance){ + 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"); + throw new IllegalArgumentException("Email address or starting balance is invalid"); } } @@ -33,12 +34,14 @@ public String getEmail(){ * @throws InsufficientFundsException if withdraw amount is greater than balance * @post reduces the balance by amount if amount is non-negative and smaller than balance */ - public void withdraw (double amount) throws InsufficientFundsException { - if (amount <= balance && amount >=0){ + public void withdraw (double amount) throws InsufficientFundsException, IllegalArgumentException { + if (amount <= balance && isAmountValid(amount)){ + balance -= amount; } + else { throw new InsufficientFundsException("Not enough money"); } diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 92d6af07..776c0e22 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -21,16 +21,19 @@ void getBalanceTest() throws InsufficientFundsException{ } @Test - void withdrawTest() throws InsufficientFundsException { + void withdrawTest() throws InsufficientFundsException, IllegalArgumentException { BankAccount bankAccount = new BankAccount("a@b.com", 200); bankAccount.withdraw(100); assertEquals(100, bankAccount.getBalance()); // check if withdraw decreased in account assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(300)); //check if withdraw amount greater than amount in bank account/ test exception bankAccount.withdraw(100); assertEquals(0, bankAccount.getBalance()); //check if amount of total withdraw equal to balance - assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(-100)); //check if withdraw negative number - assertEquals(0, bankAccount.getBalance()); //shouldn't equal 100 - + bankAccount.withdraw(0); + assertEquals(0, bankAccount.getBalance()); + assertThrows(IllegalArgumentException.class, () -> bankAccount.withdraw(-100)); //check if withdraw negative number + assertEquals(0, bankAccount.getBalance()); //shouldn't equal 100 and remain at balance it was before + assertThrows(IllegalArgumentException.class, () ->bankAccount.withdraw(10.103234)); + assertEquals(0, bankAccount.getBalance()); //should still remain 0 } @Test @@ -64,20 +67,12 @@ void isAmountValidTest(){ assertTrue(BankAccount.isAmountValid(0)); //testing 0 - assertFalse(BankAccount.isAmountValid(-10)); //negative int assertFalse(BankAccount.isAmountValid(-0.01)); //negative float assertFalse(BankAccount.isAmountValid(-.01)); //negative float without leading number assertFalse(BankAccount.isAmountValid(0.009)); //positive float with more than 2 decimals assertFalse(BankAccount.isAmountValid(-0.00242423)); //negative float with more than 2 decimals - - - - - - - } @Test @@ -88,6 +83,16 @@ void constructorTest() { assertEquals(200, bankAccount.getBalance()); //check for exception thrown correctly assertThrows(IllegalArgumentException.class, ()-> new BankAccount("", 100)); + assertThrows(IllegalArgumentException.class, () -> bankAccount.withdraw(-100)); //illegal arguement negative withdraw + + BankAccount bankAccount2 = new BankAccount("a@be.com", 200); + assertThrows(IllegalArgumentException.class, ()-> bankAccount2.withdraw(.00012)); + assertThrows(InsufficientFundsException.class, () ->bankAccount2.withdraw(200.01)); //throw insufficient funds exception + assertThrows(IllegalArgumentException.class, () ->bankAccount2.withdraw(200.0001)); + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a32@gt.com", -100)); //invalid starting negative throw illegal argument + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a32@gt.com", 100.5345)); //invalid starting float throw illegal argument + + } } \ No newline at end of file From e5fc0dc4dc92a2236f5217e28b29bcea3543fc12 Mon Sep 17 00:00:00 2001 From: thegoat Date: Tue, 28 Jan 2020 13:00:04 -0500 Subject: [PATCH 08/12] pass withdraw and constructor tests checks for both exceptions --- src/main/java/edu/ithaca/dragon/bank/BankAccount.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index cbffb9ab..ad8bf7b4 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -12,7 +12,7 @@ public class BankAccount { * @throws IllegalArgumentException if email is invalid */ public BankAccount(String email, double startingBalance) { - if (isEmailValid(email)){ + if (isEmailValid(email) && isAmountValid(startingBalance)){ this.email = email; this.balance = startingBalance; } @@ -40,7 +40,9 @@ public void withdraw (double amount) throws InsufficientFundsException, IllegalA balance -= amount; } - + else if(!isAmountValid(amount)){ + throw new IllegalArgumentException("invalid amount to withdraw"); + } else { throw new InsufficientFundsException("Not enough money"); From 76a9ef3ddfd1b489528b63cb8f28352c12f940dd Mon Sep 17 00:00:00 2001 From: thegoat Date: Tue, 28 Jan 2020 13:20:42 -0500 Subject: [PATCH 09/12] pass withdraw and constructor tests checks for both exceptions --- .../edu/ithaca/dragon/bank/BankAccount.java | 24 ++++++++++++++++--- .../ithaca/dragon/bank/BankAccountTest.java | 23 ++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index ad8bf7b4..3e6efaab 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -49,6 +49,22 @@ else if(!isAmountValid(amount)){ } + } + + /** + * @throws IllegalArgumentException if amount is negative or contains more than two decimals + * @param amount + * @post adds the amount to the balance of the bankAccount, should increase + */ + + public void deposit(double amount){ +// if(isAmountValid(amount)){ +// balance+=amount; +// } + + + + } /** @@ -79,16 +95,13 @@ public static boolean isAmountValid(double amount) { public static boolean isEmailValid(String email){ - if (email.indexOf('@') == -1 || email.indexOf('@') == 0) { return false; } if (email.indexOf('.') == -1) { return false; } - int count = 0; - for (int i = email.indexOf('@'); i < email.length(); i++) { if (email.charAt(i) == ('.')) { count++; @@ -146,4 +159,9 @@ public static boolean isEmailValid(String email){ return true; } } + + + + + } diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 776c0e22..697a472f 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -93,6 +93,29 @@ void constructorTest() { assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a32@gt.com", 100.5345)); //invalid starting float throw illegal argument + } + + @Test + void depositTest(){ + BankAccount bankAccount = new BankAccount("a@bana.com", 100); + + //Test valid amount to deposit (throws exceptions), test valid amounts deposit to increase account amount + + + bankAccount.deposit(.01); + assertEquals(100.01, bankAccount.getBalance()); + bankAccount.deposit(1.01); + assertEquals(101.02, bankAccount.getBalance()); + assertThrows(IllegalArgumentException.class, () ->bankAccount.deposit(0.034234)); + assertThrows(IllegalArgumentException.class, () ->bankAccount.deposit(-130)); + bankAccount.deposit(1000); + assertEquals(1101.02, bankAccount.getBalance()); + + + + + + } } \ No newline at end of file From f6d783bc46f0d73df4f8aff41f912d3ab464f560 Mon Sep 17 00:00:00 2001 From: thegoat Date: Tue, 28 Jan 2020 13:21:34 -0500 Subject: [PATCH 10/12] wrote out deposit tests and comments and method signature --- src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 697a472f..d27a1054 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -99,7 +99,7 @@ void constructorTest() { void depositTest(){ BankAccount bankAccount = new BankAccount("a@bana.com", 100); - //Test valid amount to deposit (throws exceptions), test valid amounts deposit to increase account amount + //Test valid amount to deposit (throws exceptions), test valid amounts deposit to increase the account amount bankAccount.deposit(.01); From 20cfe741eba70b0fb2f2b9c1734990acd67a84a8 Mon Sep 17 00:00:00 2001 From: thegoat Date: Tue, 28 Jan 2020 23:16:10 -0500 Subject: [PATCH 11/12] wrote and passed deposit tests and transfer tests with implementations --- .../edu/ithaca/dragon/bank/BankAccount.java | 72 ++++++++++++++----- .../ithaca/dragon/bank/BankAccountTest.java | 42 +++++++++-- 2 files changed, 92 insertions(+), 22 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 3e6efaab..e59fb4bc 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -1,5 +1,8 @@ package edu.ithaca.dragon.bank; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.NumberFormat; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -22,9 +25,7 @@ public BankAccount(String email, double startingBalance) { } } - public double getBalance(){ - return balance; - } + public double getBalance(){ return balance; } public String getEmail(){ return email; @@ -32,6 +33,7 @@ public String getEmail(){ /** * @throws InsufficientFundsException if withdraw amount is greater than balance + * @throws IllegalArgumentException if withdraw amount is negative or has more than 2 decimals * @post reduces the balance by amount if amount is non-negative and smaller than balance */ public void withdraw (double amount) throws InsufficientFundsException, IllegalArgumentException { @@ -51,6 +53,43 @@ else if(!isAmountValid(amount)){ } + + /** + * @throws InsufficientFundsException if amount is more than is available + * @param amount + * @post subtracts from account1 to account2 + */ + + public void transfer(double amount, BankAccount fromAccount, BankAccount toAccount) throws IllegalArgumentException, InsufficientFundsException { + if(isAmountValid(amount)){ + + fromAccount.withdraw(amount); + + + toAccount.deposit(amount); + + DecimalFormat newFormat = new DecimalFormat("#. ##"); + + + fromAccount.balance = Double.valueOf(newFormat.format(fromAccount.balance)); + + toAccount.balance = Double.valueOf(newFormat.format(toAccount.balance)); + + } + + else if(!isAmountValid(amount)){ + throw new IllegalArgumentException("Not a valid amount to transfer"); + } + + else { + throw new InsufficientFundsException("Not enough money to transfer"); + } + } + + + + + /** * @throws IllegalArgumentException if amount is negative or contains more than two decimals * @param amount @@ -58,13 +97,19 @@ else if(!isAmountValid(amount)){ */ public void deposit(double amount){ -// if(isAmountValid(amount)){ -// balance+=amount; -// } + if(isAmountValid(amount)){ + balance+=amount; + DecimalFormat newFormat = new DecimalFormat("#. ##"); + balance = Double.valueOf(newFormat.format(balance)); + + } + else{ + throw new IllegalArgumentException("Invalid deposit amount"); + } } /** @@ -79,7 +124,7 @@ public static boolean isAmountValid(double amount) { String s = ""+amount; - int i = s.indexOf('.'); + int i = s.lastIndexOf('.'); if(s.length()-i-1 > 2){ return false; @@ -144,20 +189,11 @@ public static boolean isEmailValid(String email){ } - int count4 =0; int indx = email.lastIndexOf('.'); - while(indx < email.length()) { - count4++; - indx++; - } - if(count4 <=1){ - return false; - } + int length = email.substring(indx + 1).length(); + return length >= 2; - else { - return true; - } } diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index d27a1054..16026e73 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -7,14 +7,14 @@ class BankAccountTest { @Test - void getBalanceTest() throws InsufficientFundsException{ + void getBalanceTest() throws InsufficientFundsException, IllegalArgumentException{ BankAccount bankAccount = new BankAccount("a@b.com", 200); assertEquals(200, bankAccount.getBalance()); //check get balance == starting balance bankAccount.withdraw(50); assertEquals(150,bankAccount.getBalance()); //check balance after withdraw assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(200)); //check if throws exception for invalid withfraw assertEquals(150, bankAccount.getBalance()); //check if balance remains same - assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(-200)); + assertThrows(IllegalArgumentException.class, () -> bankAccount.withdraw(-200)); assertEquals(150, bankAccount.getBalance()); //check for withdraw negative amount bankAccount.withdraw(0); assertEquals(150, bankAccount.getBalance()); //balance after withdraw 0 @@ -26,7 +26,8 @@ void withdrawTest() throws InsufficientFundsException, IllegalArgumentException bankAccount.withdraw(100); assertEquals(100, bankAccount.getBalance()); // check if withdraw decreased in account assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(300)); //check if withdraw amount greater than amount in bank account/ test exception - bankAccount.withdraw(100); + assertThrows(IllegalArgumentException.class, () -> bankAccount.withdraw(100.0001)); + bankAccount.withdraw(100.0000); assertEquals(0, bankAccount.getBalance()); //check if amount of total withdraw equal to balance bankAccount.withdraw(0); assertEquals(0, bankAccount.getBalance()); @@ -39,6 +40,7 @@ void withdrawTest() throws InsufficientFundsException, IllegalArgumentException @Test void isEmailValidTest(){ //Email should be true if starts with character or number, includes @ and ends with .'character' + assertTrue(BankAccount.isEmailValid( "a@b.com")); assertFalse( BankAccount.isEmailValid("")); assertTrue(BankAccount.isEmailValid("23s@b.com")); @@ -51,7 +53,7 @@ void isEmailValidTest(){ assertFalse(BankAccount.isEmailValid(".n@Gcom")); assertFalse(BankAccount.isEmailValid(".#Gcom")); assertFalse(BankAccount.isEmailValid("n$@@G$$com")); - assertTrue(BankAccount.isEmailValid("n12.2@gyi.g")); + assertFalse(BankAccount.isEmailValid("n12.2@gyi.g")); } @Test @@ -110,11 +112,43 @@ void depositTest(){ assertThrows(IllegalArgumentException.class, () ->bankAccount.deposit(-130)); bankAccount.deposit(1000); assertEquals(1101.02, bankAccount.getBalance()); + } + + @Test + void transferTest() throws InsufficientFundsException{ + + + BankAccount bankAccount1 = new BankAccount("bob@12.com", 500); + BankAccount bankAccount2 = new BankAccount("ang@2.com", 600); + + + bankAccount1.transfer(100, bankAccount1 , bankAccount2); //check if normal transfer works (positive number) + + assertEquals(400, bankAccount1.getBalance()); + + assertEquals(700, bankAccount2.getBalance()); + + assertThrows(InsufficientFundsException.class, () ->bankAccount1.transfer(1000, bankAccount2, bankAccount1)); //throw insufficient funds + + assertThrows(IllegalArgumentException.class, () ->bankAccount1.transfer(-120, bankAccount2, bankAccount1)); //throw illegal argument negative + + assertThrows(IllegalArgumentException.class, () ->bankAccount1.transfer(.43234, bankAccount2, bankAccount1)); //throw illegal argument more than 2 decimals + + assertEquals(400, bankAccount1.getBalance()); //make sure amount didn't change + + assertEquals(700, bankAccount2.getBalance()); //make sure amount didn't change + + bankAccount1.transfer(1.04, bankAccount2 , bankAccount1); //check transfer from reverse order with decimal number + assertEquals(401.04, bankAccount1.getBalance()); + assertEquals(698.96, bankAccount2.getBalance()); + bankAccount1.transfer(.04, bankAccount2 , bankAccount1); //check transfer amont of just cents + assertEquals(401.08, bankAccount1.getBalance()); + assertEquals(698.92, bankAccount2.getBalance()); } From 34c14d6bed837d5e95d7b9d23482a7851ff98540 Mon Sep 17 00:00:00 2001 From: thegoat Date: Wed, 29 Jan 2020 15:58:00 -0500 Subject: [PATCH 12/12] modified transfer function, pass all tests --- .../edu/ithaca/dragon/bank/BankAccount.java | 16 ++++++++------- .../ithaca/dragon/bank/BankAccountTest.java | 20 +++++++++---------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index e59fb4bc..57ad7a6e 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -60,10 +60,13 @@ else if(!isAmountValid(amount)){ * @post subtracts from account1 to account2 */ - public void transfer(double amount, BankAccount fromAccount, BankAccount toAccount) throws IllegalArgumentException, InsufficientFundsException { + public void transfer(double amount, BankAccount toAccount) throws IllegalArgumentException, InsufficientFundsException { + if(amount > balance){ + throw new InsufficientFundsException("Not enough money to transfer"); + } if(isAmountValid(amount)){ - fromAccount.withdraw(amount); + balance = balance - amount; toAccount.deposit(amount); @@ -71,19 +74,18 @@ public void transfer(double amount, BankAccount fromAccount, BankAccount toAccou DecimalFormat newFormat = new DecimalFormat("#. ##"); - fromAccount.balance = Double.valueOf(newFormat.format(fromAccount.balance)); + balance = Double.valueOf(newFormat.format(balance)); toAccount.balance = Double.valueOf(newFormat.format(toAccount.balance)); - } + else if(!isAmountValid(amount)){ throw new IllegalArgumentException("Not a valid amount to transfer"); } - else { - throw new InsufficientFundsException("Not enough money to transfer"); - } + + } diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 16026e73..a6c314e0 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -122,33 +122,33 @@ void transferTest() throws InsufficientFundsException{ BankAccount bankAccount2 = new BankAccount("ang@2.com", 600); - bankAccount1.transfer(100, bankAccount1 , bankAccount2); //check if normal transfer works (positive number) + bankAccount1.transfer(100, bankAccount2); //check if normal transfer works (positive number) assertEquals(400, bankAccount1.getBalance()); assertEquals(700, bankAccount2.getBalance()); - assertThrows(InsufficientFundsException.class, () ->bankAccount1.transfer(1000, bankAccount2, bankAccount1)); //throw insufficient funds + assertThrows(InsufficientFundsException.class, () ->bankAccount1.transfer(10000, bankAccount2)); //throw insufficient funds - assertThrows(IllegalArgumentException.class, () ->bankAccount1.transfer(-120, bankAccount2, bankAccount1)); //throw illegal argument negative + assertThrows(IllegalArgumentException.class, () ->bankAccount1.transfer(-120, bankAccount2)); //throw illegal argument negative - assertThrows(IllegalArgumentException.class, () ->bankAccount1.transfer(.43234, bankAccount2, bankAccount1)); //throw illegal argument more than 2 decimals + assertThrows(IllegalArgumentException.class, () ->bankAccount1.transfer(.43234, bankAccount2)); //throw illegal argument more than 2 decimals assertEquals(400, bankAccount1.getBalance()); //make sure amount didn't change assertEquals(700, bankAccount2.getBalance()); //make sure amount didn't change - bankAccount1.transfer(1.04, bankAccount2 , bankAccount1); //check transfer from reverse order with decimal number + bankAccount1.transfer(1.04, bankAccount2); //check transfer with decimal number - assertEquals(401.04, bankAccount1.getBalance()); + assertEquals(398.96, bankAccount1.getBalance()); - assertEquals(698.96, bankAccount2.getBalance()); + assertEquals(701.04, bankAccount2.getBalance()); - bankAccount1.transfer(.04, bankAccount2 , bankAccount1); //check transfer amont of just cents + bankAccount2.transfer(.04, bankAccount1); //check transfer amount of just cents in reverse order - assertEquals(401.08, bankAccount1.getBalance()); + assertEquals(399, bankAccount1.getBalance()); - assertEquals(698.92, bankAccount2.getBalance()); + assertEquals(701.00, bankAccount2.getBalance()); }