Created
February 3, 2020 16:51
-
-
Save M4R14/1815bf3c226acfa5bfd1a1e4fee9ba84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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