fork download
  1. # Expenses
  2.  
  3.  
  4. class Move:
  5. def __init__(self, concept: str, amountInCents: int):
  6. self.__concept = concept
  7. self.__amount = amountInCents
  8. ...
  9.  
  10. @property
  11. def amountInCents(self) -> int:
  12. return self.__amount
  13. ...
  14.  
  15. @property
  16. def amount(self) -> float:
  17. return self.__amount / 100.0
  18. ...
  19.  
  20. @property
  21. def concept(self) -> float:
  22. return self.__concept
  23. ...
  24.  
  25. def __str__(self) -> str:
  26. return f"{self.amount}€ ({self.concept})"
  27. ...
  28. ...
  29.  
  30.  
  31. class Expense(Move):
  32. def __init__(self, concept: str, amountInCents: int):
  33. super().__init__(concept, -abs(amountInCents))
  34. ...
  35.  
  36. def __str__(self):
  37. return "\t" + super().__str__()
  38. ...
  39. ...
  40.  
  41.  
  42. class Income(Move):
  43. def __init__(self, concept: str, amountInCents):
  44. super().__init__(concept, amountInCents)
  45. ...
  46. ...
  47.  
  48.  
  49. class Account:
  50. def __init__(self):
  51. self.__moves = []
  52. ...
  53.  
  54. @property
  55. def expenses(self) -> list[Move]:
  56. return list(self.__moves)
  57. ...
  58.  
  59. def add(self, move: Move):
  60. self.__moves.append(move)
  61. ...
  62.  
  63. def balanceInCents(self) -> int:
  64. toret = 0
  65.  
  66. for m in self.__moves:
  67. toret += m.amountInCents
  68. ...
  69.  
  70. return toret
  71. ...
  72.  
  73. def balance(self) -> float:
  74. return self.balanceInCents() / 100.0
  75. ...
  76.  
  77. def __str__(self):
  78. return str.join( "\n", [str(x) for x in self.__moves]) \
  79. + f"\n\nBalance: {self.balance()}€"
  80. ...
  81. ...
  82.  
  83.  
  84. import unittest
  85.  
  86.  
  87. class Test(unittest.TestCase):
  88. def setUp(self):
  89. self._acc = Account()
  90. ...
  91.  
  92. def test_income(self):
  93. concept = "salary"
  94. salary = 150000
  95. i1 = Income(concept, salary)
  96. self.assertEqual(concept, i1.concept)
  97. self.assertEqual(salary, i1.amountInCents)
  98. self.assertEqual(salary / 100.0, i1.amount)
  99. self.assertEqual(f"{salary / 100.0}€ ({concept})", str(i1))
  100. ...
  101.  
  102. def test_expense(self):
  103. concept = "super"
  104. amount = 12000
  105. e1 = Expense(concept, amount)
  106. self.assertEqual(concept, e1.concept)
  107. self.assertEqual(-amount, e1.amountInCents)
  108. self.assertEqual(-amount / 100.0, e1.amount)
  109. self.assertEqual(f"\t-{amount / 100.0}€ ({concept})", str(e1))
  110. ...
  111.  
  112. def test_account(self):
  113. self._acc.add(Income("salary", 150050))
  114. self._acc.add(Expense("super", 11010))
  115. self._acc.add(Expense("rent", 60010))
  116. print(self._acc)
  117. self.assertEqual(790.3, self._acc.balance())
  118. ...
  119. ...
  120.  
  121.  
  122. if __name__ == "__main__":
  123. unittest.main()
  124. ...
  125.  
  126.  
Success #stdin #stdout #stderr 0.52s 20496KB
stdin
Standard input is empty
stdout
1500.5€ (salary)
	-110.1€ (super)
	-600.1€ (rent)

Balance: 790.3€
stderr
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK