mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 22:59:53 +00:00
Adds code for paradigms.
This commit is contained in:
parent
e8a469a5a2
commit
3de0a59955
23
oop/code/OopBankAccount.java
Normal file
23
oop/code/OopBankAccount.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
public class OopBankAccount {
|
||||||
|
private Integer number;
|
||||||
|
private Integer balance;
|
||||||
|
|
||||||
|
public OopBankAccount(Integer number, Integer balance) {
|
||||||
|
this.number = number;
|
||||||
|
this.balance = balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void deposit(Integer amount) {
|
||||||
|
this.balance += amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
void withdraw(Integer amount) {
|
||||||
|
this.balance += amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
void transfer(OopBankAccount destination, Integer amount) {
|
||||||
|
this.withdraw(amount);
|
||||||
|
destination.deposit(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
23
oop/code/procedural_transfer.py
Normal file
23
oop/code/procedural_transfer.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
accounts = []
|
||||||
|
|
||||||
|
def transfer(source: int, destination: int, amount: int) -> None:
|
||||||
|
|
||||||
|
source_account = get_account(source)
|
||||||
|
update_account(source_account, -amount)
|
||||||
|
|
||||||
|
destination_account = get_account(destination)
|
||||||
|
update_account(destination_account, amount)
|
||||||
|
|
||||||
|
def get_account(number: int) -> dict:
|
||||||
|
return list(filter(lambda account: account['number'] == number, accounts))[0]
|
||||||
|
|
||||||
|
def update_account(account: int, delta: int) -> None:
|
||||||
|
account['balance'] += delta
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
accounts.append({'number': 1, 'balance': 100})
|
||||||
|
accounts.append({'number': 2, 'balance': 200})
|
||||||
|
|
||||||
|
transfer(1, 2, 50)
|
||||||
|
print(accounts)
|
Loading…
x
Reference in New Issue
Block a user