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

Scala changes anusha #19

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
69 changes: 48 additions & 21 deletions src/main/scala/com/abc/Account.scala
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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

}
26 changes: 7 additions & 19 deletions src/main/scala/com/abc/Bank.scala
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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"
}
}
}

}


}
34 changes: 14 additions & 20 deletions src/main/scala/com/abc/Customer.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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") +
Expand All @@ -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) =
Expand All @@ -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")
}
}
13 changes: 2 additions & 11 deletions src/main/scala/com/abc/DateProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

4 changes: 1 addition & 3 deletions src/main/scala/com/abc/Transaction.scala
Original file line number Diff line number Diff line change
@@ -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)

2 changes: 1 addition & 1 deletion src/test/scala/com/abc/BankTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down