Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions bankaccountandinheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# -*- coding: utf-8 -*-
"""BankAccountAndInheritance.ipynb

Automatically generated by Colaboratory.

Original file is located at
https://colab.research.google.com/drive/1cQrDx8mP75VonT5C-FQWxINoj9zAJKaq
"""



class BankAccount:
nums = 1000

def __init__(self,name, balance):
self.name = name
self.pin = int(input(f"{self.name} You are making a new account! What is your pin? "))
self.accountNum = 0
self.accountNum = BankAccount.nums
self.balance = balance
self.transactions = []
BankAccount.nums += 1


def displayAccount(self):
check = int(input(f"Hello {self.name}, what is your pin? "))
if check != self.pin:
print("Access Denied")
else:
print(f"Hello User: {self.name} your Account Number is: {self.accountNum} you have $ {self.balance} in your account")
self.transactions.append("Account Displayed")


def withdraw(self):
check = int(input(f"Hello {self.name}, what is your pin? "))
if check != self.pin:
print("Access Denied")
else:
amount = int(input("How much would you like to withdraw? "))
if amount > self.balance:
print(f"Ummm {self.name} you dont have that much in your account. Try again.")
else:
self.balance = self.balance - amount
print(f"You now have {self.balance} remaining in your account")
self.transactions.append(f"Withdraw occurred, amount: {amount}")


def deposit(self):
check = int(input(f"Hello {self.name}, what is your pin? "))
if check != self.pin:
print("Access Denied")
else:
amount = int(input("How much would you like to deposit? "))
self.balance += amount
print(f"You now have {self.balance} remaining in your account")
self.transactions.append(f"Deposit occured, amount: {amount}")


#Sending Transfer
def transferTo(self, BankAccountObject):
check = int(input(f"Hello {self.name}, what is your pin? "))
if check != self.pin:
print("Access Denied")
else:
print(f"You are sending funds from {self.accountNum} to --> {BankAccountObject.accountNum}")
amount = int(input("How much would you like to transfer to them? "))

if amount > self.balance:
print(f"Ummm {self.name} you dont have that much in your account. Try again.")
else:
self.balance = self.balance - amount
BankAccountObject.balance = BankAccountObject.balance + amount
print(f"Account: {self.accountNum} You now have {self.balance} remaining in your account")
print(f"Account: {BankAccountObject.accountNum} You now have {BankAccountObject.balance} remaining in your account")
self.transactions.append(f"Transfer To occurred, amount: {amount}")


#Taking Transfer
def transferFrom(self, BankAccountObject):
check = int(input(f"Hello {self.name}, what is your pin? "))
if check != self.pin:
print("Access Denied")
else:
print(f"You are taking funds from {BankAccountObject.accountNum} to --> {self.accountNum}")
amount = int(input("How much would you like to transfer from them? "))

if amount > BankAccountObject.balance:
print(f"Ummm {self.name} they dont have that much in their account. Try again.")
else:
BankAccountObject.balance = BankAccountObject.balance - amount
self.balance += amount
print(f"Account: {self.accountNum} You now have {self.balance} remaining in your account")
print(f"Account: {BankAccountObject.accountNum} You now have {BankAccountObject.balance} remaining in your account")
self.transactions.append(f"Transfer From occurred, amount: {amount}")

#Class Ends -----


#General Code starts -----
Account1 = BankAccount("Freddy", 1000)
Account2 = BankAccount("Koko", 5000)
Account3 = BankAccount("George", 5000)

Account1.displayAccount()
Account2.displayAccount()
Account3.displayAccount()

Account2.transferTo(Account1)







"""**Inheritance Teaching Example**"""

class Animal:
animalCount = 0

def __init__(self,name, animalType):
self.name = name
self.animalType = "Carnivore"

self.animalType = animalType

Animal.animalCount = Animal.animalCount + 1


def displayInfo(self):
print(self.name, self.animalType)


class Herbivore(Animal):

def __init__(self, name):
super().__init__(name, "Herbivore")


#Animal Object made by Superclass constructor
animal1 = Animal("Appa", "Omnivore")
animal1.displayInfo()

#Herbivore Object made by subclass constructor (which runs the superclass constructor)
#Herbivore object can run methods of the superclass, they are inherited
animal2 = Herbivore("Momo")
animal2.displayInfo()


#Showing that subclass object has access to superclass static variable
print(animal1.animalCount)
print(animal2.animalCount)