Skip to content

Commit

Permalink
Adding summarization code in its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Deobald committed Mar 15, 2017
1 parent 4220a9d commit 264b6e3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
44 changes: 44 additions & 0 deletions summarization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";

const addTxn = function(acc, txn){
switch(txn.type){
case "deposit":
acc.deposit += txn.amount
break

case "laundry":
acc.laundry += txn.amount
break

case "purchase":
acc.purchase += txn.amount
break
}
return acc
}

const calculateTotal = function(monies) {
return monies.deposit - (monies.laundry + monies.purchase);
}

const summarizeStudent = function(student){
let monies = student.txns.reduce(addTxn, {deposit: 0,laundry: 0,purchase: 0, total:0});
monies.total = calculateTotal(monies);
return {
name : student.name,
roomNo : student.roomNo,
seatNo : student.seatNo,
monies : monies
}

}

const summarize = function(students) {
return students.map(summarizeStudent);
}

module.exports = {
addTxn,
summarizeStudent,
summarize
}
57 changes: 57 additions & 0 deletions test/summarization_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var assert = require('assert');
var summarization = require('../summarization.js');
var models = require('../models.js');

describe('summarization', function() {
describe('addTxn', function() {

it('sums deposits', function() {
// TODO: replace manual object construction with factories or builders
var deposit20 = {type: "deposit", amount: 20};
var deposit10 = {type: "deposit", amount: 10};
var acc1 = summarization.addTxn({deposit: 0, laundry: 0, purchase: 0}, deposit20);
var acc2 = summarization.addTxn(acc1, deposit10);
assert.equal(30, acc2.deposit);
});

it('sums laundry', function() {
var laundry20 = {type: "laundry", amount: 20};
var laundry10 = {type: "laundry", amount: 10};
var acc1 = summarization.addTxn({deposit: 0, laundry: 0, purchase: 0}, laundry20);
var acc2 = summarization.addTxn(acc1, laundry10);
assert.equal(30, acc2.laundry);
});

it('sums purchase', function() {
var purchase20 = {type: "purchase", amount: 20};
var purchase10 = {type: "purchase", amount: 10};
var acc1 = summarization.addTxn({deposit: 0, laundry: 0, purchase: 0}, purchase20);
var acc2 = summarization.addTxn(acc1, purchase10);
assert.equal(30, acc2.purchase);
});

it('summarizes multiple students', function() {
var john = {name: "john", roomNo: "1a", seatNo:"2a",
txns: [{type: "deposit", amount: 200},
{type: "laundry", amount: 10},
{type: "laundry", amount: 30},
{type: "laundry", amount: 20},
{type: "purchase", amount: 50},
{type: "purchase", amount: 100}]};
var mukesh = {name: "mukesh", roomNo: "2a", seatNo: "3a",
txns: [{type: "deposit", amount: 200},
{type: "laundry", amount: 10},
{type: "laundry", amount: 50},
{type: "laundry", amount: 20},
{type: "purchase", amount: 50}]};
var summary = [
{name:"john", roomNo: "1a", seatNo: "2a",
monies: {deposit: 200, laundry: 60, purchase: 150, total: -10}},
{name:"mukesh", roomNo: "2a", seatNo: "3a",
monies: {deposit: 200, laundry: 80, purchase: 50, total: 70}}
];
assert.deepEqual(summary, summarization.summarize([john, mukesh]));
});

});
});

0 comments on commit 264b6e3

Please sign in to comment.