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

Json #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
155 changes: 87 additions & 68 deletions SoftBankNew.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,118 @@
interface Account { // Interface for each person's account
net :number;
var readlineSync = require('readline-sync')
var winston = require('winston');
var lines = require('./Transactions2013.json');

// LOGGING:

winston.level = 'debug';
winston.add(winston.transports.File, {filename: 'SoftBankNew2Problems.log' });
winston.remove(winston.transports.Console);

// 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;
}

var peopleAccounts = {} // Dictionary of people, with their names as the keys to access their accounts
// METHODS:

// Input the file and parse it into an array of lines:
var fs = require('fs');
var text = fs.readFileSync('Transactions2014.csv','utf8',(err,data) => {
if err throw err;
return data;
})
var lines = 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<len-1; i++) { // From second line to second last. There is a header line first, and an empty line last.
var details = lines[i].split(',');
/\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 {
var details = transactionLine.split(',');
var dateString = details[0]
var fromPerson = details[1];
if (!peopleAccounts.hasOwnProperty(fromPerson)) {
peopleAccounts[fromPerson] = {net: 0.0, transactions: []};
var toPerson = details[2];
var narr = details[3];
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[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};
alterAccount(toPerson,amt,thisTransaction); // Add credit to 'toPerson'
alterAccount(fromPerson,-1*amt,thisTransaction); // Remove credit from 'fromPerson'
}
peopleAccounts[fromPerson].net -= parseFloat(details[4]);
peopleAccounts[fromPerson].transactions.push({
credit: false, amount: parseFloat(details[4]), date: details[0], narrative: details[3]
})
};

var to = details[2];
if (!peopleAccounts.hasOwnProperty(to)) {
peopleAccounts[to] = {net: 0.0, transactions: []};
// Function updates an account with the given transaction details
function alterAccount(accountPerson :string, amt :number, trans :Transaction) :void {
if(!accounts.hasOwnProperty(accountPerson)) {
accounts[accountPerson] = {balance: 0.0, transactions: []};
}
peopleAccounts[to].net += parseFloat(details[4]);
peopleAccounts[to].transactions.push({
credit: true, amount: parseFloat(details[4]), date: details[0], narrative: details[3]
})
}
accounts[accountPerson].balance += amt;
accounts[accountPerson].transactions.push(trans);
};

function printPeopleBalances { // Prints everyone's account balances
for (var person in peopleAccounts) {
console.log(person + ': ' + peopleAccounts[person].net);
// Prints everyone's account balances
function printPeopleBalances() :void {
for (var person in accounts) {
console.log(person + ': ' + accounts[person].balance);
}
}
};

function printPersonsTransactions(person :string) {// Prints all the transactions for a given person
if (peopleAccounts.hasOwnProperty(person)) {
// Prints all the transactions for a given person
function printPersonsTransactions(person :string) :void {
if (accounts.hasOwnProperty(person)) {
console.log(person + ':');
var pT = peopleAccounts[person].transactions;
var pTlen = pT.length;
for (var i=0; i<pTlen; i++) {
console.log('Balance is: ' + accounts[person].balance + '.');
var pT = accounts[person].transactions;
for (var i=0; i<pT.length; i++) {
var transaction = pT[i];
if (pT[i].credit) {
console.log('Credit of ' + transaction.amount + ', dated ' + transaction.date + ', for reason: ' + transaction.narrative + '.');
} else {
console.log('Debit of ' + transaction.amount + ', dated ' + transaction.date + ', for reason: ' + transaction.narrative + '.');
}
console.log('From ' + transaction.from + ' to ' + transaction.to + ', dated ' + transaction.date + ', totalling ' + transaction.amount + ' for reason: ' + transaction.narrative + '.');
}
} else {
console.log('Person not found.');
}
}
console.log(peopleAccounts['Dan W']);

const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
};

// Ask the user for command line instructions
function prompting() {
console.log("Prompting");
var mString = "Enter a command. 'List_All' to show everyone's balances. 'List_Person' to list the transactions of a person, chosen later through a menu.";
rl.question(mString, (command) => {
switch(command) {
// Offers command line input for the program
function commandLineInstructions() :void {
while (command != 'Quit') {
var 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: ");
console.log('You command was: ' + command);
switch (command) {
case 'List_All':
printPeopleBalances();
console.log("Printed");
console.log('Done!');
break;
case 'List_Person':
rl.question('Whose account do you wish to see?', (name) => {
printPersonsTransactions(name);
});
var personName = readlineSync.question('Whose Account do you wish to see: ');
printPersonsTransactions(personName);
console.log('Done!');
break;
case 'Quit':
console.log('Quit successful');
break;
default:
console.log('Sorry, invalid input. Please try again.');
}
prompting();
});
}
}
};

// Runs the program
function run() :void {
for (var i=1; i<lines.length; i++) {
var t = lines[i];
processTransaction([t.Date,t.FromAccount,t.ToAccount,t.Narrative,t.Amount].join(','));
};
commandLineInstructions();
};

// SCRIPT:

prompting();
var accounts = {}; // Dictionary of people, with their names as the keys to access their accounts
run();