Answer to Question #290424 in Python for dv13

Question #290424

1. Create a class BankAccount, the __init__ (self, pin) method should initialize the balance as 0 and pin as “pin”.


2. Create method name deposit (self, pin, amount), which takes 2 arguments, pin and amount. Method should increment account balance by amount and return new balance.


3. Create method name withdraw (self, pin, amount), which takes 2 arguments, pin and amount. Method should decrement the account balance by amount and return new balance. When user try to withdraw the amount more than the amount in the account then It should display the message “Not sufficient balance”.


4. Create method get_balance(self, pin), this method should return account balance.


5. Create method change_pin(self, old_pin, new_pin), should change the account pin


6. If user try to enter the amount with the wrong pin program should display the “Wrong Pin” error message.


7. Create two instances of the BankAccount and call all the methods. 


1
Expert's answer
2022-01-24T18:37:59-0500
class BankAccount:
    def __init__(self, pin):
        self._pin = pin
        self._balance = 0
    
    def deposite(self, pin, amount):
        if pin != self._pin:
            print("Wrong Pin")
            return
        self._balance += amount
        return self._balance


    def withdraw(self, pin, amount):
        if pin != self._pin:
            print("Wrong Pin")
            return
        if amount > self._balance:
            print("Not sufficient balance")
        else:
            self._balance -= amount
        return self._balance



    def get_balance(self, pin):
        if pin != self._pin:
            print("Wrong Pin")
            return
        return self._balance


    def change_pin(self, old_pin, new_pin):
        if old_pin != self._pin:
            print("Wrong Pin")
            return
        self._pin = new_pin


def main():
    account1 = BankAccount(1234)
    account2 = BankAccount(9876)


    while True:
        ans = input("Whould you like to procces? ")
        if ans != 'y' and ans != 'Y':
            break


        n = int(input("Select account (1 or 2): "))
        if n == 1:
            acc = account1
        elif n == 2:
            acc = account2
        else:
            continue


        print("1 deposit")
        print("2 withdraw")
        print("3 get balance")
        print("4 change pin")
        ans = int(input("Make your choice: "))


        if ans == 1:
            pin = int(input("Enter a PIN: "))
            amount = float(input("Enter amount to deposit: "))
            balance = acc.deposite(pin, amount)
            if balance:
                print(f"New balance is ${balance:.2f}")
        elif ans == 2:
            pin = int(input("Enter a PIN: "))
            amount = float(input("Enter amount to withdraw: "))
            balance = acc.withdraw(pin, amount)
            if balance:
                print(f"New balance is ${balance:.2f}")
        elif ans == 3:
            pin = int(input("Enter a PIN: "))
            balance = acc.get_balance(pin)
            if balance:
                print(f"Balance is ${balance:.2f}")
        elif ans == 4:
            pin = int(input("Enter a PIN: "))
            new_pin = int(input("Enter a new PIN: "))
            acc.change_pin(pin, new_pin)
        else:
            print("Incorect choice")
        
        print()
    print("Have a nice day!")


if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS