Skip to content

Instantly share code, notes, and snippets.

@M4R14
Created February 3, 2020 16:51
Show Gist options
  • Select an option

  • Save M4R14/1815bf3c226acfa5bfd1a1e4fee9ba84 to your computer and use it in GitHub Desktop.

Select an option

Save M4R14/1815bf3c226acfa5bfd1a1e4fee9ba84 to your computer and use it in GitHub Desktop.
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
class Tarnsation {
constructor(account_id, qty) {
this.account_id = account_id;
this.qty = qty;
this.timestame = new Date();
}
}
class Account {
constructor(transactions) {
this.id = makeid(20);
this.balance = 0;
transactions.push(new Tarnsation(this.id, 10000));
this.updateBalance();
}
send(account, qty) {
this.updateBalance();
if (qty > this.balance) {
console.log('ยอดโอนสูงกว่าทีมมีอยู่');
return false;
}
transactions.push(new Tarnsation(this.id, qty *-1));
this.updateBalance();
transactions.push(new Tarnsation(account.id, qty));
account.updateBalance();
}
updateBalance () {
let value = 0;
transactions
.filter((tarnsation) => tarnsation.account_id == this.id)
.forEach((tarnsation) => { value += tarnsation.qty; });
this.balance = value;
}
}
const transactions = [];
const mark = new Account(transactions);
console.log(mark);
const mark2 = new Account(transactions);
console.log(mark2);
mark.send(mark2, 1000000)
mark.send(mark2, 500)
console.log(transactions);
console.log(mark);
console.log(mark2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment