From 92f0b1233fb6160fc34b9e54de9e30f695532b52 Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Wed, 22 Jan 2020 14:45:07 -0500 Subject: [PATCH 01/31] Tests for email validation. --- .gitignore | 1 + .idea/SoftwareEngineeringPractice.iml | 2 ++ .idea/encodings.xml | 1 + .idea/misc.xml | 5 +++- .idea/vcs.xml | 6 +++++ .../ithaca/dragon/bank/BankAccountTest.java | 25 +++++++++++++++++++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 .idea/SoftwareEngineeringPractice.iml create mode 100644 .idea/vcs.xml 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/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/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..5d282d4d 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..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ 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..13f31db4 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -24,7 +24,32 @@ void withdrawTest() { @Test void isEmailValidTest(){ assertTrue(BankAccount.isEmailValid( "a@b.com")); + assertTrue(BankAccount.isEmailValid( "a-c@b.com")); + assertTrue(BankAccount.isEmailValid( "a.c@b.com")); + assertTrue(BankAccount.isEmailValid( "a_c@b.com")); + assertTrue(BankAccount.isEmailValid( "a.c@b.cc")); + assertTrue(BankAccount.isEmailValid( "a.c@b-archive.com")); + assertTrue(BankAccount.isEmailValid( "a.c@b.org")); + assertTrue(BankAccount.isEmailValid( "a.c@mail.com")); + + assertFalse( BankAccount.isEmailValid("")); + assertFalse( BankAccount.isEmailValid("a-@b.com")); + assertFalse( BankAccount.isEmailValid("a..c@b.com")); + assertFalse( BankAccount.isEmailValid(".a@b.com")); + assertFalse( BankAccount.isEmailValid("a#c@b.com")); + assertFalse( BankAccount.isEmailValid("a.c@b.c")); + assertFalse( BankAccount.isEmailValid("a.c@b#archive.com")); + assertFalse( BankAccount.isEmailValid("a.c@b")); + assertFalse( BankAccount.isEmailValid("a.c@b..com")); + + + + + + + + } @Test From 443a1738fc234fb633c6116e8c322c27bc740f3b Mon Sep 17 00:00:00 2001 From: MarathonTT Date: Wed, 22 Jan 2020 19:36:31 -0500 Subject: [PATCH 02/31] implemented isEmailValid, passing all tests --- .../edu/ithaca/dragon/bank/BankAccount.java | 126 +++++++++++++++++- 1 file changed, 120 insertions(+), 6 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..a41cba67 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -1,5 +1,7 @@ package edu.ithaca.dragon.bank; +import java.util.ArrayList; + public class BankAccount { private String email; @@ -34,13 +36,125 @@ public void withdraw (double amount) { } + /** + * returns true if email follows correct conventions. + */ - public static boolean isEmailValid(String email){ - if (email.indexOf('@') == -1){ - return false; - } - else { - return true; + public static boolean isEmailValid(String email) { + int atIdx = email.indexOf('@'); + int lastAtIdx = email.indexOf('@'); + + //Checks that there is no more than 1 @. + if (atIdx != lastAtIdx) { return false; } + + //Checks that there is an @. + if (atIdx == -1) { return false; } + + //Checks that there is a period in the only two spots it can be in the domain. + //Note that there can only be 2 or 3 characters after the period in the domain. + int len = email.length(); + int lastPeriodIdx = email.lastIndexOf('.'); + + if (lastPeriodIdx == -1){ return false; } + if (lastPeriodIdx != len - 3 && lastPeriodIdx != len - 4) { return false; } + + //Checks that the @ is before the last period. + if (atIdx>lastPeriodIdx) { return false; } + + //Checks that all characters are valid and that the email obeys the letter after special rule. + int i = len - 1; + ArrayList validCharArr = getValidCharArr(); + + //going through each character in the email + while (i > -1) { + + //if invalid character return false + if (!(validCharArr.contains(email.charAt(i)))) { return false; } + + //if special character, make sure following character is not special + if (isSpecialChar(email.charAt(i))){ + //if leading character is special, return false + if (i == 0){ return false; } + //if the next character is also special return false + if (isSpecialChar(email.charAt(i + 1))){ return false; } + } + i--; } + //if no invalid characters found or special rules broken, return true + return true; + } + + private static boolean isSpecialChar(char c) { return c == '.' || c == '-' || c == '_' || c == '@'; } + + private static ArrayList getValidCharArr(){ + ArrayList validCharArr = new ArrayList<>(); + validCharArr.add('@'); + validCharArr.add('.'); + validCharArr.add('_'); + validCharArr.add('-'); + validCharArr.add('q'); + validCharArr.add('w'); + validCharArr.add('e'); + validCharArr.add('r'); + validCharArr.add('t'); + validCharArr.add('y'); + validCharArr.add('u'); + validCharArr.add('i'); + validCharArr.add('o'); + validCharArr.add('p'); + validCharArr.add('a'); + validCharArr.add('s'); + validCharArr.add('d'); + validCharArr.add('f'); + validCharArr.add('g'); + validCharArr.add('h'); + validCharArr.add('j'); + validCharArr.add('k'); + validCharArr.add('l'); + validCharArr.add('z'); + validCharArr.add('x'); + validCharArr.add('c'); + validCharArr.add('v'); + validCharArr.add('b'); + validCharArr.add('n'); + validCharArr.add('m'); + validCharArr.add('Q'); + validCharArr.add('W'); + validCharArr.add('E'); + validCharArr.add('R'); + validCharArr.add('T'); + validCharArr.add('Y'); + validCharArr.add('U'); + validCharArr.add('I'); + validCharArr.add('O'); + validCharArr.add('P'); + validCharArr.add('A'); + validCharArr.add('S'); + validCharArr.add('D'); + validCharArr.add('F'); + validCharArr.add('G'); + validCharArr.add('H'); + validCharArr.add('J'); + validCharArr.add('K'); + validCharArr.add('L'); + validCharArr.add('Z'); + validCharArr.add('X'); + validCharArr.add('C'); + validCharArr.add('V'); + validCharArr.add('B'); + validCharArr.add('N'); + validCharArr.add('M'); + validCharArr.add('1'); + validCharArr.add('2'); + validCharArr.add('3'); + validCharArr.add('4'); + validCharArr.add('5'); + validCharArr.add('6'); + validCharArr.add('7'); + validCharArr.add('8'); + validCharArr.add('9'); + validCharArr.add('0'); + + return validCharArr; } } From 377a67ad48664b4a8f0ef531ea6346cb819d96ea Mon Sep 17 00:00:00 2001 From: MarathonTT Date: Thu, 23 Jan 2020 13:19:05 -0500 Subject: [PATCH 03/31] added one more test case that previous isEmailValid failed, fixed bug, proposing as final version --- .idea/TobySoftwareEngineeringPractice.iml | 2 ++ .idea/compiler.xml | 1 + .../edu/ithaca/dragon/bank/BankAccount.java | 19 +++++++++--------- .../ithaca/dragon/bank/BankAccountTest.java | 20 ++++++++++--------- 4 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 .idea/TobySoftwareEngineeringPractice.iml 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..4e2bf249 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,6 +7,7 @@ + diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index a41cba67..ce84f3c9 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -45,21 +45,21 @@ public static boolean isEmailValid(String email) { int lastAtIdx = email.indexOf('@'); //Checks that there is no more than 1 @. - if (atIdx != lastAtIdx) { return false; } + if (atIdx != lastAtIdx) return false; //Checks that there is an @. - if (atIdx == -1) { return false; } + if (atIdx == -1) return false; //Checks that there is a period in the only two spots it can be in the domain. //Note that there can only be 2 or 3 characters after the period in the domain. int len = email.length(); int lastPeriodIdx = email.lastIndexOf('.'); - if (lastPeriodIdx == -1){ return false; } - if (lastPeriodIdx != len - 3 && lastPeriodIdx != len - 4) { return false; } + if (lastPeriodIdx == -1) return false; + if (lastPeriodIdx != len - 3 && lastPeriodIdx != len - 4) return false; //Checks that the @ is before the last period. - if (atIdx>lastPeriodIdx) { return false; } + if (atIdx>lastPeriodIdx) return false; //Checks that all characters are valid and that the email obeys the letter after special rule. int i = len - 1; @@ -69,15 +69,16 @@ public static boolean isEmailValid(String email) { while (i > -1) { //if invalid character return false - if (!(validCharArr.contains(email.charAt(i)))) { return false; } + if (!(validCharArr.contains(email.charAt(i)))) return false; //if special character, make sure following character is not special if (isSpecialChar(email.charAt(i))){ - //if leading character is special, return false - if (i == 0){ return false; } + //if leading or last character is special, return false + if (i == 0 || i == len - 1) return false; //if the next character is also special return false - if (isSpecialChar(email.charAt(i + 1))){ return false; } + if (isSpecialChar(email.charAt(i + 1))) return false; } + //go to the letter one previous i--; } //if no invalid characters found or special rules broken, 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 13f31db4..ddbe5ea5 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; @@ -33,15 +34,16 @@ void isEmailValidTest(){ assertTrue(BankAccount.isEmailValid( "a.c@mail.com")); - assertFalse( BankAccount.isEmailValid("")); - assertFalse( BankAccount.isEmailValid("a-@b.com")); - assertFalse( BankAccount.isEmailValid("a..c@b.com")); - assertFalse( BankAccount.isEmailValid(".a@b.com")); - assertFalse( BankAccount.isEmailValid("a#c@b.com")); - assertFalse( BankAccount.isEmailValid("a.c@b.c")); - assertFalse( BankAccount.isEmailValid("a.c@b#archive.com")); - assertFalse( BankAccount.isEmailValid("a.c@b")); - assertFalse( BankAccount.isEmailValid("a.c@b..com")); + assertFalse(BankAccount.isEmailValid("")); + assertFalse(BankAccount.isEmailValid("a-@b.com")); + assertFalse(BankAccount.isEmailValid("a..c@b.com")); + assertFalse(BankAccount.isEmailValid(".a@b.com")); + assertFalse(BankAccount.isEmailValid("a#c@b.com")); + assertFalse(BankAccount.isEmailValid("a.c@b.c")); + assertFalse(BankAccount.isEmailValid("a.c@b#archive.com")); + assertFalse(BankAccount.isEmailValid("a.c@b")); + assertFalse(BankAccount.isEmailValid("a.c@b..com")); + assertFalse(BankAccount.isEmailValid("abc@def.co-")); From 4062f1ec1471cd7df1b51c335ede543a44a9a4f1 Mon Sep 17 00:00:00 2001 From: MarathonTT Date: Fri, 24 Jan 2020 10:38:07 -0500 Subject: [PATCH 04/31] corrected wrong call of indexOf to lastIndexOf, added withdraw function --- .../edu/ithaca/dragon/bank/BankAccount.java | 173 +++++++++--------- 1 file changed, 84 insertions(+), 89 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index ce84f3c9..e978fa95 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -1,6 +1,6 @@ package edu.ithaca.dragon.bank; -import java.util.ArrayList; +import java.util.HashSet; public class BankAccount { @@ -32,8 +32,9 @@ public String getEmail(){ * @post reduces the balance by amount if amount is non-negative and smaller than balance */ public void withdraw (double amount) { - balance -= amount; - + if (amount <= 0) throw new IllegalArgumentException("Cannot withdraw 0 or less."); + if (balance >= amount) balance -= amount; + else throw new IllegalArgumentException("Cannot draw more than account balance."); } /** @@ -41,20 +42,16 @@ public void withdraw (double amount) { */ public static boolean isEmailValid(String email) { - int atIdx = email.indexOf('@'); - int lastAtIdx = email.indexOf('@'); - - //Checks that there is no more than 1 @. - if (atIdx != lastAtIdx) return false; - //Checks that there is an @. - if (atIdx == -1) return false; + //Checks that there is only 1 @. + int atIdx = email.indexOf('@'); + int lastAtIdx = email.lastIndexOf('@'); + if (atIdx != lastAtIdx || atIdx == -1) return false; - //Checks that there is a period in the only two spots it can be in the domain. - //Note that there can only be 2 or 3 characters after the period in the domain. + //Checks that there is a period in the one of two spots it must be in the domain. + //Note that there can only be 2 or 3 characters after the last period in the domain. int len = email.length(); int lastPeriodIdx = email.lastIndexOf('.'); - if (lastPeriodIdx == -1) return false; if (lastPeriodIdx != len - 3 && lastPeriodIdx != len - 4) return false; @@ -63,13 +60,11 @@ public static boolean isEmailValid(String email) { //Checks that all characters are valid and that the email obeys the letter after special rule. int i = len - 1; - ArrayList validCharArr = getValidCharArr(); - - //going through each character in the email - while (i > -1) { + HashSet validCharSet = getValidCharSet(); + while (i > -1) { //going through each character in the email //if invalid character return false - if (!(validCharArr.contains(email.charAt(i)))) return false; + if (!validCharSet.contains(email.charAt(i))) return false; //if special character, make sure following character is not special if (isSpecialChar(email.charAt(i))){ @@ -78,7 +73,7 @@ public static boolean isEmailValid(String email) { //if the next character is also special return false if (isSpecialChar(email.charAt(i + 1))) return false; } - //go to the letter one previous + //go back one letter i--; } //if no invalid characters found or special rules broken, return true @@ -87,75 +82,75 @@ public static boolean isEmailValid(String email) { private static boolean isSpecialChar(char c) { return c == '.' || c == '-' || c == '_' || c == '@'; } - private static ArrayList getValidCharArr(){ - ArrayList validCharArr = new ArrayList<>(); - validCharArr.add('@'); - validCharArr.add('.'); - validCharArr.add('_'); - validCharArr.add('-'); - validCharArr.add('q'); - validCharArr.add('w'); - validCharArr.add('e'); - validCharArr.add('r'); - validCharArr.add('t'); - validCharArr.add('y'); - validCharArr.add('u'); - validCharArr.add('i'); - validCharArr.add('o'); - validCharArr.add('p'); - validCharArr.add('a'); - validCharArr.add('s'); - validCharArr.add('d'); - validCharArr.add('f'); - validCharArr.add('g'); - validCharArr.add('h'); - validCharArr.add('j'); - validCharArr.add('k'); - validCharArr.add('l'); - validCharArr.add('z'); - validCharArr.add('x'); - validCharArr.add('c'); - validCharArr.add('v'); - validCharArr.add('b'); - validCharArr.add('n'); - validCharArr.add('m'); - validCharArr.add('Q'); - validCharArr.add('W'); - validCharArr.add('E'); - validCharArr.add('R'); - validCharArr.add('T'); - validCharArr.add('Y'); - validCharArr.add('U'); - validCharArr.add('I'); - validCharArr.add('O'); - validCharArr.add('P'); - validCharArr.add('A'); - validCharArr.add('S'); - validCharArr.add('D'); - validCharArr.add('F'); - validCharArr.add('G'); - validCharArr.add('H'); - validCharArr.add('J'); - validCharArr.add('K'); - validCharArr.add('L'); - validCharArr.add('Z'); - validCharArr.add('X'); - validCharArr.add('C'); - validCharArr.add('V'); - validCharArr.add('B'); - validCharArr.add('N'); - validCharArr.add('M'); - validCharArr.add('1'); - validCharArr.add('2'); - validCharArr.add('3'); - validCharArr.add('4'); - validCharArr.add('5'); - validCharArr.add('6'); - validCharArr.add('7'); - validCharArr.add('8'); - validCharArr.add('9'); - validCharArr.add('0'); - - return validCharArr; + 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; } } From cde1671c02621fd6188431df0dfe6e17e1b67c6b Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Fri, 24 Jan 2020 14:51:26 -0500 Subject: [PATCH 05/31] added new test cases --- .../ithaca/dragon/bank/BankAccountTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index ddbe5ea5..43970222 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -45,6 +45,34 @@ void isEmailValidTest(){ assertFalse(BankAccount.isEmailValid("a.c@b..com")); assertFalse(BankAccount.isEmailValid("abc@def.co-")); + 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")); + + assertFalse(BankAccount.isEmailValid("a@b")); + assertFalse(BankAccount.isEmailValid("ab.com@j")); + assertFalse(BankAccount.isEmailValid("ab@j.c")); + assertFalse(BankAccount.isEmailValid("ab@domain.c")); + assertFalse(BankAccount.isEmailValid("ab.com@j")); + assertFalse(BankAccount.isEmailValid("ab#c#domain.com")); + assertFalse(BankAccount.isEmailValid("abc-@mail.com")); + assertFalse(BankAccount.isEmailValid("abc..def@mail.com")); + assertFalse(BankAccount.isEmailValid(".abc@mail.com")); + assertFalse(BankAccount.isEmailValid("abc$def@mail.com")); + assertFalse(BankAccount.isEmailValid("abc.def@mail.c")); + assertFalse(BankAccount.isEmailValid("abc.def@mail#archive.com")); + assertFalse(BankAccount.isEmailValid("abc.def@mail")); + assertFalse(BankAccount.isEmailValid("abc.def@mail..com")); + assertFalse(BankAccount.isEmailValid("abc@def.co-")); + + From 0409ba00557dbc552b323f2f895f0b4ffd2f4f2b Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Sun, 26 Jan 2020 23:48:20 -0500 Subject: [PATCH 06/31] added notes to the updated test cases. --- .../ithaca/dragon/bank/BankAccountTest.java | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 43970222..ff8561c4 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -24,26 +24,7 @@ void withdrawTest() { @Test void isEmailValidTest(){ - assertTrue(BankAccount.isEmailValid( "a@b.com")); - assertTrue(BankAccount.isEmailValid( "a-c@b.com")); - assertTrue(BankAccount.isEmailValid( "a.c@b.com")); - assertTrue(BankAccount.isEmailValid( "a_c@b.com")); - assertTrue(BankAccount.isEmailValid( "a.c@b.cc")); - assertTrue(BankAccount.isEmailValid( "a.c@b-archive.com")); - assertTrue(BankAccount.isEmailValid( "a.c@b.org")); - assertTrue(BankAccount.isEmailValid( "a.c@mail.com")); - - - assertFalse(BankAccount.isEmailValid("")); - assertFalse(BankAccount.isEmailValid("a-@b.com")); - assertFalse(BankAccount.isEmailValid("a..c@b.com")); - assertFalse(BankAccount.isEmailValid(".a@b.com")); - assertFalse(BankAccount.isEmailValid("a#c@b.com")); - assertFalse(BankAccount.isEmailValid("a.c@b.c")); - assertFalse(BankAccount.isEmailValid("a.c@b#archive.com")); - assertFalse(BankAccount.isEmailValid("a.c@b")); - assertFalse(BankAccount.isEmailValid("a.c@b..com")); - assertFalse(BankAccount.isEmailValid("abc@def.co-")); + assertTrue(BankAccount.isEmailValid("a@b.com")); assertTrue(BankAccount.isEmailValid("abc-d@mail.com")); @@ -56,20 +37,35 @@ void isEmailValidTest(){ 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-")); From 1672780f2c090391cec314a0e7a567a5a791fb77 Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Mon, 27 Jan 2020 00:11:31 -0500 Subject: [PATCH 07/31] added javadoc on the withdraw --- .../edu/ithaca/dragon/bank/BankAccount.java | 2 + .../ithaca/dragon/bank/BankAccountTest.java | 39 +++++++++++++++---- 2 files changed, 34 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 e978fa95..4491b4cd 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -30,6 +30,8 @@ public String getEmail(){ /** * @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 or is negative, + * returns the balance if the withdraw amount is less than your balance and a positive number. */ public void withdraw (double amount) { if (amount <= 0) throw new IllegalArgumentException("Cannot withdraw 0 or less."); diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index ff8561c4..e126fd09 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -16,10 +16,42 @@ void getBalanceTest() { @Test void withdrawTest() { + + //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); 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); + bankAccount.withdraw(100); + assertEquals(0, bankAccount.getBalance()); + + //tests for a valid withdrawal of $0 that will leave you with the same balance no matter what it is. Valid email. + BankAccount bankAccount2 = new BankAccount("abc.def@mail.com", 2); + bankAccount.withdraw(0); + assertEquals(2, bankAccount.getBalance()); + + //tests for an invalid withdrawal that is more than your current balance. valid email. + BankAccount bankAccount3 = new BankAccount("abc@mail.com", 50); + bankAccount.withdraw(100); + assertEquals(-50, bankAccount.getBalance()); + //tests for an invalid withdrawal that is a negative number. Valid email. + BankAccount bankAccount4 = new BankAccount("abc_def@mail.com", 50); + bankAccount.withdraw(100); + assertEquals(-50, bankAccount.getBalance()); + + //tests for a valid withdrawal with an invalid email. + BankAccount bankAccount5 = new BankAccount("a@b", 200); + bankAccount.withdraw(100); assertEquals(100, bankAccount.getBalance()); + + //tests for an invalid withdrawal with and invalid email. + BankAccount bankAccount6 = new BankAccount("ab.com@j", 50); + bankAccount.withdraw(100); + assertEquals(-50, bankAccount.getBalance()); + } @Test @@ -69,13 +101,6 @@ void isEmailValidTest(){ assertFalse(BankAccount.isEmailValid("abc@def.co-")); - - - - - - - } @Test From 1b4a0b096eacc73fa46a9fbdf46d59ed5de408c8 Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Mon, 27 Jan 2020 00:23:17 -0500 Subject: [PATCH 08/31] added javadoc on the get balance function. --- .../ithaca/dragon/bank/BankAccountTest.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index e126fd09..6cc043f4 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -9,9 +9,19 @@ class BankAccountTest { @Test void getBalanceTest() { + + //correctly gives you the balance when you have a valid email BankAccount bankAccount = new BankAccount("a@b.com", 200); + assertEquals(200, bankAccount.getBalance()); + //gives you your balance when your balance is 0. Valid email. + BankAccount bankAccount1 = new BankAccount("abc-d@mail.com", 200); assertEquals(200, bankAccount.getBalance()); + + //returns your balance when it is negative. Valid email. + BankAccount bankAccount2 = new BankAccount("abc-d@mail.com", 200); + assertEquals(200, bankAccount.getBalance()); + } @Test @@ -42,15 +52,6 @@ void withdrawTest() { bankAccount.withdraw(100); assertEquals(-50, bankAccount.getBalance()); - //tests for a valid withdrawal with an invalid email. - BankAccount bankAccount5 = new BankAccount("a@b", 200); - bankAccount.withdraw(100); - assertEquals(100, bankAccount.getBalance()); - - //tests for an invalid withdrawal with and invalid email. - BankAccount bankAccount6 = new BankAccount("ab.com@j", 50); - bankAccount.withdraw(100); - assertEquals(-50, bankAccount.getBalance()); } From 9863c6c6f7be753806354683c4a85aea62dc5941 Mon Sep 17 00:00:00 2001 From: MarathonTT Date: Mon, 27 Jan 2020 07:47:17 -0500 Subject: [PATCH 09/31] fixed constructor for BankAccount to throw exception when initialized to negative balance --- .../java/edu/ithaca/dragon/bank/BankAccount.java | 12 +++++------- 1 file changed, 5 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 4491b4cd..268a3215 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -11,13 +11,11 @@ public class BankAccount { * @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"); - } + if (isEmailValid(email)) this.email = email; + else throw new IllegalArgumentException("Email address: " + email + " is invalid, cannot create account"); + + if (startingBalance >= 0)this.balance = startingBalance; + else throw new IllegalArgumentException("Cannot open account with negative balance"); } public double getBalance(){ From ba61c05b225cbbffc3dcfb71f37c948be37e2951 Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Tue, 28 Jan 2020 16:26:29 -0500 Subject: [PATCH 10/31] withdraw test and get balance test working. --- .../edu/ithaca/dragon/bank/BankAccount.java | 19 ++++++--- .../ithaca/dragon/bank/BankAccountTest.java | 39 ++++++++++++------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 4491b4cd..90b1061c 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -1,5 +1,6 @@ package edu.ithaca.dragon.bank; + import java.util.HashSet; public class BankAccount { @@ -30,13 +31,21 @@ public String getEmail(){ /** * @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 or is negative, + * 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) { - if (amount <= 0) throw new IllegalArgumentException("Cannot withdraw 0 or less."); - if (balance >= amount) balance -= amount; - else throw new IllegalArgumentException("Cannot draw more than account balance."); + public void withdraw (double amount) throws InsufficientFundsException { + + if (amount < .01) + throw new IllegalArgumentException("Cannot withdraw $0 or less"); + + else if (balance >= amount) + balance -= amount; + + else if (balance < amount) + throw new InsufficientFundsException("Cannot draw more than account balance."); + } /** diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 6cc043f4..209077ac 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -15,17 +15,23 @@ void getBalanceTest() { assertEquals(200, bankAccount.getBalance()); //gives you your balance when your balance is 0. Valid email. - BankAccount bankAccount1 = new BankAccount("abc-d@mail.com", 200); - assertEquals(200, bankAccount.getBalance()); + BankAccount bankAccount1 = new BankAccount("abc-d@mail.com", 0); + assertEquals(0, bankAccount1.getBalance()); //returns your balance when it is negative. Valid email. - BankAccount bankAccount2 = new BankAccount("abc-d@mail.com", 200); - assertEquals(200, bankAccount.getBalance()); + BankAccount bankAccount2 = new BankAccount("abc-d@mail.com", -10); + assertEquals(-10, bankAccount2.getBalance()); + + //correctly gives you the balance when you have a valid email + BankAccount bankAccount3 = new BankAccount("a@b.com", 800.67); + assertEquals(800.67, bankAccount3.getBalance()); + + } @Test - void withdrawTest() { + 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); @@ -37,20 +43,23 @@ void withdrawTest() { bankAccount.withdraw(100); assertEquals(0, bankAccount.getBalance()); - //tests for a valid withdrawal of $0 that will leave you with the same balance no matter what it is. Valid email. - BankAccount bankAccount2 = new BankAccount("abc.def@mail.com", 2); - bankAccount.withdraw(0); - assertEquals(2, 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); + 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@mail.com", 50); - bankAccount.withdraw(100); - assertEquals(-50, bankAccount.getBalance()); + BankAccount bankAccount3 = new BankAccount("abc-d@mail.com", 348.08); + 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_def@mail.com", 50); - bankAccount.withdraw(100); - assertEquals(-50, bankAccount.getBalance()); + BankAccount bankAccount4 = new BankAccount("abc-d@mail.com", 590.02); + 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 + //assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a@b.com", 200).withdraw(2.005)); } From 019c4f254ac8a8464a4fe8f8ef7eb1ecc847095c Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Thu, 30 Jan 2020 18:26:39 -0500 Subject: [PATCH 11/31] Full bank account class working. --- .../edu/ithaca/dragon/bank/BankAccount.java | 184 +++++++++++++++--- .../ithaca/dragon/bank/BankAccountTest.java | 113 ++++++++++- 2 files changed, 269 insertions(+), 28 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 90b1061c..4c5c0d92 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -8,10 +8,33 @@ public class BankAccount { private String email; private double balance; + /** + * @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){ + 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; @@ -29,6 +52,46 @@ public String getEmail(){ return email; } + /** + * @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 bankAccountTrasferringTo, 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; + bankAccountTrasferringTo.balance += amount; + } + } + + /** + * @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 { + balance += amount; + } + } + /** * @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 @@ -36,6 +99,9 @@ public String getEmail(){ * returns the balance if the withdraw amount is less than your balance and a positive number. */ public void withdraw (double amount) throws 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"); @@ -54,43 +120,111 @@ else if (balance < amount) public static boolean isEmailValid(String email) { - //Checks that there is only 1 @. - int atIdx = email.indexOf('@'); - int lastAtIdx = email.lastIndexOf('@'); - if (atIdx != lastAtIdx || atIdx == -1) return false; + //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; - //Checks that there is a period in the one of two spots it must be in the domain. - //Note that there can only be 2 or 3 characters after the last period in the domain. - int len = email.length(); - int lastPeriodIdx = email.lastIndexOf('.'); - if (lastPeriodIdx == -1) return false; - if (lastPeriodIdx != len - 3 && lastPeriodIdx != len - 4) return false; + int locationOfLastDash = -2; + int locationOfCurrentDash; - //Checks that the @ is before the last period. - if (atIdx>lastPeriodIdx) return false; + int locationOfLastUnderscore = -2; + int locationOfCurrentUnderscore; - //Checks that all characters are valid and that the email obeys the letter after special rule. - int i = len - 1; + //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++; + } + + + + //makes sure there is not an invalid character + int j = 0; HashSet validCharSet = getValidCharSet(); - while (i > -1) { //going through each character in the email + while (j < lengthOfEmail) { + + + if (!validCharSet.contains(email.charAt(j))) return false; - //if invalid character return false - if (!validCharSet.contains(email.charAt(i))) return false; - //if special character, make sure following character is not special - if (isSpecialChar(email.charAt(i))){ + if (isSpecialChar(email.charAt(j))){ //if leading or last character is special, return false - if (i == 0 || i == len - 1) return false; - //if the next character is also special return false - if (isSpecialChar(email.charAt(i + 1))) 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 - i--; + j++; } - //if no invalid characters found or special rules broken, return true + return true; - } + } private static boolean isSpecialChar(char c) { return c == '.' || c == '-' || c == '_' || c == '@'; } private static HashSet getValidCharSet(){ diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 209077ac..79e739c2 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -7,6 +7,109 @@ class BankAccountTest { + @Test + void transferTest() throws InsufficientFundsException { + BankAccount bankAccount = new BankAccount("abg@g.com",100); //tests for a valid transfer + BankAccount bankAccount1 = new BankAccount("a@b.com",100); + bankAccount.transfer(bankAccount1,80); + assertEquals(180,bankAccount1.getBalance()); + assertEquals(20, bankAccount.getBalance()); + + BankAccount bankAccount2 = new BankAccount("abg@g.com",100); // tests for a valid withdraw at the boundary of your balance + BankAccount bankAccount3 = new BankAccount("a@b.com",100); + bankAccount2.transfer(bankAccount3,100); + assertEquals(200,bankAccount3.getBalance()); + assertEquals(0, bankAccount2.getBalance()); + + BankAccount bankAccount4 = new BankAccount("abg@g.com",30); //tests for a valid transfer at the boundary of the smallest amount + BankAccount bankAccount5 = new BankAccount("a@b.com",50); + bankAccount4.transfer(bankAccount5,0.01); + assertEquals(29.99,bankAccount4.getBalance()); + assertEquals(50.01, bankAccount5.getBalance()); + + BankAccount bankAccount6 = new BankAccount("abg@g.com",900); //tests for an invalid transfer of 0 + BankAccount bankAccount7 = new BankAccount("a@b.com",1000); + assertThrows(IllegalArgumentException.class, () -> bankAccount6.transfer(bankAccount7,0)); + assertEquals(900,bankAccount6.getBalance()); + assertEquals(1000, bankAccount7.getBalance()); + + BankAccount bankAccount8 = new BankAccount("abg@g.com",700); //tests for an invalid transfer of an amount larger than your balance + BankAccount bankAccount9 = new BankAccount("a@b.com",654.60); + assertThrows(InsufficientFundsException.class, () -> bankAccount8.transfer(bankAccount9,701)); + assertEquals(700,bankAccount8.getBalance()); + assertEquals(654.60, bankAccount9.getBalance()); + + BankAccount bankAccount10 = new BankAccount("abg@g.com",600); //tests for an invalid transfer of a negative number + BankAccount bankAccount11 = new BankAccount("a@b.com",600); + assertThrows(IllegalArgumentException.class, () -> bankAccount10.transfer(bankAccount11,-600)); + assertEquals(600,bankAccount10.getBalance()); + assertEquals(600, bankAccount11.getBalance()); + + BankAccount bankAccount12 = new BankAccount("abg@g.com",600); //tests for an invalid transfer of a negative number and an amount too large + BankAccount bankAccount13 = new BankAccount("a@b.com",600); + 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); //tests for a valid deposit + bankAccount.deposit(100); + assertEquals(100,bankAccount.getBalance()); + + BankAccount bankAccount2 = new BankAccount("abg@g.com",5); //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); //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); //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); //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()); + + + + + } + + @Test + void isAmountValidTest() throws InsufficientFundsException { + + BankAccount bankAccount = new BankAccount("ak3@g.com",9000);//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);//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); //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); //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() { @@ -59,8 +162,9 @@ void withdrawTest() throws InsufficientFundsException { assertEquals(590.02,bankAccount4.getBalance()); //tests for an invalid withdrawal that is a fraction of a penny. Should return an illegal argument exception - //assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a@b.com", 200).withdraw(2.005)); - + BankAccount bankAccount5 = new BankAccount("abc-d@mail.com", 300.50); + assertThrows(IllegalArgumentException.class, ()-> bankAccount4.withdraw(20.567)); + assertEquals(300.50,bankAccount4.getBalance()); } @@ -116,11 +220,14 @@ void isEmailValidTest(){ @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)); + +// BankAccount bankAccount1; +// assertThrows(IllegalArgumentException.class ()-> bankAccount1 = new BankAccount("a@b.com", 200.98034)); + } } \ No newline at end of file From 6c7b9fe5002ff3af6a2ed9dd88f378d48818b0ac Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Thu, 30 Jan 2020 18:37:59 -0500 Subject: [PATCH 12/31] Full bank account class working with Contructor test. --- src/main/java/edu/ithaca/dragon/bank/BankAccount.java | 3 +++ .../java/edu/ithaca/dragon/bank/BankAccountTest.java | 9 +++++++-- 2 files changed, 10 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 4c5c0d92..f60eaf07 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -87,6 +87,9 @@ 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; } diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index 79e739c2..29f4ff52 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -84,6 +84,10 @@ void depositTest() throws IllegalArgumentException{ assertThrows(IllegalArgumentException.class, () -> bankAccount5.deposit(-10000000.9874)); assertEquals(75,bankAccount5.getBalance()); + BankAccount bankAccount6 = new BankAccount("abg@g.com",75); //tests for an invalid deposit of 0. + assertThrows(IllegalArgumentException.class, () -> bankAccount5.deposit(0)); + assertEquals(75,bankAccount5.getBalance()); + @@ -225,8 +229,9 @@ void constructorTest() { //check for exception thrown correctly assertThrows(IllegalArgumentException.class, ()-> new BankAccount("", 100)); -// BankAccount bankAccount1; -// assertThrows(IllegalArgumentException.class ()-> bankAccount1 = new BankAccount("a@b.com", 200.98034)); + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("", 100.908)); //no email inputed and invalid starting balance + + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a@b.com", 10.804)); //throws exception for illegal starting balance } From 072eba396d1bdf6110728c0e13e5fcfcac7aeda1 Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Tue, 4 Feb 2020 15:23:53 -0500 Subject: [PATCH 13/31] added the api files --- .../345-08-BankInterfaceFiles/AdminAPI.java | 15 +++++ .../AdvancedAPI.java | 9 +++ .../345-08-BankInterfaceFiles/BasicAPI.java | 18 ++++++ .../CentralBank.java | 63 +++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdminAPI.java create mode 100644 src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdvancedAPI.java create mode 100644 src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/BasicAPI.java create mode 100644 src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/CentralBank.java diff --git a/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdminAPI.java b/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdminAPI.java new file mode 100644 index 00000000..b11e2b4e --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/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/345-08-BankInterfaceFiles/AdvancedAPI.java b/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdvancedAPI.java new file mode 100644 index 00000000..26253541 --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/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/345-08-BankInterfaceFiles/BasicAPI.java b/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/BasicAPI.java new file mode 100644 index 00000000..bd65b941 --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/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/345-08-BankInterfaceFiles/CentralBank.java b/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/CentralBank.java new file mode 100644 index 00000000..37508848 --- /dev/null +++ b/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/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 checkTotalAssets() { + return 0; + } + + public Collection findAcctIdsWithSuspiciousActivity() { + return null; + } + + public void freezeAccount(String acctId) { + + } + + public void unfreezeAcct(String acctId) { + + } + +} From 5319f02813ae4c6c74de4aa024566c69fa074167 Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Tue, 4 Feb 2020 15:25:36 -0500 Subject: [PATCH 14/31] api files in correct location --- .../dragon/{345-08-BankInterfaceFiles => bank}/AdminAPI.java | 0 .../dragon/{345-08-BankInterfaceFiles => bank}/AdvancedAPI.java | 0 .../dragon/{345-08-BankInterfaceFiles => bank}/BasicAPI.java | 0 .../dragon/{345-08-BankInterfaceFiles => bank}/CentralBank.java | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename src/main/java/edu/ithaca/dragon/{345-08-BankInterfaceFiles => bank}/AdminAPI.java (100%) rename src/main/java/edu/ithaca/dragon/{345-08-BankInterfaceFiles => bank}/AdvancedAPI.java (100%) rename src/main/java/edu/ithaca/dragon/{345-08-BankInterfaceFiles => bank}/BasicAPI.java (100%) rename src/main/java/edu/ithaca/dragon/{345-08-BankInterfaceFiles => bank}/CentralBank.java (100%) diff --git a/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdminAPI.java b/src/main/java/edu/ithaca/dragon/bank/AdminAPI.java similarity index 100% rename from src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdminAPI.java rename to src/main/java/edu/ithaca/dragon/bank/AdminAPI.java diff --git a/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdvancedAPI.java b/src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java similarity index 100% rename from src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/AdvancedAPI.java rename to src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java diff --git a/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/BasicAPI.java b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java similarity index 100% rename from src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/BasicAPI.java rename to src/main/java/edu/ithaca/dragon/bank/BasicAPI.java diff --git a/src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java similarity index 100% rename from src/main/java/edu/ithaca/dragon/345-08-BankInterfaceFiles/CentralBank.java rename to src/main/java/edu/ithaca/dragon/bank/CentralBank.java From 73ea2b516e7f727dce9350f813a2ecb4f721f93d Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Thu, 6 Feb 2020 00:09:46 -0500 Subject: [PATCH 15/31] changed something in central bank --- .../edu/ithaca/dragon/bank/BankAccount.java | 1 - .../edu/ithaca/dragon/bank/CentralBank.java | 7 +++++++ .../edu/ithaca/dragon/bank/BankAccountTest.java | 3 ++- .../edu/ithaca/dragon/bank/CentralBankTest.java | 17 +++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 72fdd90d..771693e9 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -1,6 +1,5 @@ package edu.ithaca.dragon.bank; - import java.util.HashSet; public class BankAccount { diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 37508848..07a4312c 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -1,9 +1,16 @@ package edu.ithaca.dragon.bank; +import java.lang.reflect.Array; import java.util.Collection; public class CentralBank implements AdvancedAPI, AdminAPI { + public CentralBank (){ + BankAccount customerCollection[ ]; + + + } + //----------------- BasicAPI methods -------------------------// public boolean confirmCredentials(String acctId, String password) { diff --git a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java index c98f1719..acf4be2c 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -235,4 +235,5 @@ void constructorTest() { } -} \ 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..1d006bd1 --- /dev/null +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -0,0 +1,17 @@ +package edu.ithaca.dragon.bank; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CentralBankTest { + + @Test + void checkBalance() { + BankAccount bankAccount = new BankAccount("a@b.com",305); + assertEquals(305, CentralBank.checkBalance("a@b.com")); + + + } + } + From b0c7799eef9464bd26fee3e3dff5d44cea81b65d Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Sun, 9 Feb 2020 18:09:10 -0500 Subject: [PATCH 16/31] checkbalance is working with unit tests --- .../edu/ithaca/dragon/bank/BankAccount.java | 4 ++-- .../java/edu/ithaca/dragon/bank/BasicAPI.java | 2 +- .../edu/ithaca/dragon/bank/CentralBank.java | 19 ++++++++++++++----- .../ithaca/dragon/bank/CentralBankTest.java | 14 +++++++++++++- 4 files changed, 30 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 771693e9..a146b4b6 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -4,8 +4,8 @@ public class BankAccount { - private String email; - private double balance; + public String email; + public double balance; /** * @throws IllegalArgumentException if amount to be withdrawn is invalid diff --git a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java index bd65b941..e23a2adf 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java +++ b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java @@ -5,7 +5,7 @@ public interface BasicAPI { boolean confirmCredentials(String acctId, String password); - double checkBalance(String acctId); + double checkBalance(String acctId, BankAccount[] customerCollection); void withdraw(String acctId, double amount) throws InsufficientFundsException; diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 07a4312c..13f2f5fd 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -6,9 +6,8 @@ public class CentralBank implements AdvancedAPI, AdminAPI { public CentralBank (){ - BankAccount customerCollection[ ]; - - + BankAccount customerCollection[]= new BankAccount[1]; + customerCollection[0] = new BankAccount("a@b.com",305); } //----------------- BasicAPI methods -------------------------// @@ -17,8 +16,14 @@ public boolean confirmCredentials(String acctId, String password) { return false; } - public double checkBalance(String acctId) { - return 0; + + 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) throws InsufficientFundsException { @@ -55,6 +60,10 @@ public double checkTotalAssets() { return 0; } + public double calcTotalAssets() { + return 0; + } + public Collection findAcctIdsWithSuspiciousActivity() { return null; } diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index 1d006bd1..cf805fae 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -3,13 +3,25 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class CentralBankTest { + + @Test void checkBalance() { + BankAccount customerCollection[]= new BankAccount[1]; + customerCollection[0] = new BankAccount("a@b.com",305); + + //checks the balance of an account in the collection BankAccount bankAccount = new BankAccount("a@b.com",305); - assertEquals(305, CentralBank.checkBalance("a@b.com")); + CentralBank cb = new CentralBank(); + assertEquals(305, cb.checkBalance("a@b.com",customerCollection)); + + //asks for the balance of an account not in the collection + assertThrows(IllegalArgumentException.class, ()-> cb.checkBalance("ab@c.com",customerCollection)); + } From e48fd39986c94e7577880cb8f6caa01e44723250 Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Sun, 9 Feb 2020 18:51:01 -0500 Subject: [PATCH 17/31] deposit is working --- .../java/edu/ithaca/dragon/bank/BasicAPI.java | 2 +- .../edu/ithaca/dragon/bank/CentralBank.java | 30 ++++++++++++++++++- .../ithaca/dragon/bank/CentralBankTest.java | 18 ++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java index e23a2adf..1fefea62 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java +++ b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java @@ -9,7 +9,7 @@ public interface BasicAPI { void withdraw(String acctId, double amount) throws InsufficientFundsException; - void deposit(String acctId, double amount); + void deposit(String acctId, double amount, BankAccount[] customerCollection); void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, double amount) throws InsufficientFundsException; diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 13f2f5fd..ba5093ea 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -10,6 +10,22 @@ public CentralBank (){ customerCollection[0] = new BankAccount("a@b.com",305); } + 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) { @@ -30,8 +46,20 @@ public void withdraw(String acctId, double amount) throws InsufficientFundsExcep } - public void deposit(String acctId, double amount) { + 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 { diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index cf805fae..d91cef86 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -15,13 +15,29 @@ void checkBalance() { customerCollection[0] = new BankAccount("a@b.com",305); //checks the balance of an account in the collection - BankAccount bankAccount = new BankAccount("a@b.com",305); CentralBank cb = new CentralBank(); assertEquals(305, cb.checkBalance("a@b.com",customerCollection)); //asks for the balance of an account not in the collection assertThrows(IllegalArgumentException.class, ()-> cb.checkBalance("ab@c.com",customerCollection)); + } + + @Test + void depositTest() { + BankAccount customerCollection[]= new BankAccount[1]; + customerCollection[0] = new BankAccount("a@b.com",305); + + //deposits a valid amount into a valid bank account + CentralBank cb = new CentralBank(); + cb.deposit("a@b.com",50, customerCollection); + assertEquals(355, cb.checkBalance("a@b.com",customerCollection)); + + //attempts to deposit an invalid amount + assertThrows(IllegalArgumentException.class, ()-> cb.deposit("a@b.com",5000.608,customerCollection)); + + //attempts to deposit 0 + assertThrows(IllegalArgumentException.class, ()-> cb.deposit("a@b.com",0,customerCollection)); } From 95535d6dd4849875f6a141f8a966a0f3f6c4d635 Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Sun, 9 Feb 2020 18:53:25 -0500 Subject: [PATCH 18/31] --- .idea/vcs.xml | 2 ++ SoftwareEngineeringPractice | 1 + pom.xml | 2 +- .../java/edu/ithaca/dragon/bank/BankAccount.java | 6 +++++- .../java/edu/ithaca/dragon/bank/BasicAPI.java | 2 +- .../java/edu/ithaca/dragon/bank/CentralBank.java | 14 ++++++++++++-- .../edu/ithaca/dragon/bank/CentralBankTest.java | 16 ++++++++++++---- 7 files changed, 34 insertions(+), 9 deletions(-) create mode 160000 SoftwareEngineeringPractice diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 35eb1ddf..f9bd5ad9 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,7 @@ + + \ No newline at end of file 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/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index a146b4b6..4bfa63b3 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -6,6 +6,7 @@ public class BankAccount { public String email; public double balance; + private String password; /** * @throws IllegalArgumentException if amount to be withdrawn is invalid @@ -30,7 +31,7 @@ else if (amount < 0){ //checks ifd the number is negative /** * @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"); @@ -38,6 +39,7 @@ public BankAccount(String email, double startingBalance){ if (isEmailValid(email)){ this.email = email; this.balance = startingBalance; + this.password = password; } else { throw new IllegalArgumentException("Email address: " + email + " is invalid, cannot create account"); @@ -49,6 +51,8 @@ public double getBalance(){ return balance; } + public String getPassword(){ return password; } + public String getEmail(){ return email; } diff --git a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java index e23a2adf..7fb82eee 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java +++ b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java @@ -3,7 +3,7 @@ //API to be used by ATMs public interface BasicAPI { - boolean confirmCredentials(String acctId, String password); + boolean confirmCredentials(String acctId, String password, BankAccount[] customerCollection); double checkBalance(String acctId, BankAccount[] customerCollection); diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 13f2f5fd..f607979b 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -7,12 +7,22 @@ public class CentralBank implements AdvancedAPI, AdminAPI { public CentralBank (){ BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",305); + customerCollection[0] = new BankAccount("a@b.com",305, "abcdefg1234"); } //----------------- BasicAPI methods -------------------------// - public boolean confirmCredentials(String acctId, String password) { + public boolean confirmCredentials(String acctId, String password, BankAccount[] customerCollection) { + for (int i = 0; i < customerCollection.length; i++) { + if (customerCollection[i].email == acctId){ + if (customerCollection[i].getPassword() != password){ + return false; + } + else{ + return true; + } + } + } return false; } diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index cf805fae..d5048fda 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -2,20 +2,28 @@ import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.*; public class CentralBankTest { + @Test + void checkCredentials(){ + BankAccount customerCollection[]= new BankAccount[1]; + customerCollection[0] = new BankAccount("a@b.com",305, "abcd1234"); + + CentralBank cb = new CentralBank(); + assertEquals(true, cb.confirmCredentials("a@b.com","abcd1234", customerCollection)); + + } @Test void checkBalance() { BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",305); + customerCollection[0] = new BankAccount("a@b.com",305, "abcd1234"); //checks the balance of an account in the collection - BankAccount bankAccount = new BankAccount("a@b.com",305); + //BankAccount bankAccount = new BankAccount("a@b.com",305, "abcd1234"); CentralBank cb = new CentralBank(); assertEquals(305, cb.checkBalance("a@b.com",customerCollection)); From 6d626e6c8dd206f31b4ecc27ceaa3cd42c8ed476 Mon Sep 17 00:00:00 2001 From: Joel0420 Date: Sun, 9 Feb 2020 19:31:55 -0500 Subject: [PATCH 19/31] Withdraw Working --- .../java/edu/ithaca/dragon/bank/BasicAPI.java | 4 ++-- .../edu/ithaca/dragon/bank/CentralBank.java | 19 ++++++++++++++++- .../ithaca/dragon/bank/CentralBankTest.java | 21 ++++++++++++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java index 1fefea62..e039f7cd 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java +++ b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java @@ -7,9 +7,9 @@ public interface BasicAPI { double checkBalance(String acctId, BankAccount[] customerCollection); - void withdraw(String acctId, double amount) throws InsufficientFundsException; + void withdraw(String acctId, double amount, BankAccount[] customerCollection) throws InsufficientFundsException; - void deposit(String acctId, double amount, BankAccount[] customerCollection); + void deposit(String acctId, double amount, BankAccount[] customerCollection) throws IllegalArgumentException; void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, double amount) throws InsufficientFundsException; diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index ba5093ea..24dcfb74 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -42,7 +42,24 @@ public double checkBalance(String acctId, BankAccount[] customerCollection) thro throw new IllegalArgumentException("The account ID you entered is not in the system"); } - public void withdraw(String acctId, double amount) throws InsufficientFundsException { + 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 + } + } + } diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index d91cef86..537627e8 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -41,5 +41,24 @@ void depositTest() { } - } + + @Test + void withdrawTest() throws InsufficientFundsException { + BankAccount customerCollection[]= new BankAccount[1]; + customerCollection[0] = new BankAccount("a@b.com",400); + CentralBank cb = new CentralBank(); + + //withdraws a valid amount with sufficient funds + cb.withdraw("a@b.com",200,customerCollection); + assertEquals(200,cb.checkBalance("a@b.com",customerCollection)); + + //withdraws an invalid amount with sufficient funds + assertThrows(IllegalArgumentException.class, ()-> cb.withdraw("a@b.com",-20.9088,customerCollection)); + + //withdraws a valid amount with insufficient funds + assertThrows(InsufficientFundsException.class, ()-> cb.withdraw("a@b.com",560,customerCollection)); + + + } +} From 92485a09452f6ca3981863c053abd47136a6f2e3 Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Sun, 9 Feb 2020 19:52:42 -0500 Subject: [PATCH 20/31] --- .idea/misc.xml | 2 +- .../ithaca/dragon/bank/BankAccountTest.java | 76 +++++++++---------- .../ithaca/dragon/bank/CentralBankTest.java | 5 +- 3 files changed, 40 insertions(+), 43 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d282d4d..dd62e066 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -11,7 +11,7 @@ - + \ 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 acf4be2c..1ea6ac00 100644 --- a/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java @@ -9,44 +9,44 @@ class BankAccountTest { @Test void transferTest() throws InsufficientFundsException { - BankAccount bankAccount = new BankAccount("abg@g.com",100); //tests for a valid transfer - BankAccount bankAccount1 = new BankAccount("a@b.com",100); + 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); // tests for a valid withdraw at the boundary of your balance - BankAccount bankAccount3 = new BankAccount("a@b.com",100); + 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); //tests for a valid transfer at the boundary of the smallest amount - BankAccount bankAccount5 = new BankAccount("a@b.com",50); + 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); //tests for an invalid transfer of 0 - BankAccount bankAccount7 = new BankAccount("a@b.com",1000); + 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); //tests for an invalid transfer of an amount larger than your balance - BankAccount bankAccount9 = new BankAccount("a@b.com",654.60); + 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); //tests for an invalid transfer of a negative number - BankAccount bankAccount11 = new BankAccount("a@b.com",600); + 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); //tests for an invalid transfer of a negative number and an amount too large - BankAccount bankAccount13 = new BankAccount("a@b.com",600); + 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()); @@ -64,27 +64,27 @@ void transferTest() throws InsufficientFundsException { @Test void depositTest() throws IllegalArgumentException{ - BankAccount bankAccount = new BankAccount("abg@g.com",0); //tests for a valid deposit + 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); //tests for a valid deposit with two decimal places + 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); //tests for an invalid deposit with more than two decimal places + 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); //tests for an invalid deposit with a negative number + 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); //tests for an invalid deposit with a negative number and more than 2 decimal places + 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); //tests for an invalid deposit of 0. + 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()); @@ -96,19 +96,19 @@ void depositTest() throws IllegalArgumentException{ @Test void isAmountValidTest() throws InsufficientFundsException { - BankAccount bankAccount = new BankAccount("ak3@g.com",9000);//gets a postive number with 2 decimal points. Should pass + 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);//gets a postive number with 1 decimal point. Should pass + 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); //checks to make sure a positive number with 3 decimal places is invalid + 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); //checks to make sure a negative number with 4 decimal places is invalid + 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()); @@ -118,19 +118,19 @@ void isAmountValidTest() throws InsufficientFundsException { void getBalanceTest() { //correctly gives you the balance when you have a valid email - BankAccount bankAccount = new BankAccount("a@b.com", 200); + 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); + 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); + 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); + BankAccount bankAccount3 = new BankAccount("a@b.com", 800.67, "ABCD1234"); assertEquals(800.67, bankAccount3.getBalance()); @@ -141,32 +141,32 @@ void getBalanceTest() { 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); + 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); + 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); + 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); + 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); + 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); + BankAccount bankAccount5 = new BankAccount("abc-d@mail.com", 300.50, "ABCD1234"); assertThrows(IllegalArgumentException.class, ()-> bankAccount4.withdraw(20.567)); assertEquals(300.50,bankAccount5.getBalance()); @@ -223,15 +223,15 @@ void isEmailValidTest(){ @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)); //no email inputed and invalid starting balance + 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)); //throws exception for illegal starting balance + assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a@b.com", 10.804, "ABCD1234")); //throws exception for illegal starting balance } diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index 47264629..5c312365 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -23,10 +23,7 @@ void checkBalance() { customerCollection[0] = new BankAccount("a@b.com",305, "abcd1234"); //checks the balance of an account in the collection -<<<<<<< HEAD //BankAccount bankAccount = new BankAccount("a@b.com",305, "abcd1234"); -======= ->>>>>>> e48fd39986c94e7577880cb8f6caa01e44723250 CentralBank cb = new CentralBank(); assertEquals(305, cb.checkBalance("a@b.com",customerCollection)); @@ -38,7 +35,7 @@ void checkBalance() { @Test void depositTest() { BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",305); + customerCollection[0] = new BankAccount("a@b.com",305, "abcd1234"); //deposits a valid amount into a valid bank account CentralBank cb = new CentralBank(); From c244b20e2eccb7d5ac16bc2719e523b261df0143 Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Sun, 9 Feb 2020 19:57:57 -0500 Subject: [PATCH 21/31] --- src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index d86f9633..c8f9927e 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -54,7 +54,7 @@ void depositTest() { @Test void withdrawTest() throws InsufficientFundsException { BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",400); + customerCollection[0] = new BankAccount("a@b.com",400, "ABCD1234"); CentralBank cb = new CentralBank(); //withdraws a valid amount with sufficient funds From 1949229149309f0a80940a4cfb3ef1b6885418e9 Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Mon, 10 Feb 2020 11:35:08 -0500 Subject: [PATCH 22/31] --- .../edu/ithaca/dragon/bank/BankAccount.java | 4 +-- .../edu/ithaca/dragon/bank/CentralBank.java | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 4bfa63b3..14bfef7f 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -63,7 +63,7 @@ public String getEmail(){ * @throws IllegalArgumentException if deposit amount is invalid * */ - public void transfer ( BankAccount bankAccountTrasferringTo, double amount) throws InsufficientFundsException, IllegalArgumentException{ + 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"); } @@ -77,7 +77,7 @@ else if (amount == 0 || amount < 0){ else { balance -= amount; - bankAccountTrasferringTo.balance += amount; + bankAccountTranferringTo.balance += amount; } } diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index f39dd952..36a71267 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -1,7 +1,8 @@ package edu.ithaca.dragon.bank; - import java.lang.reflect.Array; -import java.util.Collection; +import java.util.*; +import java.awt.*; + public class CentralBank implements AdvancedAPI, AdminAPI { @@ -100,8 +101,27 @@ public String transactionHistory(String acctId) { //----------------- AdvancedAPI methods -------------------------// + // I/O not working... need to fix in order for function to work public void createAccount(String acctId, double startingBalance) { - + System.out.println("Is this a checking or savings account? (c/s): "); + String A = scan.nextLine(); + System.out.println("Create Password: "); + String X = scan.nextLine(); + System.out.println("Please Re-type Password: "); + String Y = scan.nextLine(); + + while (X != Y){ + System.out.println("Please try again to re-type Password: "); + Y = scan.nextLine(); + } + BankAccount newAcct = new BankAccount(acctId,startingBalance,X); + if (A == "c" || A == "C"){ + //add to checking collection + } + else if (A == "s" || A == "S"){ + //add to saving collection + } + //add to customer collection } public void closeAccount(String acctId) { From 2c97c31bc99b1dcfa73842991f13898210e3386e Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Mon, 10 Feb 2020 21:55:52 -0500 Subject: [PATCH 23/31] committing changes to deposit, transfer, withdraw, checkCredentials, checkBalance --- .../edu/ithaca/dragon/bank/AdvancedAPI.java | 2 +- .../java/edu/ithaca/dragon/bank/BasicAPI.java | 8 +- .../edu/ithaca/dragon/bank/CentralBank.java | 180 ++++++++++++------ .../ithaca/dragon/bank/CentralBankTest.java | 137 +++++++------ 4 files changed, 198 insertions(+), 129 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java b/src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java index 26253541..647a8c5d 100644 --- a/src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java +++ b/src/main/java/edu/ithaca/dragon/bank/AdvancedAPI.java @@ -3,7 +3,7 @@ //API to be used by Teller systems public interface AdvancedAPI extends BasicAPI { - public void createAccount(String acctId, double startingBalance); + public void createAccount(String acctId, double startingBalance, String password); public void closeAccount(String acctId); } diff --git a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java index 6be792c9..b54a74bb 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java +++ b/src/main/java/edu/ithaca/dragon/bank/BasicAPI.java @@ -3,13 +3,13 @@ //API to be used by ATMs public interface BasicAPI { - boolean confirmCredentials(String acctId, String password, BankAccount[] customerCollection); + boolean confirmCredentials(String acctId, String password); - double checkBalance(String acctId, BankAccount[] customerCollection); + double checkBalance(String acctId); - void withdraw(String acctId, double amount, BankAccount[] customerCollection) throws InsufficientFundsException; + void withdraw(String acctId, double amount) throws InsufficientFundsException; - void deposit(String acctId, double amount, BankAccount[] customerCollection) throws IllegalArgumentException; + void deposit(String acctId, double amount) throws IllegalArgumentException; void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, double amount) throws InsufficientFundsException; diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 36a71267..6407278c 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -1,14 +1,15 @@ package edu.ithaca.dragon.bank; -import java.lang.reflect.Array; -import java.util.*; -import java.awt.*; + +import java.util.Collection; +import java.util.HashMap; public class CentralBank implements AdvancedAPI, AdminAPI { + private HashMap customerCollection = new HashMap(); + public CentralBank (){ - BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",305, "abcdefg1234"); + } private boolean isAmountValid(double amount) throws IllegalArgumentException{ @@ -17,25 +18,25 @@ private boolean isAmountValid(double amount) throws IllegalArgumentException{ 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."); + 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, BankAccount[] customerCollection) { - for (int i = 0; i < customerCollection.length; i++) { - if (customerCollection[i].email == acctId){ - if (customerCollection[i].getPassword() != password){ - return false; - } - else{ + 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; } } @@ -43,8 +44,109 @@ public boolean confirmCredentials(String acctId, String password, BankAccount[] 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.balance -= 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.balance += 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.balance -= amount; + depositBankAccount.balance += amount; + } + } - public double checkBalance(String acctId, BankAccount[] customerCollection) throws IllegalArgumentException { + @Override + public String transactionHistory(String acctId) { + 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) { + + } + + + /*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) @@ -52,16 +154,13 @@ public double checkBalance(String acctId, BankAccount[] customerCollection) thro } 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. @@ -70,13 +169,9 @@ public void withdraw(String acctId, double amount, BankAccount[] customerCollect 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"); } @@ -89,66 +184,29 @@ else if (amount == 0){ customerCollection[i].balance += amount; } } - public void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, double amount) throws InsufficientFundsException { - } - public String transactionHistory(String acctId) { return null; } - - //----------------- AdvancedAPI methods -------------------------// - - // I/O not working... need to fix in order for function to work public void createAccount(String acctId, double startingBalance) { - System.out.println("Is this a checking or savings account? (c/s): "); - String A = scan.nextLine(); - System.out.println("Create Password: "); - String X = scan.nextLine(); - System.out.println("Please Re-type Password: "); - String Y = scan.nextLine(); - - while (X != Y){ - System.out.println("Please try again to re-type Password: "); - Y = scan.nextLine(); - } - BankAccount newAcct = new BankAccount(acctId,startingBalance,X); - if (A == "c" || A == "C"){ - //add to checking collection - } - else if (A == "s" || A == "S"){ - //add to saving collection - } - //add to customer collection } - 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/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index c8f9927e..9a62e583 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -7,67 +7,78 @@ public class CentralBankTest { - @Test - void checkCredentials(){ - BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",305, "abcd1234"); - - CentralBank cb = new CentralBank(); - assertEquals(true, cb.confirmCredentials("a@b.com","abcd1234", customerCollection)); - - } - - @Test - void checkBalance() { - BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",305, "abcd1234"); - - //checks the balance of an account in the collection - //BankAccount bankAccount = new BankAccount("a@b.com",305, "abcd1234"); - CentralBank cb = new CentralBank(); - assertEquals(305, cb.checkBalance("a@b.com",customerCollection)); - - //asks for the balance of an account not in the collection - assertThrows(IllegalArgumentException.class, ()-> cb.checkBalance("ab@c.com",customerCollection)); - - } - - @Test - void depositTest() { - BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",305, "abcd1234"); - - //deposits a valid amount into a valid bank account - CentralBank cb = new CentralBank(); - cb.deposit("a@b.com",50, customerCollection); - assertEquals(355, cb.checkBalance("a@b.com",customerCollection)); - - //attempts to deposit an invalid amount - assertThrows(IllegalArgumentException.class, ()-> cb.deposit("a@b.com",5000.608,customerCollection)); - - //attempts to deposit 0 - assertThrows(IllegalArgumentException.class, ()-> cb.deposit("a@b.com",0,customerCollection)); - - - } - - @Test - void withdrawTest() throws InsufficientFundsException { - BankAccount customerCollection[]= new BankAccount[1]; - customerCollection[0] = new BankAccount("a@b.com",400, "ABCD1234"); - CentralBank cb = new CentralBank(); - - //withdraws a valid amount with sufficient funds - cb.withdraw("a@b.com",200,customerCollection); - assertEquals(200,cb.checkBalance("a@b.com",customerCollection)); - - //withdraws an invalid amount with sufficient funds - assertThrows(IllegalArgumentException.class, ()-> cb.withdraw("a@b.com",-20.9088,customerCollection)); - - //withdraws a valid amount with insufficient funds - assertThrows(InsufficientFundsException.class, ()-> cb.withdraw("a@b.com",560,customerCollection)); - - - } + @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)); + + + + } } - From 0445854358126c34437b613d1de677d8b48f06b9 Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Mon, 10 Feb 2020 22:17:59 -0500 Subject: [PATCH 24/31] committing closeAccount method/test --- .../java/edu/ithaca/dragon/bank/CentralBank.java | 16 +++++++++++++++- .../edu/ithaca/dragon/bank/CentralBankTest.java | 13 ++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 6407278c..97746178 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -141,8 +141,22 @@ public void createAccount(String acctId, double startingBalance, String password } @Override - public void closeAccount(String acctId) { + 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; } diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index 9a62e583..8ede6fd8 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -77,8 +77,19 @@ void transferTest() throws InsufficientFundsException { 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")); } } From f66e6bfed2ce0ca899d2a486e85a01724ac400ee Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Mon, 10 Feb 2020 22:40:59 -0500 Subject: [PATCH 25/31] committing changes --- .../edu/ithaca/dragon/bank/BankAccount.java | 34 +++++++++++++++---- .../edu/ithaca/dragon/bank/CentralBank.java | 10 +++--- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 14bfef7f..e3a29975 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -7,6 +7,9 @@ public class BankAccount { public String email; public double balance; private String password; + private int withdrawcount; + private int depositcount; + private int transfercount; /** * @throws IllegalArgumentException if amount to be withdrawn is invalid @@ -40,6 +43,9 @@ public BankAccount(String email, double startingBalance, String password){ 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"); @@ -77,7 +83,10 @@ else if (amount == 0 || amount < 0){ else { balance -= amount; + transfercount ++; bankAccountTranferringTo.balance += amount; + bankAccountTranferringTo.transfercount ++; + } } @@ -97,6 +106,8 @@ else if (amount == 0){ } else { balance += amount; + depositcount ++; + } } @@ -111,15 +122,16 @@ public void withdraw (double amount) throws InsufficientFundsException { throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); } - if (amount < .01) + if (amount < .01) { throw new IllegalArgumentException("Cannot withdraw $0 or less"); - - else if (balance >= amount) + } + else if (balance >= amount) { balance -= amount; - - else if (balance < amount) + withdrawcount++; + } + else if (balance < amount) { throw new InsufficientFundsException("Cannot draw more than account balance."); - + } } /** @@ -306,4 +318,14 @@ private static HashSet getValidCharSet(){ return validCharSet; } + + private int getWithdrawcount(){ + return withdrawcount; + } + private int getDepositcount(){ + return depositcount; + } + private int getTransfercount(){ + return transfercount; + } } diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 6407278c..e98231cb 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -67,7 +67,7 @@ public void withdraw(String acctId, double amount) throws InsufficientFundsExcep throw new InsufficientFundsException("Cannot draw more than account balance."); } else { - bankAccount.balance -= amount; + bankAccount.withdraw(amount); } } @@ -85,7 +85,7 @@ public void deposit(String acctId, double amount) throws IllegalArgumentExceptio throw new IllegalArgumentException("Cannot deposit less than $0.01"); } else { - bankAccount.balance += amount; + bankAccount.deposit(amount); } } @@ -104,13 +104,15 @@ public void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, doub throw new InsufficientFundsException("amount you wish to withdraw exceeds balance"); } else{ - withdrawBankAccount.balance -= amount; - depositBankAccount.balance += amount; + withdrawBankAccount.transfer(depositBankAccount,amount); + } } @Override public String transactionHistory(String acctId) { + BankAccount bankAccount = customerCollection.get(acctId); + bankAccount.get return null; } From 79e8e43925e52cc9ca9172c2504158cf2b579650 Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Mon, 10 Feb 2020 22:45:41 -0500 Subject: [PATCH 26/31] committing changes --- .../edu/ithaca/dragon/bank/BankAccount.java | 33 +++++++++++++++---- .../edu/ithaca/dragon/bank/CentralBank.java | 7 ++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 14bfef7f..7bb81ed5 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -7,6 +7,10 @@ public class BankAccount { 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 @@ -40,6 +44,9 @@ public BankAccount(String email, double startingBalance, String password){ 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"); @@ -77,7 +84,9 @@ else if (amount == 0 || amount < 0){ else { balance -= amount; + transferCount ++; bankAccountTranferringTo.balance += amount; + bankAccountTranferringTo.transferCount++; } } @@ -97,6 +106,7 @@ else if (amount == 0){ } else { balance += amount; + depositCount ++; } } @@ -111,15 +121,16 @@ public void withdraw (double amount) throws InsufficientFundsException { throw new IllegalArgumentException("The amount you entered " + amount + " is invalid"); } - if (amount < .01) + if (amount < .01) { throw new IllegalArgumentException("Cannot withdraw $0 or less"); - - else if (balance >= amount) + } + else if (balance >= amount) { balance -= amount; - - else if (balance < amount) + withdrawCount++; + } + else if (balance < amount) { throw new InsufficientFundsException("Cannot draw more than account balance."); - + } } /** @@ -306,4 +317,14 @@ private static HashSet getValidCharSet(){ return validCharSet; } + + private int getDepositCount(){ + return depositCount; + } + private int getWithdrawCount(){ + return withdrawCount; + } + private int getTransferCount(){ + return transferCount; + } } diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 97746178..bfaf9514 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -67,7 +67,7 @@ public void withdraw(String acctId, double amount) throws InsufficientFundsExcep throw new InsufficientFundsException("Cannot draw more than account balance."); } else { - bankAccount.balance -= amount; + bankAccount.withdraw(amount); } } @@ -85,7 +85,7 @@ public void deposit(String acctId, double amount) throws IllegalArgumentExceptio throw new IllegalArgumentException("Cannot deposit less than $0.01"); } else { - bankAccount.balance += amount; + bankAccount.deposit(amount); } } @@ -104,8 +104,7 @@ public void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, doub throw new InsufficientFundsException("amount you wish to withdraw exceeds balance"); } else{ - withdrawBankAccount.balance -= amount; - depositBankAccount.balance += amount; + withdrawBankAccount.transfer(depositBankAccount,amount); } } From bb7ff1fb282997e3b23f311071554d955d04798d Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Mon, 10 Feb 2020 23:00:16 -0500 Subject: [PATCH 27/31] committing transHistory --- .../edu/ithaca/dragon/bank/BankAccount.java | 18 +++++++++--------- .../edu/ithaca/dragon/bank/CentralBank.java | 9 +++++++++ .../ithaca/dragon/bank/CentralBankTest.java | 15 +++++++++++++++ 3 files changed, 33 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 7bb81ed5..f4315926 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -63,6 +63,15 @@ public double getBalance(){ 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. @@ -318,13 +327,4 @@ private static HashSet getValidCharSet(){ return validCharSet; } - private int getDepositCount(){ - return depositCount; - } - private int getWithdrawCount(){ - return withdrawCount; - } - private int getTransferCount(){ - return transferCount; - } } diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index bfaf9514..96176cd0 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -110,6 +110,15 @@ public void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, doub @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; } diff --git a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java index 8ede6fd8..0f2eff07 100644 --- a/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java +++ b/src/test/java/edu/ithaca/dragon/bank/CentralBankTest.java @@ -90,6 +90,21 @@ void removeAccountTest(){ 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"); + } } From de34f07312542afa20d36aad5ea867413832d4c8 Mon Sep 17 00:00:00 2001 From: PraveshPatel Date: Mon, 10 Feb 2020 23:06:32 -0500 Subject: [PATCH 28/31] committing trans History --- .../edu/ithaca/dragon/bank/BankAccount.java | 37 +++++++++++++++---- .../edu/ithaca/dragon/bank/CentralBank.java | 32 +++++++++++++--- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java index 14bfef7f..0845dd70 100644 --- a/src/main/java/edu/ithaca/dragon/bank/BankAccount.java +++ b/src/main/java/edu/ithaca/dragon/bank/BankAccount.java @@ -7,6 +7,10 @@ public class BankAccount { 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 @@ -40,6 +44,9 @@ public BankAccount(String email, double startingBalance, String password){ 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"); @@ -56,6 +63,15 @@ public double getBalance(){ 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. @@ -77,7 +93,9 @@ else if (amount == 0 || amount < 0){ else { balance -= amount; + transferCount ++; bankAccountTranferringTo.balance += amount; + bankAccountTranferringTo.transferCount++; } } @@ -97,6 +115,7 @@ else if (amount == 0){ } else { balance += amount; + depositCount ++; } } @@ -106,20 +125,21 @@ else if (amount == 0){ * 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) throws InsufficientFundsException { + 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) + if (amount < .01) { throw new IllegalArgumentException("Cannot withdraw $0 or less"); - - else if (balance >= amount) + } + else if (balance >= amount) { balance -= amount; - - else if (balance < amount) + withdrawCount++; + } + else if (balance < amount) { throw new InsufficientFundsException("Cannot draw more than account balance."); - + } } /** @@ -306,4 +326,5 @@ private static HashSet getValidCharSet(){ return validCharSet; } -} + +} \ No newline at end of file diff --git a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java index 6407278c..96176cd0 100644 --- a/src/main/java/edu/ithaca/dragon/bank/CentralBank.java +++ b/src/main/java/edu/ithaca/dragon/bank/CentralBank.java @@ -67,7 +67,7 @@ public void withdraw(String acctId, double amount) throws InsufficientFundsExcep throw new InsufficientFundsException("Cannot draw more than account balance."); } else { - bankAccount.balance -= amount; + bankAccount.withdraw(amount); } } @@ -85,7 +85,7 @@ public void deposit(String acctId, double amount) throws IllegalArgumentExceptio throw new IllegalArgumentException("Cannot deposit less than $0.01"); } else { - bankAccount.balance += amount; + bankAccount.deposit(amount); } } @@ -104,13 +104,21 @@ public void transfer(String acctIdToWithdrawFrom, String acctIdToDepositTo, doub throw new InsufficientFundsException("amount you wish to withdraw exceeds balance"); } else{ - withdrawBankAccount.balance -= amount; - depositBankAccount.balance += amount; + 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; } @@ -141,8 +149,22 @@ public void createAccount(String acctId, double startingBalance, String password } @Override - public void closeAccount(String acctId) { + 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; } From d7660d5bb0c8e19879ae4ff769d96d62e2ca2590 Mon Sep 17 00:00:00 2001 From: bencordova2 Date: Fri, 14 Feb 2020 09:26:20 -0500 Subject: [PATCH 29/31] Diagram update 2/14 --- .idea/SCentralBankFunctioning.iml | 2 ++ .idea/compiler.xml | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 .idea/SCentralBankFunctioning.iml 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/compiler.xml b/.idea/compiler.xml index 4e2bf249..a2eb181d 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,8 +6,9 @@ - + + From a255784ebcea37a375d20c73c7fa67d51533f003 Mon Sep 17 00:00:00 2001 From: bencordova2 <33659608+bencordova2@users.noreply.github.com> Date: Fri, 14 Feb 2020 09:28:44 -0500 Subject: [PATCH 30/31] Update README.md --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 87c23b2d..5696c7eb 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ # SoftwareEngineeringPractice ## grading - -To Do | correct ----|--- -at least 8 commits| -isEmailValid| -withdraw| -isamountValid| -constructor & withdraw fix| +UML: +https://www.draw.io/#G1ClJfvlx-DafIY19BRQ7PtKhMV5oUpCB4 +Use Case: +https://www.draw.io/#G16QeH0AdywvUHLzUHYaT50bWsE6ocJg3M +Sequence: + TransHistory: + https://www.draw.io/#G1QVD4o9ISDGrVABXhoTN2-LIzO8AcCr5z + CheckBal: + https://www.draw.io/#G1Hd51PWDOxilkn22_zhSnwhkEnVxUM0fU From e20ab5e5e03d313e4411be42ff8edba40afdeacd Mon Sep 17 00:00:00 2001 From: bencordova2 <33659608+bencordova2@users.noreply.github.com> Date: Fri, 14 Feb 2020 09:29:35 -0500 Subject: [PATCH 31/31] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 5696c7eb..281acb71 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,14 @@ ## grading UML: https://www.draw.io/#G1ClJfvlx-DafIY19BRQ7PtKhMV5oUpCB4 + Use Case: https://www.draw.io/#G16QeH0AdywvUHLzUHYaT50bWsE6ocJg3M + Sequence: + TransHistory: https://www.draw.io/#G1QVD4o9ISDGrVABXhoTN2-LIzO8AcCr5z + CheckBal: https://www.draw.io/#G1Hd51PWDOxilkn22_zhSnwhkEnVxUM0fU