0

I am very new to python let alone Tkinter, and am creating an object oriented bank account, i have a working program, however when designing the application i planned to add all the features of OOP into the code, how would i add method overriding and overloading to this program whilst keeping its full functionality? Give your insight.

Bank Account Code

from tkinter import *
from random import randint
import time

class Account:
    def __init__(self, init_balance=0):
        self.balance = init_balance
    def deposit(self, amount):
        self.balance += amount
    def withdraw(self, amount):
        self.balance -= amount
    def get_balance(self, init_balance, rate):
        return self.get_balance() * self._rate

class InterestAccount(Account):
    def __init__(self, init_balance=0, rate=0.1):
        super().__init__(init_balance)
        self._rate = rate
    def interest(self):
        return self.balance * self._rate

class GUI(Tk):
    def __init__(self):
       Tk.__init__(self)
        self.title('Bank Account')

        #Menu#
        menu = Menu(self)
        acct_type_menu = Menu(menu)
        menu.add_cascade(label='Account Type', menu=acct_type_menu)
        acct_type_menu.add_command(label='Standard', command=self.set_type_standard)
        acct_type_menu.add_command(label='Interest', command=self.set_type_interest)
        self.config(menu=menu)

        #Account#
        start_balance = randint(100, 500)
        self.acct = Account(start_balance)
        self.my_interest = InterestAccount(start_balance)
        self.interest = self.my_interest.balance + self.my_interest.interest()

        #Labels#
        Label(self, text='Current Balance:').pack()
        self.balance_label = Label(self, text='Error: Select account type')
        self.balance_label.pack()

        #Button#
        btns_frame = Frame(self)
        btns_frame.pack(side=TOP, fill=X)

        Button(btns_frame, text='Deposit', width=13, command=self.deposit).pack(side=LEFT)
        Button(btns_frame, text='Withdraw', width=13, command=self.withdraw).pack(side=RIGHT)

        #Textbox#

        vcmd = (self.register(self.onValidate), '%S')
        self.text = Entry(self, validate='key', vcmd=vcmd)
        self.text.pack()

    def onValidate(self, S):
        if S in '0123456789.':
            return True
        return False

    def set_type_standard(self):
        self.acct_type = 'standard'
        self.balance_label.config(text=round(self.acct.balance, 2))

    def set_type_interest(self):
        self.acct_type = 'interest'
        self.balance_label.config(text=round(self.interest, 2))

    def clear_entry(self):
        self.text.delete(0, END)

    def deposit(self): 
        if self.acct_type == 'interest':
            a = int(self.text.get())
            self.interest += a
            self.balance_label.config(text=round(self.interest, 2))
        elif self.acct_type == 'standard':
            a = int(self.text.get())
            self.acct.balance += a
            self.balance_label.config(text=round(self.acct.balance, 2))
        else:
            self.balance_label.config(text='Error: Select account type')
            self.clear_entry()

    def withdraw(self):
        if self.acct_type == 'interest':
            a = int(self.text.get())
            self.interest -= a
            self.balance_label.config(text=round(self.interest, 2))
        elif self.acct_type == 'standard':
            a = int(self.text.get())
            self.acct.balance -= a
            self.balance_label.config(text=round(self.acct.balance, 2))
        else:
            self.balance_label.config(text='Error: Select account type')
        self.clear_entry()


if __name__ == '__main__':
    GUI().mainloop()
  • Your question is unclear, and too broad. Why is simply creating a method in a subclass different than what you are asking for? What exactly are you wanting to override and overload? – Bryan Oakley Jul 20 '17 at 12:13
  • Is method overloading and overriding already implemented with my code? If so can you highlight where please, as i said im new. – Mohamed Ismail Jul 20 '17 at 12:21

1 Answers1

1

Function overloading (also method overloading) is a programming concept that allows programmers to define two or more functions with the same name and in the same scope.

You already have some "overloading" in your code:

class InterestAccount(Account):
    def __init__(self, init_balance=0, rate=0.1):

When creating a new InterestAccount object, it can be called with 0, 1, or 2 parameters because of those default values specified. As mentioned in this SO answer, Python is dynamically-typed, so you don't need to create multiple of the same method with different parameters like you do in Java.

Overriding is an object-oriented programming feature that enables a child class to provide different implementation for a method that is already defined and/or implemented in its parent class...

You have a great opportunity to override the deposit and withdraw methods in your InterestAccount class because it inherits from Account and currently uses its parent's implementations of these methods. Simply define deposit and withdraw methods in your InterestAccount class, but do something different than what is done in the parent class.

Nelson
  • 922
  • 1
  • 9
  • 23
  • Can you show me a coded example , im more of a visual learner, of how i could override , it would help alot. – Mohamed Ismail Jul 20 '17 at 12:24
  • @MohamedIsmail It's already done in your code. Copy and paste the `deposit` and `withdraw` methods from `Account` into `InterestAccount`, then change both methods to accommodate for the interest. There is also a mistake in your `Account.get_balance` method. It should be `def get_balance(self): return self.balance` – Nelson Jul 20 '17 at 12:30
  • Thank you very much nelson, you have been very helpful – Mohamed Ismail Jul 20 '17 at 12:34