From 4c3a8454b86b7908593a1caf359d186197101ff1 Mon Sep 17 00:00:00 2001 From: Dhruv Tapasvi Date: Thu, 28 Jul 2016 11:19:43 +0100 Subject: [PATCH 1/3] Dodgy edit --- SoftBankNew.ts | 123 +++++++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 54 deletions(-) diff --git a/SoftBankNew.ts b/SoftBankNew.ts index 71c958d..c860319 100644 --- a/SoftBankNew.ts +++ b/SoftBankNew.ts @@ -1,3 +1,9 @@ +// Logging preamble +var winston = require('winston'); +winston.level = 'debug'; +winston.add(winston.transports.File, {filename: 'SoftBankNew2Problems.log' }); +winston.remove(winston.transports.Console); + interface Account { // Interface for each person's account net :number; transactions :Transaction[]; @@ -10,12 +16,32 @@ interface Transaction { // Interface for all the transactions of each person. narrative :string; } +function printPersonsTransactions(person :string) {// Prints all the transactions for a given person + if (peopleAccounts.hasOwnProperty(person)) { + console.log(person + ':'); + var bal = peopleAccounts[person].net; + console.log('Your balance is: ' + bal + '.'); + var pT = peopleAccounts[person].transactions; + var pTlen = pT.length; + for (var i=0; i { - if err throw err; +var text = fs.readFileSync('DodgyTransactions2015.csv','utf8',(err,data) => { + if (err) throw err; return data; }) var lines = text.split('\r\n'); @@ -24,76 +50,65 @@ var lines = text.split('\r\n'); var len = lines.length; for (var i=1; i { - switch(command) { - case 'List_All': - printPeopleBalances(); - console.log("Printed"); - break; - case 'List_Person': - rl.question('Whose account do you wish to see?', (name) => { - printPersonsTransactions(name); - }); - break; - default: - } - prompting(); - }); +var readlineSync = require('readline-sync'); +var command = ''; +var personName = ''; +while (command != 'Quit') { + command = readlineSync.question("Enter a command. 'List_All' to show everyone's balances. 'List_Person' to list the transactions of a person, chosen later through a menu. 'Quit' to quit: "); + switch (command) { + case 'List_All': + printPeopleBalances(); + console.log(); + break; + case 'List_Person': + personName = readlineSync.question('Whose Account do you wish to see: '); + printPersonsTransactions(personName); + console.log(); + break; + case 'Quit': + console.log('Quit successful'); + break; + default: + console.log('Sorry, invalid input. Please try again.'); + } } - -prompting(); \ No newline at end of file From 18dc04ddb7210ee1242f033bdbe65cddf9487ddd Mon Sep 17 00:00:00 2001 From: Dhruv Tapasvi Date: Thu, 28 Jul 2016 12:35:38 +0100 Subject: [PATCH 2/3] Markups made --- SoftBankNew.ts | 181 ++++++++++++++++++++++++++----------------------- 1 file changed, 96 insertions(+), 85 deletions(-) diff --git a/SoftBankNew.ts b/SoftBankNew.ts index c860319..5647b0c 100644 --- a/SoftBankNew.ts +++ b/SoftBankNew.ts @@ -1,114 +1,125 @@ -// Logging preamble +var readlineSync = require('readline-sync') var winston = require('winston'); + +// LOGGING: + winston.level = 'debug'; winston.add(winston.transports.File, {filename: 'SoftBankNew2Problems.log' }); winston.remove(winston.transports.Console); -interface Account { // Interface for each person's account - net :number; +// ITNERFACES: + +// Interface for each person's account +interface Account { + balance :number; transactions :Transaction[]; } -interface Transaction { // Interface for all the transactions of each person. - credit :boolean; - amount :number +// Interface for all the transactions of each person. +interface Transaction { + from :string; //Other person involved in the transaction + to: string; + amount :number; date :string; narrative :string; } -function printPersonsTransactions(person :string) {// Prints all the transactions for a given person - if (peopleAccounts.hasOwnProperty(person)) { - console.log(person + ':'); - var bal = peopleAccounts[person].net; - console.log('Your balance is: ' + bal + '.'); - var pT = peopleAccounts[person].transactions; - var pTlen = pT.length; - for (var i=0; i { - if (err) throw err; - return data; -}) -var lines = text.split('\r\n'); +// Inputs the file and parse it into an array of lines: +function inputParse() :string[] { + var fs = require('fs'); + var text = fs.readFileSync('DodgyTransactions2015.csv','utf8',(err,data) => { + if (err) throw err; + return data; + }); + return text.split('\r\n'); +}; -// Process each line to produce all the accounts and update their transactions accordingly -var len = lines.length; -for (var i=1; i Date: Thu, 28 Jul 2016 14:01:20 +0100 Subject: [PATCH 3/3] now accepts json --- SoftBankNew.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/SoftBankNew.ts b/SoftBankNew.ts index 5647b0c..8320767 100644 --- a/SoftBankNew.ts +++ b/SoftBankNew.ts @@ -1,5 +1,6 @@ var readlineSync = require('readline-sync') var winston = require('winston'); +var lines = require('./Transactions2013.json'); // LOGGING: @@ -26,15 +27,7 @@ interface Transaction { // METHODS: -// Inputs the file and parse it into an array of lines: -function inputParse() :string[] { - var fs = require('fs'); - var text = fs.readFileSync('DodgyTransactions2015.csv','utf8',(err,data) => { - if (err) throw err; - return data; - }); - return text.split('\r\n'); -}; +/\b[1-2][0-9][0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|1[1-9]|2[1-9]|30|31)T00-00-00\b/ // Parses the transactionLine and alter both the creditor and the debtor function processTransaction(transactionLine: string) :void { @@ -46,7 +39,7 @@ function processTransaction(transactionLine: string) :void { var amt = parseFloat(details[4]); if (isNaN(amt)) { winston.log('error', 'You have entered an invalid trasaction, becuase you have entered ' + details[4] + ' as the amount, which is not valid. This transaction will not be recorded.'); - } else if (!/\b(0[1-9]|1[1-9]|2[1-9]|30|31)\/(0[1-9]|1[0-2])\/[1-2][0-9][0-9][0-9]\b/.test(dateString)) { + } else if (!/\b[1-2][0-9][0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|1[1-9]|2[1-9]|30|31)T00:00:00\b/.test(dateString)) { winston.log('error', 'You have entered an invalid trasaction, becuase you have entered ' + dateString + ' as the date, which is not a valid format. This transaction will not be recorded. For future reference, please enter the date in a dd/mm/yyyy format'); } else { var thisTransaction = { from: fromPerson, to: toPerson, amount: amt, date: dateString, narrative: narr}; @@ -112,9 +105,9 @@ function commandLineInstructions() :void { // Runs the program function run() :void { - var lines = inputParse(); - for (var i=1; i