Скачиваний:
1
Добавлен:
03.06.2024
Размер:
2.67 Кб
Скачать
from typing import Dict

class Wallet:
    def __init__(self):
        self._wallet: Dict[str, Dict[int, float]] = {
            'RUB': {}, 'USD': {}, 'EUR': {}}
        self._exchange_rates = {
            'RUB': {'USD': 0.014, 'EUR': 0.012},
            'USD': {'RUB': 70.21, 'EUR': 0.84},
            'EUR': {'RUB': 81.09, 'USD': 1.20}
        }

    def add_money(self, currency: str, denomination: int, amount: float):
        if denomination not in self._wallet[currency]:
            self._wallet[currency][denomination] = 0
        self._wallet[currency][denomination] += amount

    def get_total(self, currency: str) -> float:
        total = 0
        for curr, denominations in self._wallet.items():
            for denom, amount in denominations.items():
                if curr == currency:
                    total += denom * amount
                else:
                    total += self.convert(curr, currency, denom, amount)
        return total

    def get_amount(self, currency: str, denomination: int) -> float:
        return self._wallet[currency].get(denomination, 0)
    
    def get_currency_total(self, currency: str):
        currency_total=0
        for curr, denominations in self._wallet.items():
            if curr == currency:
                for denom, amount in denominations.items():
                    currency_total += denom * amount
        return currency_total
        
    def convert(self, from_currency: str, to_currency: str, denom: int, amount: float) -> float:
        if from_currency == to_currency:
            return amount
        rate = self._exchange_rates[from_currency][to_currency]
        return denom * amount * rate

    def get_currency_totals(self) -> str:
        return str(self._wallet)

    def get_exchange_rates(self) -> str:
        return str(self._exchange_rates)


def tests():
    wallet = Wallet()

    wallet.add_money('RUB', 10, 50)
    wallet.add_money('RUB', 100, 20)
    wallet.add_money('USD', 1, 10)
    wallet.add_money('EUR', 5, 15)

    assert wallet.get_total('RUB') == 9283.85
    assert wallet.get_amount('USD', 2) == 0
    assert wallet.convert('EUR', 'RUB', 5, 100) == 40545
    assert wallet.convert('USD', 'EUR', 5, 100) == 420

    print ("Деньги в кошельке:",wallet.get_currency_totals())
    print ("Курсы обмена:",wallet.get_exchange_rates())
    print ("Рублей в кошельке:",wallet.get_currency_total('RUB'))

    try:
        wallet.convert('BYN', 'RUB', 1, 1)
    except KeyError:
        print("Такой валюты нет в кошельке")

tests()
Соседние файлы в папке 7