How to print new balance after adding amount to the initial one?

Question:

I’m learning Python and went with a simple ATM code. I’ve tested it and everything works DownStream – what I mean by this is:

  1. I have a few options when the class is initialized – Balance, Deposit, Withdraw, Exit.
  2. When I run Balance I receive the amount set.

2.1. I go with Deposit – it shows the new amount the person has in their account

2.2. When I use Withdraw I get correct amount as well

  1. Question – When I Deposit and then type Balance I’m getting the initial Balance of the user – that is expected. How can I change the code so after Depositing Money and select Balance to show me the new Balance?

Is this possible to be performed without much complicating the code?

The code:



class User:

    def __init__(self):
        self.fname = input('Enter your first name: ')
        self.lname = input('Enter your last name: ')
        self.age = input('Enter your age: ')

    def user_details(self):
        print('Details:')
        print(f"First Name: {self.fname}")
        print(f"Last Name: {self.lname}")
        print(f"User age: {self.age}")

    def deposit_money(self):
        self.deposit_amount = 100
        return self.deposit_amount
    
    def withdraw_money(self, withdraw_amount):
        self.withdraw_amount = withdraw_amount
        return self.withdraw_amount
    
class ATM:
    
    atm_balance = 10000

    def __init__(self):
        self.machine_balance = self.atm_balance
    
    def user_bank_balance(self):
        self.user_balance = 300
        print ('Your current balance is ${}'.format(self.user_balance))

    def deposit_atm(self, user):
        self.total_savings = 0
        deposit_m = float(input('How much do you want to deposit? '))
        if deposit_m > user.deposit_money():
            print('You do not have enough money to deposit')
        elif deposit_m == user.deposit_money():
            print('Amount deposited: ${}'.format(deposit_m))
        self.total_savings = self.user_balance + deposit_m
        print('Total amount in your account: ${}'.format(self.total_savings))

    def withdraw_atm(self):
        savings_left = 0
        sum_to_withdraw = float(input('How much do you want to withdraw? '))
        if self.atm_balance > sum_to_withdraw and self.user_balance > sum_to_withdraw:
            savings_left = self.total_savings - sum_to_withdraw
            print("You have withdraw {}".format(sum_to_withdraw))
            print('You balance is {}'.format(savings_left))
        elif self.atm_balance > sum_to_withdraw and self.user_balance < sum_to_withdraw:
            print('Daily limit eceeded')
        else:
            print('ATM out of service')



class ATMUsage:
    @classmethod
    def run(cls):
        
        print('Bulbank ATM')
        instructions = print(""" 
        Type 'Balance' to check your current balance,
        Type  'Deposit' to deposit amount into your account,
        Type  'Withdraw' to withdraw from your account,
        Type  'Exit' to exit from your account,
            """)
        active = True
        user1 = User()
        atm1 = ATM()
        user1.user_details()
        while active:
            selection = input("What would you like to do: 'Balance', 'Deposit', 'Withdraw', 'Exit': ") 
            if selection == 'Balance'.lower():
                atm1.user_bank_balance()
            elif selection == 'Deposit'.lower():
                atm1.deposit_atm(user1)
            elif selection == "Withdraw".lower():
                atm1.withdraw_atm()
            elif selection == 'Exit'.lower():
                print('Thanks for passing by. Have a good one!')
                break
            else:
                print('Wrong selection. Please, try again')


ATMUsage.run()

Asked By: gginchev

||

Answers:

That’s because every time you call the user_bank_balance method, you set the user_balance attribute to 300. So it wouldn’t matter what updates you did on the user_balance, whenever you call the user_bank_balance method, you’ll get 300

class ATM:

    atm_balance = 10000

    def __init__(self):
        self.machine_balance = self.atm_balance
        self.user_balance = 300

    def user_bank_balance(self):
        print ('Your current balance is ${}'.format(self.user_balance))
Answered By: Seyi Daniel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.