I have this task of modelling a system where given a group of users each can keep track of their expenses. The basic requirements are as followed:
- To be able to give credit to an user.
- Find all the transactions of a given user.
- Filter out the transaction where either user is borrower or spender.
- Create a group and put users in that group and then when a group is mentioned then distribute money equally among them.[TODO]
I have tried few things but I am not convinced at all.
const trxn = new Array();
class Transaction {
constructor(amount, from, to) {
this.timestamp = new Date();
this.amount = amount;
this.from = from;
this.to = to;
trxn.push(this);
}
toString() {
let dd = this.timestamp.getDate();
let mm = this.timestamp.getMonth();
let yy = this.timestamp.getFullYear();
return `[Transaction: ${dd}/${mm}/${yy}] ${this.from} Gives ${this.amount} to ${this.to}`;
}
}
class Person {
constructor(name) {
this.name = name;
this._transactions = [];
}
transfer(amount, to) {
let t = new Transaction(amount, this, to);
this._transactions.push(t);
}
transactions() {
return this._transactions;
}
toString() {
return `${this.name.charAt(0).toUpperCase() + this.name.slice(1)}`;
}
}
function history(transactions, user) {
return transactions.filter((t) => {
return t.to.name === user.name || t.from.name === user.name;
});
}
let amy = new Person('amy');
let foo = new Person('foo');
amy.transfer(500, foo);
amy.transfer(500, foo);
foo.transfer(200, amy);
for (let t of amy.transactions()) {
console.log('Transaction by AMY');
console.log(t);
}
for (let f of foo.transactions()) {
console.log('Transaction by FOO');
console.log(f);
}
console.log('Transaction history');
console.log(history(trxn, amy));
Output of the above logic:
Transaction by AMY
[Transaction: 22/10/2016] Amy Gives 500 to Foo
Transaction by AMY
[Transaction: 22/10/2016] Amy Gives 500 to Foo
Transaction by FOO
[Transaction: 22/10/2016] Foo Gives 200 to Amy
Transaction history
[Transaction: 22/10/2016] Amy Gives 500 to Foo,
[Transaction: 22/10/2016] Amy Gives 500 to Foo,
[Transaction: 22/10/2016] Foo Gives 200 to Amy
How to approach problems like these? I have read that just think about the business logic and not about the implementation details hence in my code there is no mention of delivery mechanism and all.
How can I make my code more flexible and robust following the OOP practices?