|
| 1 | +class Bank_account: |
| 2 | + def __init__(self, balance = 0): |
| 3 | + self.__balance = balance |
| 4 | + self.__file = "project/files/transactions.txt" |
| 5 | + self.__transactions = [] |
| 6 | + self.__load_transactions() |
| 7 | + |
| 8 | + def check_statement(self): |
| 9 | + print("===== Extrato =====") |
| 10 | + |
| 11 | + for transaction in self.__transactions: |
| 12 | + print(f"{transaction[0]}: {transaction[1]}") |
| 13 | + |
| 14 | + print("===================") |
| 15 | + print(f"Saldo (=): {self.__balance}") |
| 16 | + print("===================") |
| 17 | + |
| 18 | + def __load_transactions(self): |
| 19 | + try: |
| 20 | + with open(self.__file, "r") as file: |
| 21 | + for line in file: |
| 22 | + transaction, amount = line.strip().split(", ") |
| 23 | + amount = float(amount) |
| 24 | + |
| 25 | + self.__transactions.append((transaction, amount)) |
| 26 | + |
| 27 | + if transaction == "deposito (+)": |
| 28 | + self.__balance += amount |
| 29 | + elif transaction == "saque (-)": |
| 30 | + self.__balance -= amount |
| 31 | + except: |
| 32 | + print("Algo deu errado em abrir o arquivo!") |
| 33 | + pass |
| 34 | + |
| 35 | + def deposit(self, amount): |
| 36 | + self.__balance += amount |
| 37 | + |
| 38 | + try: |
| 39 | + with open(self.__file, "a") as file: |
| 40 | + file.write(f"deposito (+), {amount}\n") |
| 41 | + self.__transactions.append(("deposito (+)", amount)) |
| 42 | + except: |
| 43 | + print("Algo deu errado em abrir o arquivo!") |
| 44 | + pass |
| 45 | + |
| 46 | + print(f"Depósito de R${amount} realizado!") |
| 47 | + |
| 48 | + def withdraw(self, amount): |
| 49 | + if amount == 0: |
| 50 | + return print("Saque deve ser maior que zero!") |
| 51 | + |
| 52 | + if amount <= self.__balance: |
| 53 | + self.__balance -= amount |
| 54 | + |
| 55 | + try: |
| 56 | + with open(self.__file, "a") as file: |
| 57 | + file.write(f"saque (-), {amount}\n") |
| 58 | + self.__transactions.append(("saque (-)", amount)) |
| 59 | + except: |
| 60 | + print("Algo deu errado em abrir o arquivo!") |
| 61 | + pass |
| 62 | + |
| 63 | + print(f"Saque de R${amount} realizado!") |
| 64 | + else: |
| 65 | + print("Saldo insuficiente!") |
| 66 | + |
| 67 | +account = Bank_account() |
| 68 | +waiting_menu = False # flag |
| 69 | +while True: |
| 70 | + if waiting_menu == True: |
| 71 | + input("\nPressine Enter para seguir...") |
| 72 | + |
| 73 | + # print("\033[H\033[J") # terminal clear |
| 74 | + # print("\033c", end="") # terminal clear |
| 75 | + waiting_menu = True |
| 76 | + |
| 77 | + print(''' |
| 78 | +=== Automated Teller Machine === |
| 79 | + [1] Ver extrato |
| 80 | + [2] Fazer o depósito |
| 81 | + [3] Fazer saque |
| 82 | + [4] Sair |
| 83 | +================================ |
| 84 | +''') |
| 85 | + |
| 86 | + option = input("\bEscolha uma opção: ") |
| 87 | + print("") |
| 88 | + |
| 89 | + if option == "1": |
| 90 | + account.check_statement() |
| 91 | + elif option == "2": |
| 92 | + amount = float(input("Digite o valor para depositar: ")) |
| 93 | + account.deposit(amount) |
| 94 | + elif option == "3": |
| 95 | + amount = float(input("Digite o valor para saque: ")) |
| 96 | + account.withdraw(amount) |
| 97 | + elif option == "4": |
| 98 | + print("Programa encerrado!\n") |
| 99 | + break |
| 100 | + else: |
| 101 | + print("Opção inválida!") |
0 commit comments