diff --git a/.gitignore b/.gitignore index 95abee5..d20e704 100644 --- a/.gitignore +++ b/.gitignore @@ -103,4 +103,5 @@ target/* .project .classpath -.settings \ No newline at end of file +.settings +/target/ diff --git a/UML/Bank-AccountUML.jpg b/UML/Bank-AccountUML.jpg new file mode 100644 index 0000000..3d704a5 Binary files /dev/null and b/UML/Bank-AccountUML.jpg differ diff --git a/src/main/java/io/zipcoder/Account.java b/src/main/java/io/zipcoder/Account.java deleted file mode 100644 index 1a0742e..0000000 --- a/src/main/java/io/zipcoder/Account.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.zipcoder; - -public abstract class Account { -} diff --git a/src/main/java/io/zipcoder/account/Account.java b/src/main/java/io/zipcoder/account/Account.java new file mode 100644 index 0000000..cecbb3d --- /dev/null +++ b/src/main/java/io/zipcoder/account/Account.java @@ -0,0 +1,83 @@ +package io.zipcoder.account; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +import io.zipcoder.holder.AccountHolder; +import io.zipcoder.transaction.Transaction; +import io.zipcoder.transaction.TransactionType; + +public abstract class Account { + + private final AccountHolder accountHolder; + private BigDecimal balance; + private final long accountNumber; + private final List transactionList; + + public Account(AccountHolder accountHolder, BigDecimal balance, long accountNumber) { + if(balance.compareTo(BigDecimal.ZERO)<0){ + throw new IllegalArgumentException("Initial balance can't be negative"); + } + this.accountHolder = accountHolder; + this.balance = balance; + this.accountNumber = accountNumber; + this.transactionList = new ArrayList(); + addTransaction(TransactionType.CREATION, balance, balance); + } + + public AccountHolder getAccountHolder() { + return accountHolder; + } + + public BigDecimal getBalance() { + return balance; + } + + public long getAccountNumber() { + return accountNumber; + } + + public void withdraw(BigDecimal amount) { + if (amount.compareTo(BigDecimal.ZERO) > 0) { + this.balance = balance.subtract(amount); + this.transactionList.add(new Transaction(TransactionType.WITHDRAWAL, amount, this.balance)); + } else { + throw new IllegalArgumentException("The amount is negative."); + } + + } + + public void deposit(BigDecimal amount) { + + if (amount.compareTo(BigDecimal.ZERO) > 0) { + this.balance = balance.add(amount); + this.addTransaction(TransactionType.DEPOSIT, amount, this.balance); + + } else { + throw new IllegalArgumentException("Amount can't be negative."); + } + + } + + protected void addTransaction(TransactionType type, BigDecimal amount, BigDecimal remainingBalance ){ + transactionList.add(new Transaction(type, amount, remainingBalance)); + } + + public String printTransactions(int n) { + String result = ""; + int index = transactionList.size(); + + if (n <= index) { + for (int i = 0; i < n; i++) { + result+=transactionList.get(index-1)+"\n"; + index--; + } + }else{ + return "There are not enough transactions in this account."; + } + return result; + } + + +} diff --git a/src/main/java/io/zipcoder/account/CheckingAccount.java b/src/main/java/io/zipcoder/account/CheckingAccount.java new file mode 100644 index 0000000..f88d722 --- /dev/null +++ b/src/main/java/io/zipcoder/account/CheckingAccount.java @@ -0,0 +1,40 @@ +package io.zipcoder.account; + +import java.math.BigDecimal; + +import io.zipcoder.holder.AccountHolder; + +public class CheckingAccount extends Account { + + private boolean canOverDraft; + + + public CheckingAccount(AccountHolder accountHolder, BigDecimal balance, long accountNumber, boolean canOverDraft) { + super(accountHolder, balance, accountNumber); + this.canOverDraft=canOverDraft; + } + + + public boolean isCanOverDraft() { + return canOverDraft; + } + + + public void setCanOverDraft(boolean canOverDraft) { + this.canOverDraft = canOverDraft; + } + + @Override + public void withdraw(BigDecimal amount){ + if(amount.subtract(this.getBalance()).compareTo(BigDecimal.ZERO)>0){ + if(isCanOverDraft()){ + super.withdraw(amount); + }else{ + System.out.println("This accoun't doesn't have enough founds."); + } + }else{ + super.withdraw(amount); + } + } + +} diff --git a/src/main/java/io/zipcoder/account/InvestmentAccount.java b/src/main/java/io/zipcoder/account/InvestmentAccount.java new file mode 100644 index 0000000..4ef020e --- /dev/null +++ b/src/main/java/io/zipcoder/account/InvestmentAccount.java @@ -0,0 +1,40 @@ +package io.zipcoder.account; + +import java.math.BigDecimal; + +import io.zipcoder.holder.AccountHolder; + +public class InvestmentAccount extends Account { + + private double interestRate; + + public InvestmentAccount(AccountHolder accountHolder, BigDecimal balance, long accountNumber, double interestRate) { + super(accountHolder, balance, accountNumber); + this.interestRate = interestRate; + } + + public double getInterestRate() { + return interestRate; + } + + public void setInterestRate(double interestRate) { + this.interestRate = interestRate; + } + + public void applyInterest(){ + + BigDecimal interestBD = new BigDecimal(this.interestRate); + + BigDecimal totalInterest = interestBD.multiply(this.getBalance()).divide(new BigDecimal("100.00")); + + super.deposit(totalInterest); + + } + + + + + + + +} diff --git a/src/main/java/io/zipcoder/account/SavingAccount.java b/src/main/java/io/zipcoder/account/SavingAccount.java new file mode 100644 index 0000000..6f676b8 --- /dev/null +++ b/src/main/java/io/zipcoder/account/SavingAccount.java @@ -0,0 +1,40 @@ +package io.zipcoder.account; + +import java.math.BigDecimal; + +import io.zipcoder.holder.AccountHolder; + +public class SavingAccount extends InvestmentAccount{ + + private boolean canOverDraft; + + + public SavingAccount(AccountHolder accountHolder, BigDecimal balance, long accountNumber, boolean canOverDraft,double interestRate) { + super(accountHolder, balance, accountNumber,interestRate); + this.canOverDraft=canOverDraft; + } + + + public boolean isCanOverDraft() { + return canOverDraft; + } + + + public void setCanOverDraft(boolean canOverDraft) { + this.canOverDraft = canOverDraft; + } + + @Override + public void withdraw(BigDecimal amount){ + if(amount.subtract(this.getBalance()).compareTo(BigDecimal.ZERO)>0){ + if(isCanOverDraft()){ + super.withdraw(amount); + }else{ + System.out.println("This accoun't doesn't have enough founds."); + } + }else{ + super.withdraw(amount); + } + } + +} diff --git a/src/main/java/io/zipcoder/holder/AccountHolder.java b/src/main/java/io/zipcoder/holder/AccountHolder.java new file mode 100644 index 0000000..9ea160d --- /dev/null +++ b/src/main/java/io/zipcoder/holder/AccountHolder.java @@ -0,0 +1,5 @@ +package io.zipcoder.holder; + +public abstract class AccountHolder { + +} diff --git a/src/main/java/io/zipcoder/holder/Business.java b/src/main/java/io/zipcoder/holder/Business.java new file mode 100644 index 0000000..db9f2a5 --- /dev/null +++ b/src/main/java/io/zipcoder/holder/Business.java @@ -0,0 +1,23 @@ +package io.zipcoder.holder; + +public class Business extends AccountHolder { + + private String businessName; + + public Business(String businessName) { + this.businessName = businessName; + } + + public String getBusinessName() { + return businessName; + } + + public void setBusinessName(String businessName) { + this.businessName = businessName; + } + + + + + +} diff --git a/src/main/java/io/zipcoder/holder/Person.java b/src/main/java/io/zipcoder/holder/Person.java new file mode 100644 index 0000000..2a01340 --- /dev/null +++ b/src/main/java/io/zipcoder/holder/Person.java @@ -0,0 +1,40 @@ +package io.zipcoder.holder; + +public class Person extends AccountHolder { + private String firstName; + private String middleName; + private String lastName; + + public Person(String firstName, String middleName, String lastName){ + + this.firstName = firstName; + this.middleName = middleName; + this.lastName = lastName; + + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getMiddleName() { + return middleName; + } + + public void setMiddleName(String middleName) { + this.middleName = middleName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + +} diff --git a/src/main/java/io/zipcoder/transaction/Transaction.java b/src/main/java/io/zipcoder/transaction/Transaction.java new file mode 100644 index 0000000..39dc86a --- /dev/null +++ b/src/main/java/io/zipcoder/transaction/Transaction.java @@ -0,0 +1,28 @@ +package io.zipcoder.transaction; + +import java.math.BigDecimal; + +public class Transaction { + + private final TransactionType type; + private final BigDecimal amount; + private final BigDecimal remainingBalance; + + + public Transaction(TransactionType type, BigDecimal amount, BigDecimal remainingBalance ){ + + this.type = type; + this.amount = amount; + this.remainingBalance = remainingBalance; + + } + + + @Override + public String toString() { + return "Transaction: [Type=" + type + ", Amount=" + amount + ", Remaining Balance=" + remainingBalance + "]"; + } + + + +} diff --git a/src/main/java/io/zipcoder/transaction/TransactionType.java b/src/main/java/io/zipcoder/transaction/TransactionType.java new file mode 100644 index 0000000..42aa963 --- /dev/null +++ b/src/main/java/io/zipcoder/transaction/TransactionType.java @@ -0,0 +1,7 @@ +package io.zipcoder.transaction; + +public enum TransactionType { + + WITHDRAWAL,DEPOSIT, CREATION, INTEREST + +} diff --git a/src/test/java/io/zipcoder/AccountTest.java b/src/test/java/io/zipcoder/AccountTest.java index f4e8dee..a5a4550 100644 --- a/src/test/java/io/zipcoder/AccountTest.java +++ b/src/test/java/io/zipcoder/AccountTest.java @@ -1,4 +1,109 @@ package io.zipcoder; +import java.math.BigDecimal; + +import org.junit.Before; +import org.junit.Test; + +import io.zipcoder.account.Account; +import io.zipcoder.holder.AccountHolder; +import io.zipcoder.holder.Person; + +import org.junit.Assert; + public class AccountTest { + + class AccountFixture extends Account{ + + public AccountFixture(AccountHolder accountHolder, BigDecimal balance, long accountNumber) { + super(accountHolder, balance, accountNumber); + } + + } + + private Account target; + + @Before + public void setup(){ + AccountHolder holderFixture = new Person("Jhonny", "Anything", "Tom"); + target = new AccountFixture(holderFixture,new BigDecimal("10.00"),100000001); + } + + @Test(expected=IllegalArgumentException.class) + public void test_createAccount_negativeBalance(){ + //:Given + AccountHolder holderFixture = new Person("Jhonny", "Anything", "Tom"); + //:When + new AccountFixture(holderFixture, new BigDecimal("-10.00"), 1000002); + + } + + @Test + public void test_deposit_success(){ + //:When + target.deposit(new BigDecimal("3.00")); + //:Then + Assert.assertEquals(new BigDecimal("13.00"), target.getBalance()); + + } + + @Test(expected=IllegalArgumentException.class) + public void test_deposit_amountUnderZero(){ + + //:When + target.deposit(new BigDecimal("-3.00")); + } + + @Test + public void test_withdraw_success(){ + + //:When + target.withdraw(new BigDecimal("2.00")); + + //:Then + Assert.assertEquals(new BigDecimal("8.00"), target.getBalance()); + } + + @Test(expected=IllegalArgumentException.class) + public void test_withdraw_amountUnderZero(){ + + //:When + target.withdraw(new BigDecimal("-2.00")); + + } + + @Test + public void test_withdraw_overdraft(){ + //:Given + + //:When + target.withdraw(new BigDecimal("20.00")); + + //:Then + Assert.assertEquals(new BigDecimal("-10.00"), target.getBalance()); + + } + + @Test + public void test_printTransactions_success(){ + //:Given + + target.deposit(new BigDecimal("30.00")); + target.withdraw(new BigDecimal("10.00")); + //:When + String result = target.printTransactions(2); + + String expected ="Transaction: [Type=" + "WITHDRAWAL" + ", Amount=" + "10.00" + ", Remaining Balance=" + "30.00" + "]\n"+ + "Transaction: [Type=" + "DEPOSIT" + ", Amount=" + "30.00" + ", Remaining Balance=" + "40.00" + "]\n"; + + + //:Then + + Assert.assertEquals(expected, result); + + + } + + + } diff --git a/src/test/java/io/zipcoder/CheckingAccountTest.java b/src/test/java/io/zipcoder/CheckingAccountTest.java new file mode 100644 index 0000000..3f338ba --- /dev/null +++ b/src/test/java/io/zipcoder/CheckingAccountTest.java @@ -0,0 +1,49 @@ +package io.zipcoder; + +import java.math.BigDecimal; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import io.zipcoder.account.CheckingAccount; +import io.zipcoder.holder.AccountHolder; +import io.zipcoder.holder.Business; + +public class CheckingAccountTest { + + private CheckingAccount target; + + @Before + public void setup(){ + AccountHolder holderFixture = new Business("JP Morgan"); + target = new CheckingAccount(holderFixture,new BigDecimal("10.00"), 1000001, true); + } + + @Test + public void test_withdraw_noProtected(){ + //:When + target.withdraw(new BigDecimal("20.00")); + //:Then + Assert.assertEquals(new BigDecimal("-10.00"), target.getBalance()); + } + + @Test + public void test_withdraw_isProtected(){ + //:When + target.setCanOverDraft(false); + target.withdraw(new BigDecimal("20.00")); + //:Then + Assert.assertEquals(new BigDecimal("10.00"), target.getBalance()); + } + + @Test + public void test_withdraw_isProtectedEnoughFounds(){ + //:When + target.setCanOverDraft(false); + target.withdraw(new BigDecimal("5.00")); + //:Then + Assert.assertEquals(new BigDecimal("5.00"), target.getBalance()); + } + +} diff --git a/src/test/java/io/zipcoder/InvestmentAccountTest.java b/src/test/java/io/zipcoder/InvestmentAccountTest.java new file mode 100644 index 0000000..9930f32 --- /dev/null +++ b/src/test/java/io/zipcoder/InvestmentAccountTest.java @@ -0,0 +1,33 @@ +package io.zipcoder; + +import java.math.BigDecimal; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import io.zipcoder.account.InvestmentAccount; +import io.zipcoder.holder.AccountHolder; +import io.zipcoder.holder.Business; + +public class InvestmentAccountTest { + + private InvestmentAccount target; + + @Before + public void setup(){ + AccountHolder holderFixture = new Business("JP Morgan"); + target = new InvestmentAccount(holderFixture, new BigDecimal("10.00"), 1000001, 3.5); + } + + @Test + public void test_applyInterest_success(){ + //:When + target.applyInterest(); + + //:Then + Assert.assertEquals(new BigDecimal("10.35"), target.getBalance()); + + } + +} diff --git a/src/test/java/io/zipcoder/SavingAccountTest.java b/src/test/java/io/zipcoder/SavingAccountTest.java new file mode 100644 index 0000000..6da2a1e --- /dev/null +++ b/src/test/java/io/zipcoder/SavingAccountTest.java @@ -0,0 +1,49 @@ +package io.zipcoder; + +import java.math.BigDecimal; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import io.zipcoder.account.SavingAccount; +import io.zipcoder.holder.AccountHolder; +import io.zipcoder.holder.Business; + +public class SavingAccountTest { + + private SavingAccount target; + + @Before + public void setup(){ + AccountHolder holderFixture = new Business("JP Morgan"); + target = new SavingAccount(holderFixture, new BigDecimal("10.00"), 100001, true, 3.5); + } + + @Test + public void test_withdraw_noProtected(){ + //:When + target.withdraw(new BigDecimal("20.00")); + //:Then + Assert.assertEquals(new BigDecimal("-10.00"), target.getBalance()); + } + + @Test + public void test_withdraw_isProtected(){ + //:When + target.setCanOverDraft(false); + target.withdraw(new BigDecimal("20.00")); + //:Then + Assert.assertEquals(new BigDecimal("10.00"), target.getBalance()); + } + + @Test + public void test_withdraw_isProtectedEnoughFounds(){ + //:When + target.setCanOverDraft(false); + target.withdraw(new BigDecimal("5.00")); + //:Then + Assert.assertEquals(new BigDecimal("5.00"), target.getBalance()); + } + +}