From 79f6a9acd8597deaf9d64890f78b61355d922911 Mon Sep 17 00:00:00 2001 From: Simon El Nahas Christensen Date: Sun, 21 Jul 2019 00:48:53 +0200 Subject: [PATCH 1/2] problem 2 solution proposal --- problems/03.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/problems/03.md b/problems/03.md index f5d385a..fb5a5a2 100644 --- a/problems/03.md +++ b/problems/03.md @@ -10,10 +10,91 @@ with a `withdraw` command. ```js // bank.js +var jsonStream = require('duplex-json-stream') +var net = require('net') + +var log = [ + + ] + + + +function sum() { + return log.reduce( + function (accu, x) { + switch (x.cmd) { + case 'deposit': + return accu + x.amount + + case 'withdraw': + return accu - x.amount + default: + return accu + } + } + , 0) +} + + +var server = net.createServer(function (socket) { + socket = jsonStream(socket) + socket.on('data', function (msg) { + console.log('Bank received:', msg) + switch (msg.cmd){ + case 'balance': + socket.write({ cmd: 'balance', balance: sum() }) + break + case 'deposit': + log.push({cmd: 'deposit', amount: parseInt(msg.amount)}) + socket.write({ cmd: 'balance', balance: sum() }) + break + case 'withdraw': + log.push({cmd: 'withdraw', amount: parseInt(msg.amount)}) + socket.write({ cmd: 'balance', balance: sum() }) + break + default: + break + } + + }) +}) + +server.listen(3876) + +console.log("started bank") + ``` ```js // teller.js +var jsonStream = require('duplex-json-stream') +var net = require('net') + +var client = jsonStream(net.connect(3876)) + +client.on('data', function (msg) { + console.log('Teller received:', msg) +}) + +const args = process.argv.slice(2) +const command = args[0] +const amount = parseInt(args[1]) + +switch (command){ + case 'balance': + client.end({cmd: 'balance'}) + break + case 'deposit': + client.end({cmd: 'deposit', amount: amount}) + break + case 'withdraw': + client.end({cmd: 'withdraw'}) + break + default: + break +} + +console.log("started teller") ``` From aba5cc64f21c8dee4d2aa2c4bb8c67830aad5410 Mon Sep 17 00:00:00 2001 From: Simon El Nahas Christensen Date: Sun, 21 Jul 2019 01:15:52 +0200 Subject: [PATCH 2/2] Update 03.md --- problems/03.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/problems/03.md b/problems/03.md index fb5a5a2..2f015b8 100644 --- a/problems/03.md +++ b/problems/03.md @@ -35,22 +35,27 @@ function sum() { , 0) } - var server = net.createServer(function (socket) { socket = jsonStream(socket) socket.on('data', function (msg) { console.log('Bank received:', msg) + let amount = Math.abs(parseInt(msg.amount)) switch (msg.cmd){ case 'balance': socket.write({ cmd: 'balance', balance: sum() }) break case 'deposit': - log.push({cmd: 'deposit', amount: parseInt(msg.amount)}) + log.push({cmd: 'deposit', amount: amount}) socket.write({ cmd: 'balance', balance: sum() }) break case 'withdraw': - log.push({cmd: 'withdraw', amount: parseInt(msg.amount)}) - socket.write({ cmd: 'balance', balance: sum() }) + if(amount<=sum()){ + log.push({cmd: 'withdraw', amount: amount}) + socket.write({ cmd: 'balance', balance: sum() }) + + } else { + socket.write("insufficient funds") + } break default: break @@ -78,7 +83,7 @@ client.on('data', function (msg) { const args = process.argv.slice(2) const command = args[0] -const amount = parseInt(args[1]) +const amount = Math.abs(parseInt(args[1])) switch (command){ case 'balance': @@ -88,7 +93,7 @@ switch (command){ client.end({cmd: 'deposit', amount: amount}) break case 'withdraw': - client.end({cmd: 'withdraw'}) + client.end({cmd: 'withdraw', amount: amount}) break default: break