Advertisement
JmihPodvalbniy

Untitled

Dec 25th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | Software | 0 0
  1. ##  Итоговая работа.
  2. ##  Название: Система управления финансами
  3. ##  Реализуйте программу для учета доходов и расходов.
  4. ##  Дополнительно: добавьте возможность сохранения данных в файл.
  5.  
  6. class Transaction:
  7.     def __init__(self, date, description, amount, type):
  8.         self.date = date
  9.         self.description = description
  10.         self.amount = amount
  11.         self.type = type
  12.  
  13.     def __str__(self):
  14.         return f"Transaction: {self.date} - {self.description}: {self.amount} {'' if self.type == 'income' else 'out'}"
  15.  
  16. class FinanceManager:
  17.     def __init__(self):
  18.         self.transactions = []
  19.  
  20.     def add_transaction(self, date, description, amount, transaction_type):
  21.         transaction = Transaction(date, description, amount, transaction_type)
  22.         self.transactions.append(transaction)
  23.  
  24.     def display_transactions(self):
  25.         for transaction in self.transactions:
  26.             print(transaction)
  27.  
  28.     def save_transactions(self, filename):
  29.         with open(filename, 'w') as file:
  30.             for transaction in self.transactions:
  31.                 file.write(f"{transaction.date}: {transaction.description}: {transaction.amount} {'' if transaction.type == 'income' else 'out'}\n")
  32.  
  33. # Пример использования
  34. finance_manager = FinanceManager()
  35.  
  36. # Добавление транзакций
  37. finance_manager.add_transaction('2023-12-01', 'Salary', 5000, 'income')
  38. finance_manager.add_transaction('2023-12-02', 'Rent', 1500, 'out')
  39. finance_manager.add_transaction('2023-12-03', 'Groceries', 800, 'out')
  40. finance_manager.add_transaction('2023-12-04', 'Utility Bill', 1200, 'out')
  41.  
  42. # Просмотр транзакций
  43. finance_manager.display_transactions()
  44.  
  45. # Сохранение транзакций в файл
  46. finance_manager.save_transactions('transactions.txt')
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement