Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Request for abc-bank application #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ project/plugins/project/

# IntelliJ
.idea
*.iml
*.iml
/bin
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ version := "1.0"

scalaVersion := "2.11.0"

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.1.6" % "test"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.1.6" % "test"
libraryDependencies ++= Seq( "joda-time" % "joda-time" % "2.3"
, "org.joda" % "joda-convert" % "1.6"
)
31 changes: 28 additions & 3 deletions src/main/scala/com/abc/Account.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.abc

import scala.collection.mutable.ListBuffer
import org.joda.time.{DateTime, Days}

object Account {
final val CHECKING: Int = 0
Expand Down Expand Up @@ -31,14 +32,38 @@ class Account(val accountType: Int, var transactions: ListBuffer[Transaction] =
if (amount <= 1000) amount * 0.001
else 1 + (amount - 1000) * 0.002
case Account.MAXI_SAVINGS =>
if (amount <= 1000) return amount * 0.02
if (amount <= 2000) return 20 + (amount - 1000) * 0.05
70 + (amount - 2000) * 0.1
numOfDaysSinceLastWithdrawal match {
case Some(daysInterval) if daysInterval >= 10 => amount * 0.05
case Some(daysInterval) => amount * 0.001
case None if numOfDaysSinceFirstDeposit > 10 => amount * 0.05
case _ => amount * 0.001
}
//if (amount <= 1000) return amount * 0.02
//if (amount <= 2000) return 20 + (amount - 1000) * 0.05
//70 + (amount - 2000) * 0.1
case _ =>
amount * 0.001
}
}

def sumTransactions(checkAllTransactions: Boolean = true): Double = transactions.map(_.amount).sum

private def numOfDaysSinceLastWithdrawal():Option[Int] = {
transactions.reverse find ( _.amount < 0) match {
case Some(lastWithdrawTrans) => { val lastWithdrawDateTime = new DateTime(lastWithdrawTrans.transactionDate);
val currDateTime = new DateTime();
val daysInterval = Days.daysBetween(currDateTime , lastWithdrawDateTime).getDays();
Some(daysInterval)
}
case None =>{ println("we are in None"); None }
}
}

private def numOfDaysSinceFirstDeposit:Int = {
val firstdepositDateTime = new DateTime(transactions.head.transactionDate);
val currDateTime = new DateTime();
val daysInterval = Days.daysBetween(currDateTime , firstdepositDateTime).getDays();
daysInterval
}

}
40 changes: 39 additions & 1 deletion src/main/scala/com/abc/Customer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Customer(val name: String, var accounts: ListBuffer[Account] = ListBuffer(
def numberOfAccounts: Int = accounts.size

def totalInterestEarned: Double = accounts.map(_.interestEarned).sum
private var fromAccount = None: Option[Account]
private var toAccount = None: Option[Account]


/**
* This method gets a statement
Expand Down Expand Up @@ -49,6 +52,41 @@ class Customer(val name: String, var accounts: ListBuffer[Account] = ListBuffer(
case _ => "N/A"
}

private def toDollars(number: Double): String = f"$$$number%.2f"
private def toDollars(number: Double): String = f"$$$number%.2f"

// Adding method to find is account exist for Customer to and from transfer is happening
private def accountExist(account: Account): Boolean = {
accounts.contains(account)
}

def transferFrom(account: Account): Customer={
if (!accountExist(account))
throw new IllegalArgumentException(f"No Such Account exist for $name\n")
else{
fromAccount = Some(account)
this}
}
def transferTo(account: Account): Customer={
if (!accountExist(account))
throw new IllegalArgumentException(f"No Such Account exist for $name\n")
else{
toAccount = Some(account)
this}
}

def transferAmount(amount: Double) = {
try {
fromAccount match {
case Some(fAccount) if amount > 0 && (fAccount.sumTransactions(true) >= amount) => toAccount match {
case Some(tAccount) => { fAccount.withdraw(amount); tAccount.deposit(amount) }
case None => throw new IllegalArgumentException(f"Transaction failed \n")
}
case None => throw new IllegalArgumentException(f"Transaction failed \n")
}
} catch{
case ex: Exception => printf(s"${ex.getMessage()} \n")
}
}

}

10 changes: 10 additions & 0 deletions src/test/scala/com/abc/BankTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,15 @@ class BankTest extends FlatSpec with Matchers {
checkingAccount.deposit(3000.0)
bank.totalInterestPaid should be(170.0)
}

it should "maxi savings accounts rate as 5% assuming no withdrawals" in {
val bank: Bank = new Bank
val checkingAccount: Account = new Account(Account.MAXI_SAVINGS)
bank.addCustomer(new Customer("Bill").openAccount(checkingAccount))
checkingAccount.deposit(300.000)
checkingAccount.withdraw(100.000)
checkingAccount.deposit(10.000)
bank.totalInterestPaid should be(0.210)
}

}
14 changes: 14 additions & 0 deletions src/test/scala/com/abc/CustomerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,23 @@ class CustomerTest extends FlatSpec with Matchers {
oscar.numberOfAccounts should be(2)
}

it should "testTransferBetweenTwoAccounts" in {
val checkingAccount: Account = new Account(Account.CHECKING)
val savingsAccount: Account = new Account(Account.SAVINGS)
val John: Customer = new Customer("Henry").openAccount(checkingAccount).openAccount(savingsAccount)
checkingAccount.deposit(200.0)
savingsAccount.deposit(400.0)

John.transferFrom(checkingAccount).transferTo(savingsAccount).transferAmount(150.00)
checkingAccount.sumTransactions(true) should be(50.0)
savingsAccount.sumTransactions(true) should be(550.0)
}

ignore should "testThreeAcounts" in {
val oscar: Customer = new Customer("Oscar").openAccount(new Account(Account.SAVINGS))
oscar.openAccount(new Account(Account.CHECKING))
oscar.numberOfAccounts should be(3)
}


}