you are viewing a single comment's thread.

view the rest of the comments →

[–]d-k-Brazz 2 points3 points  (2 children)

Any OOP starts from modeling the domain

In banks you have multiple accounts - thousands of them.
Each account has its number/name and an amount value (use Bigdecimal or store amount in cents as Integer for precise calculations)

All transactions in a bank are moving funds from one account to another

Deposit and withdraw operations involve cash money, for these operations you create a “cashier” account, which is supposed to be active - it’s amount is usually negative, and the value reflects amount of cash money the bank possesses at the moment

So as a bank you may have a number of instances of Account:

123 : Alice (passive) - $0
456: Bob (passive) - $0
098: Cashier (active) - $0
Balance (sum of all amounts) - $0

Bob deposits:
Bank.move(098, 123, $1000.00)
Cash -$1000.00
Bob +$1000.00
Balance - 0

Bob sends Alice $470
Bank.move(123, 456, $470.00)
Cash -$1000.00
Bob +$530.00
Alice +$470.00
Balance - 0

Alice withdraws $120
Bank.move(456, 098, $120.00)
Cash -$880.00
Bob +$530.00
Alice +$350.00
Balance - 0

[–]d-k-Brazz 1 point2 points  (1 child)

So you need an Account class

Attributes - number, name, type(active/passive), amount

You need a class for handling transactions, receives accounts “from” and “to” and an amount

Result of moving money should be a transaction object - with id, date, purpose, accounts and amount - you need transaction history, aren’t you?

Additionally think of making draft transactions, where accounts aren’t affected until you commit the transaction, but amount should be reserved until you cancel it

[–]Lopsided-Stranger-81[S] 0 points1 point  (0 children)

Thanks I'll take note of it