Bank Account: Inheritance in python is the same as that of inheritance in real life. A class that inherits another class obtains all the latter’s attributes and methods. The former is called the Child class whilst the latter is called Parent class. This phenomenon would be very promising in applications dealing with multiple classes that are constituted by similar or more likely same attributes. You ‘ll get to know the importance of inheritance from the following problem. All type of accounts in a bank have common attributes which can be inherited from an Account class.
Create a class Account with the following attributes.
Attributes | Datatype |
account_name | str |
account_no | str |
bank_name | str |
Include the following method in the Account class.
Method | Description |
display() | This method displays the account details |
Create a class CurrentAccount with the following attributes, which inherit Account class
Attributes | Datatype |
tin_number | str |
Include a constructor
__init__(self,account name, account number, bank name,tin number)
Include the following method in the CurrentAccount class.
Method | Description |
display() | This method displays the TIN number |
Create a class SavingsAccount with following attributes, which inherit Account class.
Attributes | Datatype |
org_name | str |
Include a constructor
__init__(self,account name, account number, bank name,organisation name)
Include the following method in the SavingsAccount class.
Method | Description |
display() | This method displays the Organisation Name |
Input Format:
The first input corresponds to choose current or savings account
The next line consists of the account name, account number, bank name, org name or tin number (according to chosen account type)
Output Format:
The output consists of account details and TIN number or Organisation name
Refer to sample output for formatting specifications.
Sample Input/Output-1[All bold lines corresponds to the input]:
Choose Account Type
1.Savings Account
2.Current Account
1
Enter Account details in comma-separated(Account Name, Account Number, Bank Name, Organisation Name)
Morsh,033808020000879,Baroda,Amphisoft
Account Name:Morsh
Account Number:033808020000879
Bank Name:Baroda
Organisation Name:Amphisoft
Sample Input/Output-2[All bold lines corresponds to the input]:
Choose Account Type
1.Savings Account
2.Current Account
2
Enter Account details in comma separated(Account Name,Account Number,Bank Name,TIN Number)
Krish,131231451,ICICI,798902
Account Name:Krish
Account Number:131231451
Bank Name:ICICI
TIN Number:798902
Solution
class Account:
# fill your code
def __init__(self,account_name,account_no,bank_name):
self.account_name=account_name
self.account_no=account_no
self.bank_name=bank_name
def display(self):
print("Account Name: {}".format(self.account_name))
print("Account Number: {}".format(self.account_no))
print("Bank Name: {}".format(self.bank_name))
class SavingsAccount(Account):
# fill your code
def __init__(self,account_name,account_no,bank_name,org_name):
self.account_name=account_name
self.account_no=account_no
self.bank_name=bank_name
self.org_name=org_name
def display_SavingsAccount(self):
# fill your code
print("Account Name: {}".format(self.account_name))
print("Account Number: {}".format(self.account_no))
print("Bank Name: {}".format(self.bank_name))
print("Organisation Name: {}".format(self.org_name))
class CurrentAccount(Account):
# fill your code
def __init__(self,account_name,account_no,bank_name,tin_number):
self.account_name=account_name
self.account_no=account_no
self.bank_name=bank_name
self.tin_number=tin_number
def display_CurrentAccount(self):
# fill your code
print("Account Name: {}".format(self.account_name))
print("Account Number: {}".format(self.account_no))
print("Bank Name: {}".format(self.bank_name))
print("TIN Number: {}".format(self.tin_number))
#Main
# sauravhathi
print("Choose Account Type")
print("1.Savings Account")
print("2.Current Account")
n = int(input())
# fill your code
if n == 1:
print("Enter Account details in comma separated(Account Name,Account Number,Bank Name,Organisation Name")
account_name,account_no,bank_name,org_name=raw_input().split(",")
obj=SavingsAccount(account_name,account_no,bank_name,org_name)
obj.display_SavingsAccount()
elif n == 2:
print("Enter Account details in comma separated(Account Name,Account Number,Bank Name,TIN Number")
account_name,account_no,bank_name,tin_number=raw_input().split(",")
obj=CurrentAccount(account_name,account_no,bank_name,tin_number)
obj.display_CurrentAccount()
else:
print("Invalid Input")
exit()
Happy Learning – If you require any further information, feel free to contact me.