Transactions with factor
JavaScript
class Transactions {
constructor(factor = 1) {
this.factor = factor
this._transactions = []
}
add(transaction) {
this._transactions.push(transaction)
}
get total() {
const euro = this._transactions.reduce((total, current) => total += current, 0)
return Transaction.convert(euro, this.factor)
}
get transactions() {
return this._transactions.map(transaction => transaction.convert(this.factor))
}
get maxTransaction() {
return (this._transactions.slice().sort((a, b) => a.amount < b.amount)[0]).convert(this.factor)
}
}
class Transaction {
constructor(name, amount) {
this.name = name
this.amount = amount
}
valueOf() {
return this.amount
}
convert(factor) {
return {
name: this.name,
euro: this.amount,
pln: Transaction.convert(this.amount, factor).pln
}
}
static convert(amount, factor) {
return {
euro: amount,
pln: (amount * factor).toFixed(2)
}
}
}
t = new Transactions
t.add(new Transaction('Foo', 10))
t.add(new Transaction('Baz', 30))
t.add(new Transaction('Baz', 90))
t.add(new Transaction('Bar', 20))