-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
126 lines (111 loc) · 3.56 KB
/
index.js
File metadata and controls
126 lines (111 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const getCSV = require('./transactionsFromCSV')
const getJSON = require('./transactionsFromJSON')
const getXML = require('./transactionsFromXML')
class Person{
constructor(name, assets) {
this.name = name;
this.assets = assets;
}
}
function isInPopulation(name, population){
// let isin = false;
// population.forEach(function(element) {
// if (element.name === name) {
// isin = true
// }
// });
// return isin
return population.some((element) => {
return element.name === name;
});
}
function indexOf(name, population){
return population.findIndex((person) => {
return person.name === name;
})
}
function getPopulationFromTransactions(transactionList) {
//
// let fromPeople = transactionList.map((transaction) => {
// return new Person(transaction.FromAccount, 0);
// });
//
// let toPeople = transactionList.map((transaction) => {
// return new Person(transaction.ToAccount, 0);
// });
//
// let allPeople = fromPeople.concat(toPeople);
// // do something to remove duplicates
//
// transactions.forEach((transaction) {
//
// })
//
//
// let fromIndex = indexOf(element.FromAccount, population);
// let toIndex = indexOf(element.ToAccount, population);
// population[fromIndex].assets -= element.Amount;
// population[toIndex].assets += element.Amount;
let population = [];
transactionList.forEach(function (element) {
if (!isInPopulation(element.FromAccount, population)) {
population.push(new Person(element.FromAccount, 0))
}
if (!isInPopulation(element.ToAccount, population)) {
population.push(new Person(element.ToAccount, 0))
}
let fromIndex = indexOf(element.FromAccount, population);
let toIndex = indexOf(element.ToAccount, population);
population[fromIndex].assets -= element.Amount;
population[toIndex].assets += element.Amount;
});
return population;
}
function listAll(file){
let transactionList = getTransactionList(file);
let population = getPopulationFromTransactions(transactionList);
console.log(population);
}
function getTransactionList(file) {
let filename = file;
let transactionList;
if (filename.slice(-3) === 'csv') {
transactionList = getCSV.getTransactionListCSV(file);
} else if (filename.slice(-4) === 'json') {
transactionList = getJSON.getTransactionListJSON(file);
} else if (filename.slice(-3) === 'xml') {
transactionList = getXML.getTransactionListXML(file)
}
return transactionList;
}
function list(name, file){
let transactionList = getTransactionList(file);
let myTransactions = transactionList.filter(function(item) {
return (item.FromAccount === name || item.ToAccount === name);
});
console.log( myTransactions )
}
function main(){
let actionRequired = userInput(`What Function would you like?
(1): LIST ALL names with their credit
(2): LIST one single account and all transactions associated with it
(3): IMPORT FILE from disk
(4): END`);
switch (actionRequired) {
case '1':
//Get year then list all;
break;
case '2':
// get year and name then do
break;
case '3':
// to implement
break;
case '4':
stop()
}
}
getTransactionList('Transactions2012.xml')
listAll('Transactions2012.xml');
list('Todd', 'Transactions2012.xml');
//main()