2020-03-27 20:14:22 +04:00
|
|
|
"""PiggyBank implementation."""
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
from os.path import exists
|
|
|
|
from typing import List
|
|
|
|
|
2020-07-07 02:30:21 +04:00
|
|
|
from piggybank import PIGGYBANK_FILE_EXTENSION, CURRENCIES
|
|
|
|
from piggybank.currency import CurrencyIsNotSupportedError
|
2020-06-04 03:12:31 +04:00
|
|
|
from piggybank.transaction import Transaction, sum_transactions, TYPE_INCOME
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
__all__ = ["PiggyBank"]
|
|
|
|
|
|
|
|
|
|
|
|
class PiggyBank:
|
2020-03-27 20:14:22 +04:00
|
|
|
"""This class stores array of transactions and perform some actions on it."""
|
2020-06-04 03:12:31 +04:00
|
|
|
def __init__(self, currency: str = None) -> None:
|
2020-06-05 03:51:29 +04:00
|
|
|
self._currency = None
|
2020-06-04 03:12:31 +04:00
|
|
|
if not currency is None:
|
|
|
|
self.currency = currency
|
2019-12-27 12:54:58 +04:00
|
|
|
self._transactions = []
|
2020-06-04 03:12:31 +04:00
|
|
|
self._last_transaction = None
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
def transact(self, coins: List[int], direction: str = TYPE_INCOME) -> None:
|
|
|
|
"""Make a transaction."""
|
2020-06-07 04:47:15 +04:00
|
|
|
if len(coins) != len(CURRENCIES[self._currency]):
|
2020-06-04 03:12:31 +04:00
|
|
|
raise ValueError("Length of passed coins list doesn't match the " \
|
|
|
|
f"currency's coins count. ({len(coins)} " \
|
2020-06-07 04:47:15 +04:00
|
|
|
f"!= {len(CURRENCIES[self._currency])})")
|
2020-06-04 03:12:31 +04:00
|
|
|
self._last_transaction = Transaction(coins, direction)
|
|
|
|
self._transactions.append(self._last_transaction)
|
|
|
|
for coin_count in sum_transactions(self._transactions):
|
2019-12-25 23:08:20 +04:00
|
|
|
if coin_count < 0:
|
2019-12-27 12:54:58 +04:00
|
|
|
del self._transactions[-1]
|
2020-06-04 03:12:31 +04:00
|
|
|
self._last_transaction = None
|
2019-12-25 23:08:20 +04:00
|
|
|
raise ValueError(
|
|
|
|
"You can't take out more than you have.")
|
|
|
|
|
|
|
|
@property
|
2020-06-04 03:12:31 +04:00
|
|
|
def currency(self) -> str:
|
|
|
|
"""Returns a currency of a PiggyBank."""
|
|
|
|
return self._currency
|
2019-12-25 23:08:20 +04:00
|
|
|
|
2020-06-04 03:12:31 +04:00
|
|
|
@currency.setter
|
2020-06-05 03:51:29 +04:00
|
|
|
def currency(self, currency: str) -> None:
|
|
|
|
"""Sets a currency of a PiggyBank with check for support."""
|
2020-06-04 03:12:31 +04:00
|
|
|
currency = currency.upper()
|
2020-06-05 03:51:29 +04:00
|
|
|
if not currency in CURRENCIES.keys():
|
2020-06-04 03:12:31 +04:00
|
|
|
raise CurrencyIsNotSupportedError
|
|
|
|
self._currency = currency
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
@property
|
2020-06-04 03:12:31 +04:00
|
|
|
def transactions(self) -> List[Transaction]:
|
|
|
|
"""Returns a list of transactions."""
|
|
|
|
return self._transactions
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_file(filename: str) -> PiggyBank:
|
2020-06-04 03:12:31 +04:00
|
|
|
"""Returns a PiggyBank object loaded from a file."""
|
2019-12-25 23:08:20 +04:00
|
|
|
piggybank = PiggyBank()
|
|
|
|
piggybank.load(filename)
|
|
|
|
return piggybank
|
|
|
|
|
|
|
|
def save(self, filename: str) -> None:
|
2020-06-04 03:12:31 +04:00
|
|
|
"""Appends last successful transaction to a file. File will be created
|
|
|
|
if not exist yet."""
|
|
|
|
if self._last_transaction is None:
|
|
|
|
raise ValueError("No successful transaction found.")
|
2019-12-25 23:08:20 +04:00
|
|
|
if not filename.endswith(PIGGYBANK_FILE_EXTENSION):
|
|
|
|
filename += PIGGYBANK_FILE_EXTENSION
|
2020-06-04 03:12:31 +04:00
|
|
|
if not exists(filename):
|
|
|
|
with open(filename, 'w') as pbf:
|
|
|
|
pbf.write(f"{self.currency}\n{self._last_transaction}\n")
|
|
|
|
else:
|
|
|
|
with open(filename, 'a') as pbf:
|
|
|
|
pbf.write(f"{self._last_transaction}\n")
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
def load(self, filename: str) -> None:
|
2020-06-04 03:12:31 +04:00
|
|
|
"""Loads a PiggyBank from a file."""
|
2019-12-25 23:08:20 +04:00
|
|
|
if not filename.endswith(PIGGYBANK_FILE_EXTENSION):
|
|
|
|
filename += PIGGYBANK_FILE_EXTENSION
|
|
|
|
if not exists(filename):
|
|
|
|
raise FileNotFoundError(filename)
|
2020-06-04 03:12:31 +04:00
|
|
|
with open(filename, 'r') as pbf:
|
|
|
|
currency = pbf.readline()[:-1]
|
2019-12-25 23:08:20 +04:00
|
|
|
if currency not in CURRENCIES:
|
|
|
|
raise CurrencyIsNotSupportedError
|
|
|
|
else:
|
2020-06-04 03:12:31 +04:00
|
|
|
self._currency = currency
|
|
|
|
for line in pbf:
|
|
|
|
self._transactions.append(Transaction.from_string(line))
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
def __eq__(self, piggybank: PiggyBank) -> str:
|
2020-06-04 03:12:31 +04:00
|
|
|
"""Compares only currency."""
|
|
|
|
return self._currency == piggybank.currency
|