Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.idea/**/dictionaries
.idea/**/shelf

target/
# Generated files
.idea/**/contentModel.xml

Expand Down
5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

181 changes: 170 additions & 11 deletions src/main/java/edu/ithaca/dragon/bank/BankAccount.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package edu.ithaca.dragon.bank;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BankAccount {

private String email;
Expand All @@ -8,39 +14,192 @@ public class BankAccount {
/**
* @throws IllegalArgumentException if email is invalid
*/
public BankAccount(String email, double startingBalance){
if (isEmailValid(email)){
public BankAccount(String email, double startingBalance) {
if (isEmailValid(email) && isAmountValid(startingBalance)){
this.email = email;
this.balance = startingBalance;
}

else {
throw new IllegalArgumentException("Email address: " + email + " is invalid, cannot create account");
throw new IllegalArgumentException("Email address or starting balance is invalid");
}
}

public double getBalance(){
return balance;
}
public double getBalance(){ return balance; }

public String getEmail(){
return email;
}

/**
* @throws InsufficientFundsException if withdraw amount is greater than balance
* @throws IllegalArgumentException if withdraw amount is negative or has more than 2 decimals
* @post reduces the balance by amount if amount is non-negative and smaller than balance
*/
public void withdraw (double amount) {
balance -= amount;
public void withdraw (double amount) throws InsufficientFundsException, IllegalArgumentException {
if (amount <= balance && isAmountValid(amount)){

balance -= amount;
}

else if(!isAmountValid(amount)){
throw new IllegalArgumentException("invalid amount to withdraw");
}

else {
throw new InsufficientFundsException("Not enough money");
}


}


/**
* @throws InsufficientFundsException if amount is more than is available
* @param amount
* @post subtracts from account1 to account2
*/

public void transfer(double amount, BankAccount toAccount) throws IllegalArgumentException, InsufficientFundsException {
if(amount > balance){
throw new InsufficientFundsException("Not enough money to transfer");
}
if(isAmountValid(amount)){

balance = balance - amount;


toAccount.deposit(amount);

DecimalFormat newFormat = new DecimalFormat("#. ##");


balance = Double.valueOf(newFormat.format(balance));

toAccount.balance = Double.valueOf(newFormat.format(toAccount.balance));
}


else if(!isAmountValid(amount)){
throw new IllegalArgumentException("Not a valid amount to transfer");
}



}





/**
* @throws IllegalArgumentException if amount is negative or contains more than two decimals
* @param amount
* @post adds the amount to the balance of the bankAccount, should increase
*/

public void deposit(double amount){
if(isAmountValid(amount)){
balance+=amount;

DecimalFormat newFormat = new DecimalFormat("#. ##");

balance = Double.valueOf(newFormat.format(balance));



}
else{
throw new IllegalArgumentException("Invalid deposit amount");
}
}

/**
* @param amount
* @return true if amount is not negative and two decimals or less
*/

public static boolean isAmountValid(double amount) {
if(amount < 0) {
return false;
}

String s = ""+amount;

int i = s.lastIndexOf('.');

if(s.length()-i-1 > 2){
return false;
}


return true;
}






public static boolean isEmailValid(String email){
if (email.indexOf('@') == -1){
if (email.indexOf('@') == -1 || email.indexOf('@') == 0) {
return false;
}
else {
return true;
if (email.indexOf('.') == -1) {
return false;
}
int count = 0;
for (int i = email.indexOf('@'); i < email.length(); i++) {
if (email.charAt(i) == ('.')) {
count++;
}
}
if (count != 1) {
return false;
}

for (int i = 0; i < email.indexOf('@'); i++) {
if (email.charAt(i) == ('.')) {
if (email.charAt(i + 1) == ('.')) {
return false;
}
}
}

int count3 = 0;
for (int j = email.indexOf('.'); j < email.length(); j++) {
if (email.charAt(j) != (' ')) {
count3++;
}
}


if (count3 <= 1) {
return false;
}


int count2=0;
for(int i=0; i<email.length(); i++){
if(email.indexOf('-') == email.indexOf('@')-1){
count2++;
}
}

if(count2 != 0){
return false;
}


int indx = email.lastIndexOf('.');

int length = email.substring(indx + 1).length();
return length >= 2;

}





}
127 changes: 121 additions & 6 deletions src/test/java/edu/ithaca/dragon/bank/BankAccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,74 @@
class BankAccountTest {

@Test
void getBalanceTest() {
void getBalanceTest() throws InsufficientFundsException, IllegalArgumentException{
BankAccount bankAccount = new BankAccount("a@b.com", 200);

assertEquals(200, bankAccount.getBalance());
assertEquals(200, bankAccount.getBalance()); //check get balance == starting balance
bankAccount.withdraw(50);
assertEquals(150,bankAccount.getBalance()); //check balance after withdraw
assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(200)); //check if throws exception for invalid withfraw
assertEquals(150, bankAccount.getBalance()); //check if balance remains same
assertThrows(IllegalArgumentException.class, () -> bankAccount.withdraw(-200));
assertEquals(150, bankAccount.getBalance()); //check for withdraw negative amount
bankAccount.withdraw(0);
assertEquals(150, bankAccount.getBalance()); //balance after withdraw 0
}

@Test
void withdrawTest() {
void withdrawTest() throws InsufficientFundsException, IllegalArgumentException {
BankAccount bankAccount = new BankAccount("a@b.com", 200);
bankAccount.withdraw(100);

assertEquals(100, bankAccount.getBalance());
assertEquals(100, bankAccount.getBalance()); // check if withdraw decreased in account
assertThrows(InsufficientFundsException.class, () -> bankAccount.withdraw(300)); //check if withdraw amount greater than amount in bank account/ test exception
assertThrows(IllegalArgumentException.class, () -> bankAccount.withdraw(100.0001));
bankAccount.withdraw(100.0000);
assertEquals(0, bankAccount.getBalance()); //check if amount of total withdraw equal to balance
bankAccount.withdraw(0);
assertEquals(0, bankAccount.getBalance());
assertThrows(IllegalArgumentException.class, () -> bankAccount.withdraw(-100)); //check if withdraw negative number
assertEquals(0, bankAccount.getBalance()); //shouldn't equal 100 and remain at balance it was before
assertThrows(IllegalArgumentException.class, () ->bankAccount.withdraw(10.103234));
assertEquals(0, bankAccount.getBalance()); //should still remain 0
}

@Test
void isEmailValidTest(){
//Email should be true if starts with character or number, includes @ and ends with .'character'

assertTrue(BankAccount.isEmailValid( "a@b.com"));
assertFalse( BankAccount.isEmailValid(""));
assertTrue(BankAccount.isEmailValid("23s@b.com"));
assertTrue(BankAccount.isEmailValid("a1@b.com"));
assertFalse(BankAccount.isEmailValid("@n.com"));
assertFalse(BankAccount.isEmailValid("n.com"));
assertFalse(BankAccount.isEmailValid("n!!@Gcom"));
assertTrue(BankAccount.isEmailValid("n@gmail.com"));
assertTrue(BankAccount.isEmailValid("n@gyi.org"));
assertFalse(BankAccount.isEmailValid(".n@Gcom"));
assertFalse(BankAccount.isEmailValid(".#Gcom"));
assertFalse(BankAccount.isEmailValid("n$@@G$$com"));
assertFalse(BankAccount.isEmailValid("n12.2@gyi.g"));
}

@Test
void isAmountValidTest(){

/*Equivalence tests would : test negative number no decimals, test positive number no decimals, test negative float, test positive float
test float with more than two decimals positive and negative, test float with one decimal positive and negative, test 0 */

assertTrue(BankAccount.isAmountValid(10)); //positive int
assertTrue(BankAccount.isAmountValid(0.01)); //positive float
assertTrue(BankAccount.isAmountValid(.01)); //positive float without leading number
assertTrue(BankAccount.isAmountValid(1.1)); //positive float with one decimal
assertTrue(BankAccount.isAmountValid(0)); //testing 0


assertFalse(BankAccount.isAmountValid(-10)); //negative int
assertFalse(BankAccount.isAmountValid(-0.01)); //negative float
assertFalse(BankAccount.isAmountValid(-.01)); //negative float without leading number
assertFalse(BankAccount.isAmountValid(0.009)); //positive float with more than 2 decimals
assertFalse(BankAccount.isAmountValid(-0.00242423)); //negative float with more than 2 decimals

}

@Test
Expand All @@ -35,6 +85,71 @@ void constructorTest() {
assertEquals(200, bankAccount.getBalance());
//check for exception thrown correctly
assertThrows(IllegalArgumentException.class, ()-> new BankAccount("", 100));
assertThrows(IllegalArgumentException.class, () -> bankAccount.withdraw(-100)); //illegal arguement negative withdraw

BankAccount bankAccount2 = new BankAccount("a@be.com", 200);
assertThrows(IllegalArgumentException.class, ()-> bankAccount2.withdraw(.00012));
assertThrows(InsufficientFundsException.class, () ->bankAccount2.withdraw(200.01)); //throw insufficient funds exception
assertThrows(IllegalArgumentException.class, () ->bankAccount2.withdraw(200.0001));
assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a32@gt.com", -100)); //invalid starting negative throw illegal argument
assertThrows(IllegalArgumentException.class, ()-> new BankAccount("a32@gt.com", 100.5345)); //invalid starting float throw illegal argument


}

@Test
void depositTest(){
BankAccount bankAccount = new BankAccount("a@bana.com", 100);

//Test valid amount to deposit (throws exceptions), test valid amounts deposit to increase the account amount


bankAccount.deposit(.01);
assertEquals(100.01, bankAccount.getBalance());
bankAccount.deposit(1.01);
assertEquals(101.02, bankAccount.getBalance());
assertThrows(IllegalArgumentException.class, () ->bankAccount.deposit(0.034234));
assertThrows(IllegalArgumentException.class, () ->bankAccount.deposit(-130));
bankAccount.deposit(1000);
assertEquals(1101.02, bankAccount.getBalance());
}

@Test
void transferTest() throws InsufficientFundsException{


BankAccount bankAccount1 = new BankAccount("bob@12.com", 500);
BankAccount bankAccount2 = new BankAccount("ang@2.com", 600);


bankAccount1.transfer(100, bankAccount2); //check if normal transfer works (positive number)

assertEquals(400, bankAccount1.getBalance());

assertEquals(700, bankAccount2.getBalance());

assertThrows(InsufficientFundsException.class, () ->bankAccount1.transfer(10000, bankAccount2)); //throw insufficient funds

assertThrows(IllegalArgumentException.class, () ->bankAccount1.transfer(-120, bankAccount2)); //throw illegal argument negative

assertThrows(IllegalArgumentException.class, () ->bankAccount1.transfer(.43234, bankAccount2)); //throw illegal argument more than 2 decimals

assertEquals(400, bankAccount1.getBalance()); //make sure amount didn't change

assertEquals(700, bankAccount2.getBalance()); //make sure amount didn't change

bankAccount1.transfer(1.04, bankAccount2); //check transfer with decimal number

assertEquals(398.96, bankAccount1.getBalance());

assertEquals(701.04, bankAccount2.getBalance());

bankAccount2.transfer(.04, bankAccount1); //check transfer amount of just cents in reverse order

assertEquals(399, bankAccount1.getBalance());

assertEquals(701.00, bankAccount2.getBalance());

}

}