SevdanurGenc's picture
Upload 60 files
63f6453 verified
Raw History Blame Contribute Delete
2.29 kB
"""Basit banka hesabı modeli."""
class InsufficientFunds(Exception):
"""Bakiye yetersiz olduğunda fırlatılır."""
class BankAccount:
def __init__(self, owner: str, balance: float = 0.0, overdraft_limit: float = 0.0):
if not owner or not owner.strip():
raise ValueError("owner boş olamaz")
if balance < 0:
raise ValueError("başlangıç bakiyesi negatif olamaz")
if overdraft_limit < 0:
raise ValueError("ek hesap limiti negatif olamaz")
self.owner = owner.strip()
self.balance = float(balance)
self.overdraft_limit = float(overdraft_limit)
self.history: list[tuple[str, float]] = []
def deposit(self, amount: float) -> float:
if amount <= 0:
raise ValueError("yatırılan tutar pozitif olmalı")
self.balance += amount
self.history.append(("deposit", amount))
return self.balance
def withdraw(self, amount: float) -> float:
if amount <= 0:
raise ValueError("çekilen tutar pozitif olmalı")
if self.balance - amount < -self.overdraft_limit:
raise InsufficientFunds(f"{self.owner}: yetersiz bakiye")
self.balance -= amount
self.history.append(("withdraw", amount))
return self.balance
def transfer(self, other: "BankAccount", amount: float) -> None:
if other is self:
raise ValueError("aynı hesaba transfer yapılamaz")
self.withdraw(amount)
other.deposit(amount)
def apply_interest(self, rate: float) -> float:
"""Pozitif bakiyeye yıllık faiz uygular; negatif bakiyeye faiz uygulanmaz."""
if not 0 <= rate <= 1:
raise ValueError("oran 0 ile 1 arasında olmalı")
if self.balance > 0:
interest = round(self.balance * rate, 2)
self.balance += interest
self.history.append(("interest", interest))
return self.balance
def statement(self) -> str:
lines = [f"Hesap sahibi: {self.owner}"]
for kind, amount in self.history:
sign = "-" if kind == "withdraw" else "+"
lines.append(f"{kind:<9}{sign}{amount:.2f}")
lines.append(f"Bakiye: {self.balance:.2f}")
return "\n".join(lines)