Adds code for paradigms.

This commit is contained in:
Tanmay 2022-08-31 15:48:26 +01:00
parent e8a469a5a2
commit 3de0a59955
2 changed files with 46 additions and 0 deletions

View 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);
}
}

View 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)