File size: 2,288 Bytes
63f6453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""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)