From 90bf006876f267e454eda9f15bf147ea9ab84012 Mon Sep 17 00:00:00 2001 From: Anusha Gurijala Date: Sat, 30 Apr 2016 16:34:23 -0400 Subject: [PATCH] Scala changes anusha Changes made by 'anushadevid' --- src/main/scala/com/abc/Account.scala | 69 ++++++++++++++++------- src/main/scala/com/abc/Bank.scala | 26 +++------ src/main/scala/com/abc/Customer.scala | 34 +++++------ src/main/scala/com/abc/DateProvider.scala | 13 +---- src/main/scala/com/abc/Transaction.scala | 4 +- src/test/scala/com/abc/BankTest.scala | 2 +- 6 files changed, 73 insertions(+), 75 deletions(-) diff --git a/src/main/scala/com/abc/Account.scala b/src/main/scala/com/abc/Account.scala index fa23d85..9a13c46 100644 --- a/src/main/scala/com/abc/Account.scala +++ b/src/main/scala/com/abc/Account.scala @@ -1,15 +1,58 @@ -package com.abc +package abc import scala.collection.mutable.ListBuffer +import java.util.Calendar +import java.util.Date -object Account { +trait Account { + final val CHECKING: Int = 0 final val SAVINGS: Int = 1 final val MAXI_SAVINGS: Int = 2 + var transactions: ListBuffer[Transaction] = ListBuffer.empty[Transaction] + def printStatement + def interestEarned } -class Account(val accountType: Int, var transactions: ListBuffer[Transaction] = ListBuffer()) { - +object Account { + + + private class Checking extends Account { + override def printStatement = "Checkings Account \n" + + def interestEarned = sumTransactions() * 0.001 + } + + private class Savings extends Account { + override def printStatement = "Savings Account \n" + + def interestEarned = { + val amount: Double = sumTransactions() + if (amount <= 1000) amount * 0.001 + else 1 + (amount - 1000) * 0.002 + } + } + + private class MaxiSavings extends Account { + override def printStatement = "Maxi_Savings Account \n" + + def interestEarned = { + val amount: Double = sumTransactions() + if (amount <= 1000) amount * 0.02 + else if (amount >= 1000 && amount <= 2000) 20 + (amount - 1000) * 0.05 + 70 + (amount - 2000) * 0.1 + } + } + + def apply(accountType: Int): Account = { + accountType match { + case CHECKING => new Checking + case SAVINGS => new Savings + case MAXI_SAVINGS => new MaxiSavings + case _ => throw new IllegalArgumentException("Not Supported") + } + } + def deposit(amount: Double) { if (amount <= 0) throw new IllegalArgumentException("amount must be greater than zero") @@ -23,22 +66,6 @@ class Account(val accountType: Int, var transactions: ListBuffer[Transaction] = else transactions += Transaction(-amount) } - - def interestEarned: Double = { - val amount: Double = sumTransactions() - accountType match { - case Account.SAVINGS => - 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 - case _ => - amount * 0.001 - } - } - + def sumTransactions(checkAllTransactions: Boolean = true): Double = transactions.map(_.amount).sum - } \ No newline at end of file diff --git a/src/main/scala/com/abc/Bank.scala b/src/main/scala/com/abc/Bank.scala index 4667c0e..6a46a9f 100644 --- a/src/main/scala/com/abc/Bank.scala +++ b/src/main/scala/com/abc/Bank.scala @@ -1,18 +1,16 @@ -package com.abc +package abc -import scala.collection.mutable.ListBuffer - -class Bank { - var customers = new ListBuffer[Customer] +object Bank { + var customers = Seq.empty[Customer] def addCustomer(customer: Customer) { - customers += customer + customers +: customer } def customerSummary: String = { var summary: String = "Customer Summary" for (customer <- customers) - summary = summary + "\n - " + customer.name + " (" + format(customer.numberOfAccounts, "account") + ")" + summary = summary + "\n - " + customer.printSummary() summary } @@ -27,18 +25,8 @@ class Bank { } def getFirstCustomer: String = { - try { - customers = null + if(!customers.isEmpty) customers(0).name - } - catch { - case e: Exception => { - e.printStackTrace - return "Error" - } - } } -} - - +} \ No newline at end of file diff --git a/src/main/scala/com/abc/Customer.scala b/src/main/scala/com/abc/Customer.scala index 6c517b0..95a58ea 100644 --- a/src/main/scala/com/abc/Customer.scala +++ b/src/main/scala/com/abc/Customer.scala @@ -1,8 +1,7 @@ -package com.abc - +package abc import scala.collection.mutable.ListBuffer -class Customer(val name: String, var accounts: ListBuffer[Account] = ListBuffer()) { +class Customer(val name: String, var accounts: ListBuffer[Account] = ListBuffer.empty[Account]) { def openAccount(account: Account): Customer = { accounts += account @@ -13,13 +12,9 @@ class Customer(val name: String, var accounts: ListBuffer[Account] = ListBuffer( def totalInterestEarned: Double = accounts.map(_.interestEarned).sum - /** - * This method gets a statement - */ + def getStatement: String = { - //JIRA-123 Change by Joe Bloggs 29/7/1988 start var statement: String = null //reset statement to null here - //JIRA-123 Change by Joe Bloggs 29/7/1988 end val totalAcrossAllAccounts = accounts.map(_.sumTransactions()).sum statement = f"Statement for $name\n" + accounts.map(statementForAccount).mkString("\n", "\n\n", "\n") + @@ -28,18 +23,16 @@ class Customer(val name: String, var accounts: ListBuffer[Account] = ListBuffer( } private def statementForAccount(a: Account): String = { - val accountType = a.accountType match { - case Account.CHECKING => - "Checking Account\n" - case Account.SAVINGS => - "Savings Account\n" - case Account.MAXI_SAVINGS => - "Maxi Savings Account\n" - } val transactionSummary = a.transactions.map(t => withdrawalOrDepositText(t) + " " + toDollars(t.amount.abs)) .mkString(" ", "\n ", "\n") val totalSummary = s"Total ${toDollars(a.transactions.map(_.amount).sum)}" - accountType + transactionSummary + totalSummary + a.printStatement + transactionSummary + totalSummary + } + + private def toDollars(number: Double): String = f"$$$number%.2f" + + private def printSummary(): String = { + name + " (" + format(numberOfAccounts, "account") + ")" } private def withdrawalOrDepositText(t: Transaction) = @@ -49,6 +42,7 @@ class Customer(val name: String, var accounts: ListBuffer[Account] = ListBuffer( case _ => "N/A" } - private def toDollars(number: Double): String = f"$$$number%.2f" -} - + private def format(number: Int, word: String): String = { + number + " " + (if (number == 1) word else word + "s") + } +} \ No newline at end of file diff --git a/src/main/scala/com/abc/DateProvider.scala b/src/main/scala/com/abc/DateProvider.scala index c43f6e1..cf287ac 100644 --- a/src/main/scala/com/abc/DateProvider.scala +++ b/src/main/scala/com/abc/DateProvider.scala @@ -4,17 +4,8 @@ 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 + def now = Calendar.getInstance.getTime + def getDays(numDays: Int) = Calendar.getInstance.add(Calendar.DAY_OF_MONTH, 1 * numDays).getTime } -class DateProvider { - def now: Date = { - return Calendar.getInstance.getTime - } -} diff --git a/src/main/scala/com/abc/Transaction.scala b/src/main/scala/com/abc/Transaction.scala index ba6cec8..059b355 100644 --- a/src/main/scala/com/abc/Transaction.scala +++ b/src/main/scala/com/abc/Transaction.scala @@ -1,6 +1,4 @@ package com.abc -case class Transaction(var amount: Double) { - val transactionDate = DateProvider.getInstance.now -} +case class Transaction(val amount: Double, val transactionDate = DateProvider.now) diff --git a/src/test/scala/com/abc/BankTest.scala b/src/test/scala/com/abc/BankTest.scala index 13b3067..24fd154 100644 --- a/src/test/scala/com/abc/BankTest.scala +++ b/src/test/scala/com/abc/BankTest.scala @@ -21,7 +21,7 @@ class BankTest extends FlatSpec with Matchers { } it should "savings account" in { - val bank: Bank = new Bank + val bank: Bank = new Bank new val checkingAccount: Account = new Account(Account.SAVINGS) bank.addCustomer(new Customer("Bill").openAccount(checkingAccount)) checkingAccount.deposit(1500.0)