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

commit-1 (Alternate Features) #47

Open
wants to merge 1 commit 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
12 changes: 7 additions & 5 deletions src/main/scala/com/abc/Account.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.abc

import scala.collection.mutable.ListBuffer
import java.util.Date

object Account {
final val CHECKING: Int = 0
final val SAVINGS: Int = 1
final val MAXI_SAVINGS: Int = 2
}

class Account(val accountType: Int, var transactions: ListBuffer[Transaction] = ListBuffer()) {
class Account(val accountType: Int, val transactions: ListBuffer[Transaction] = ListBuffer()) {

def deposit(amount: Double) {
if (amount <= 0)
Expand All @@ -31,14 +32,15 @@ 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
val currDate: Date = DateProvider.now
if (transactions.exists(tx => ((currDate.getTime() - tx.transactionDate.getTime()) / (1000 * 60 * 60 * 24)) <10)) {
return amount * 0.001
} else return amount * 0.05
case _ =>
amount * 0.001
}
}

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

}
13 changes: 1 addition & 12 deletions src/main/scala/com/abc/Bank.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,7 @@ class Bank {
return total
}

def getFirstCustomer: String = {
try {
customers = null
customers(0).name
}
catch {
case e: Exception => {
e.printStackTrace
return "Error"
}
}
}
def getFirstCustomer: String = customers.headOption.map(_.name).getOrElse("None")

}

Expand Down
9 changes: 9 additions & 0 deletions src/main/scala/com/abc/Customer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class Customer(val name: String, var accounts: ListBuffer[Account] = ListBuffer(
s"\nTotal In All Accounts ${toDollars(totalAcrossAllAccounts)}"
statement
}

def transferBetweenTheAccounts(fromAcc: Account, toAcc : Account, amount: Double) = {
if (amount <= 0)
throw new IllegalArgumentException("amount must be greater than zero")
else {
fromAcc.withdraw(amount)
toAcc.deposit(amount)
}
}

private def statementForAccount(a: Account): String = {
val accountType = a.accountType match {
Expand Down
9 changes: 0 additions & 9 deletions src/main/scala/com/abc/DateProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ import java.util.Calendar
import java.util.Date

object DateProvider {
def getInstance: DateProvider = {
if (instance == null) instance = new DateProvider
instance
}

private var instance: DateProvider = null
}

class DateProvider {
def now: Date = {
return Calendar.getInstance.getTime
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/abc/Transaction.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.abc

case class Transaction(var amount: Double) {
val transactionDate = DateProvider.getInstance.now
val transactionDate = DateProvider.now
}

34 changes: 27 additions & 7 deletions src/test/scala/com/abc/BankTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ class BankTest extends FlatSpec with Matchers {
bank.addCustomer(john)
bank.customerSummary should be("Customer Summary\n - John (1 account)")
}

it should "customer summary2" in {
val bank: Bank = new Bank
val checkingAccount: Account = new Account(Account.CHECKING)
val savingsAccount: Account = new Account(Account.SAVINGS)
val john: Customer = new Customer("John").openAccount(checkingAccount)
val joe: Customer = new Customer("Joe").openAccount(checkingAccount).openAccount(savingsAccount)
bank.addCustomer(john)
bank.addCustomer(joe)
bank.customerSummary should be("Customer Summary\n - John (1 account)\n - Joe (2 accounts)")
}

it should "get First Customer Name" in {
val bank: Bank = new Bank
bank.getFirstCustomer should be("None")
val checkingAccount: Account = new Account(Account.CHECKING)
val john: Customer = new Customer("John").openAccount(checkingAccount)
bank.addCustomer(john)
bank.getFirstCustomer should be ("John")
}

it should "checking account" in {
val bank: Bank = new Bank
Expand All @@ -22,18 +42,18 @@ class BankTest extends FlatSpec with Matchers {

it should "savings account" in {
val bank: Bank = new Bank
val checkingAccount: Account = new Account(Account.SAVINGS)
bank.addCustomer(new Customer("Bill").openAccount(checkingAccount))
checkingAccount.deposit(1500.0)
val savingsAccount: Account = new Account(Account.SAVINGS)
bank.addCustomer(new Customer("Bill").openAccount(savingsAccount))
savingsAccount.deposit(1500.0)
bank.totalInterestPaid should be(2.0)
}

it should "maxi savings account" in {
val bank: Bank = new Bank
val checkingAccount: Account = new Account(Account.MAXI_SAVINGS)
bank.addCustomer(new Customer("Bill").openAccount(checkingAccount))
checkingAccount.deposit(3000.0)
bank.totalInterestPaid should be(170.0)
val maxiSavingsAccount: Account = new Account(Account.MAXI_SAVINGS)
bank.addCustomer(new Customer("Bill").openAccount(maxiSavingsAccount))
maxiSavingsAccount.deposit(3000.0)
bank.totalInterestPaid should be(3.0)
}

}
11 changes: 11 additions & 0 deletions src/test/scala/com/abc/CustomerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@ class CustomerTest extends FlatSpec with Matchers {
oscar.openAccount(new Account(Account.CHECKING))
oscar.numberOfAccounts should be(3)
}

it should "testTransferBetweenAccounts" in {
val checkingAccount: Account = new Account(Account.CHECKING)
val savingsAccount: Account = new Account(Account.SAVINGS)
val John: Customer = new Customer("John").openAccount(checkingAccount).openAccount(savingsAccount)
checkingAccount.deposit(500.0)
savingsAccount.deposit(100.0)
John.transferBetweenTheAccounts(checkingAccount, savingsAccount, 100.0)
checkingAccount.sumTransactions() should be (400.0)
savingsAccount.sumTransactions() should be (200.0)
}
}