We have been learning about Object Orientated Programming in my Computer Science lessons but the code that we were given as an example doesn't seem to work properly.
class bankAccount():
'''This is a bank account class'''
def __init__(self, account_name = "Bank Account", balance = 1500):
self.__account_name = account_name
self.__balance = balance
def deposit(self, value):
self.__balance = self.__balance + value
print("You now have: ", self.__balance)
def withdraw(self, value):
self.__balance = self.__balance - value
print("You now have: ", self.__balance)
class currentAccount(bankAccount):
'''This is a current account class'''
def __init__(self, account_name = "Current Account", balance = 1500):
self.__account_name = account_name
self.__balance = balance
super().__init__()
def withdraw(self, value):
if value > 1000:
print("You will have to phone the bank manager")
else:
self.__balance = self.__balance - value
print("You now have: ", self.__balance)
class savingsAccount(bankAccount):
'''This is a current account class'''
def __init__(self, account_name = "Savings Account", balance = 1500):
self.__account_name = account_name
self.__balance = balance
super().__init__()
def deposit(self, value):
self.__balance = self.__balance + (value *1.03)
print("You now have: ", self.__balance)
currentObject = currentAccount()
savingsObject = savingsAccount()
while True:
print("1. Current Account")
print("2. Savings Account")
menu_option = int(input())
if menu_option == 1:
print("1. Deposit funds")
print("2. Withdraw funds")
submenu_option = int(input())
if submenu_option == 1:
value = int(input("How much would you like to deposit? "))
currentObject.deposit(value)
elif submenu_option == 2:
value = int(input("How much would you like to withdraw? "))
currentObject.withdraw(value)
else:
print("Wrong menu choice!")
elif menu_option == 2:
print("1. Deposit funds")
print("2. Withdraw funds")
submenu_option = int(input())
if submenu_option == 1:
value = int(input("How much would you like to deposit? "))
savingsObject.deposit(value)
elif submenu_option == 2:
value = int(input("How much would you like to withdraw? "))
savingsObject.withdraw(value)
else:
print("Wrong menu choice!")
else:
print("Wrong menu choice!")
input()
Seemingly there is a separate data stored for each withdrawal and deposit. For example, if you deposit 100 from a savings account and then withdraw 100, instead of going from (1500 + 100) = 1600 and then doing (1600 - 100) the program seems to default back to using 1500 as the initial balance creating different balances for when you deposit and withdraw.
Any help would be greatly appreciated as OOP is new to me, and I can't figure out why this is happening.