class MoneyTrackerMenu:
    def __init__(self):
        os.system('clear')
        self._user_name = input('Enter user name:')
        self.initialize_file_and_money_tracker()
        self.initialize_options()
        self.date_message = '(Format of the date is DD,MM,YYYY)\nEnter date:'

    def initialize_file_and_money_tracker(self):
        information_entered = False
        while information_entered is False:
            try:
                self._file_with_data = input('Enter file to load:')
                self._money_tracker = MoneyTracker(self._file_with_data)
                information_entered = True
                os.system('clear')
            except Exception:
                print('The file name is incorect or its content is invalid!')

    def initialize_options(self):
        self.options = ['1', '2', '3', '4', '5', '6', '7']
        self.options_functions = [
            self.option_one, self.option_two, self.option_three,
            self.option_four, self.option_five, self.option_six,
            self.option_seven
        ]

    def start(self):
        self.working = True
        option_chosen = ''
        while self.working is True:
            self.print_menu_options()
            option_chosen = input()
            if option_chosen not in self.options:
                print('Invalid option, try again!')
            else:
                self.options_functions[int(option_chosen) - 1]()
            if self.working is True:
                input('Press enter to continue:')
            os.system('clear')

    def print_menu_options(self):
        print(f'Hello, {self._user_name}!\n'
              'Choose one of the following options to continue:\n'
              '1 - show all data\n'
              '2 - show data for specific date\n'
              '3 - show expenses, ordered by categories\n'
              '4 - add new income\n'
              '5 - add new expense\n'
              '6 - exit without saving\n'
              '7 - exit and save\n')

    def option_one(self):
        print(self._money_tracker.list_user_data())

    def option_two(self):
        try:
            day, month, year = self.date_parser(input(self.date_message))
            print(self._money_tracker.show_user_data_per_date(
                day, month, year))
        except ValueError as ex:
            print(f'\nError!\n{ex}\n')

    def option_three(self):
        print(self._money_tracker.list_user_exepences_ordered_by_categories())

    def option_four(self):
        try:
            day, month, year = self.date_parser(input(self.date_message))
            category = input('Category of income:')
            amount = self.amount_parser(input('Amount of income:'))
            self._money_tracker.add_income(day, month, year, category, amount)
        except ValueError as ex:
            print(f'\nError!\n{ex}\n')

    def option_five(self):
        try:
            day, month, year = self.date_parser(input(self.date_message))
            category = input('Category of expence:')
            amount = self.amount_parser(input('Amount of expense:'))
            self._money_tracker.add_expence(day, month, year, category, amount)
        except ValueError as ex:
            print(f'\nError!\n{ex}\n')

    def option_six(self):
        self.working = False

    def option_seven(self):
        self.working = False
        self.save_data_in_a_file()

    def save_data_in_a_file(self):
        with open(self._file_with_data, 'w') as f:
            f.write(self._money_tracker.list_user_data())

    @staticmethod
    def date_parser(date):
        if re.match(r'\d{1,2},\d{1,2},\d{4}', date) is None:
            raise ValueError('The date is invalid!')
        parts = date.split(',')
        return int(parts[0]), int(parts[1]), int(parts[2])

    @staticmethod
    def amount_parser(amount):
        try:
            checked_amount = float(amount)
            return checked_amount
        except Exception:
            raise ValueError('The amount should be a number!')
Exemple #2
0
class TestsMoneyTracker(unittest.TestCase):
    def setUp(self):
        agr_money_tracker = AgregatedMoneyTracker()
        agr_money_tracker._data = {
            '2018,11,12': {
                'income': [],
                'expence': [Expense('food', 5)]
            },
            '2018,11,15': {
                'income': [Income('salary', 50)],
                'expence': [Expense('shoes', 100)]
            },
            '2018,11,17': {
                'income': [Income('clothes', 20)],
                'expence': []
            }
        }
        self.money_tracker = MoneyTracker()
        self.money_tracker._data_base = agr_money_tracker

    def test_list_user_data(self):
        expected_result = ('=== 12-11-2018 ===\n'
                           '5, food, New Expense\n'
                           '=== 15-11-2018 ===\n'
                           '50, salary, New Income\n'
                           '100, shoes, New Expense\n'
                           '=== 17-11-2018 ===\n'
                           '20, clothes, New Income\n')
        self.assertEqual(self.money_tracker.list_user_data(), expected_result)

    def test_get_string_with_empty_data(self):
        self.assertEqual(MoneyTracker.get_string([]), '')

    def test_get_string_with_data(self):
        self.assertEqual(MoneyTracker.get_string([1, 2, 3]), '123')
        self.assertEqual(
            MoneyTracker.get_string(
                [Income('salary', 50),
                 Expense('shoes', 100)]), ('50, salary, New Income\n'
                                           '100, shoes, New Expense\n'))

    def test_show_user_data_per_date_no_data_for_the_date(self):
        self.assertEqual(
            self.money_tracker.show_user_data_per_date(5, 5, 2010), '')

    def test_show_user_data_per_date(self):
        self.assertEqual(
            self.money_tracker.show_user_data_per_date(15, 11, 2018),
            '50, salary, New Income\n'
            '100, shoes, New Expense\n')

    def test_get_user_income_no_income(self):
        self.assertEqual(MoneyTracker().get_user_incomes(), [])

    def test_get_user_income(self):
        self.assertEqual(
            self.money_tracker.get_user_incomes(),
            [Income('salary', 50), Income('clothes', 20)])

    def test_get_user_expences_no_income(self):
        self.assertEqual(MoneyTracker().get_user_expences(), [])

    def test_get_user_expences(self):
        self.assertEqual(
            self.money_tracker.get_user_expences(),
            [Expense('food', 5), Expense('shoes', 100)])

    def test_show_user_income(self):
        self.assertEqual(
            self.money_tracker.show_user_incomes(), '50, salary, New Income\n'
            '20, clothes, New Income\n')

    def test_show_user_expences(self):
        self.assertEqual(self.money_tracker.show_user_expences(),
                         '5, food, New Expense\n'
                         '100, shoes, New Expense\n')

    def test_list_income_categories_with_no_categories(self):
        self.assertEqual(MoneyTracker().list_income_categories(), '')

    def test_list_income_categories(self):
        self.assertEqual(self.money_tracker.list_income_categories(),
                         'salary\n'
                         'clothes\n')

    def test_list_expence_categories_with_no_categories(self):
        self.assertEqual(MoneyTracker().list_expence_categories(), '')

    def test_list_expece_categories(self):
        self.assertEqual(self.money_tracker.list_expence_categories(), 'food\n'
                         'shoes\n')

    def test_list_user_exepences_ordered_by_categories(self):
        self.money_tracker._data_base.set_record_for_date(
            '2018,11,17', '10, book, '
            'New Expense\n')
        exp = self.money_tracker.list_user_exepences_ordered_by_categories()
        self.assertEqual(
            exp, '10, book, New Expense\n'
            '5, food, New Expense\n'
            '100, shoes, New Expense\n')