Skip to content

Commit 4191b8f

Browse files
authored
Move python-self-type to root and rename for consistency (#399)
1 parent 7f64188 commit 4191b8f

File tree

9 files changed

+66
-0
lines changed

9 files changed

+66
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import random
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass
6+
class BankAccount:
7+
account_number: int
8+
balance: float
9+
10+
def display_balance(self) -> "BankAccount":
11+
print(f"Account Number: {self.account_number}")
12+
print(f"Balance: ${self.balance:,.2f}\n")
13+
return self
14+
15+
def deposit(self, amount: float) -> "BankAccount":
16+
self.balance += amount
17+
return self
18+
19+
def withdraw(self, amount: float) -> "BankAccount":
20+
if self.balance >= amount:
21+
self.balance -= amount
22+
else:
23+
print("Insufficient balance")
24+
return self
25+
26+
27+
@dataclass
28+
class SavingsAccount(BankAccount):
29+
interest_rate: float
30+
31+
@classmethod
32+
def from_application(
33+
cls, deposit: float = 0, interest_rate: float = 1
34+
) -> "SavingsAccount":
35+
# Generate a random seven-digit bank account number
36+
account_number = random.randint(1000000, 9999999)
37+
return cls(account_number, deposit, interest_rate)
38+
39+
def calculate_interest(self) -> float:
40+
return self.balance * self.interest_rate / 100
41+
42+
def add_interest(self) -> "SavingsAccount":
43+
self.deposit(self.calculate_interest())
44+
return self
45+
46+
47+
account = BankAccount(account_number=1534899324, balance=50)
48+
(
49+
account.display_balance()
50+
.deposit(50)
51+
.display_balance()
52+
.withdraw(30)
53+
.display_balance()
54+
)
55+
56+
savings = SavingsAccount.from_application(deposit=100, interest_rate=5)
57+
(
58+
savings.display_balance()
59+
.add_interest()
60+
.display_balance()
61+
.deposit(50)
62+
.display_balance()
63+
.withdraw(30)
64+
.add_interest()
65+
.display_balance()
66+
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)