Giter VIP home page Giter VIP logo

python-bank-account-inheritance's Introduction

Inheritance in Python

Use your knowledge of Python Classes and Python Inheritance to model several types of bank accounts.

Create your classes in the provided bank.py file.

Exercise: Write Bank Account Classes

Let's practice writing classes and using inheritance by modelling different types of Bank accounts.

Part 1: Making the Parent Class

  • Create a base BankAccount class
    • Bank accounts keep track of their current balance
      • the balance starts at 0 until a deposit is made
    • Bank accounts are created with a default interest rate of 2%
    • Bank accounts have a deposit method
      • the deposit method returns the balance of the account after adding the deposited amount.
    • Bank accounts have a withdraw method
      • the withdraw method returns the balance after the money was successfully withdrawn.
    • Bank accounts return False if someone tries to deposit or withdraw a negative amount.
    • Bank accounts have a accumulate_interest method that sets the balance equal to the balance plus the balance times the interest rate
      • accumulate_interest returns the balance of the account after calculating the accumulated interest

Sample Input:

basic_account = BankAccount()
basic_account.deposit(600)
print("Basic account has ${}".format(basic_account.balance))
basic_account.withdraw(17)
print("Basic account has ${}".format(basic_account.balance))
basic_account.accumulate_interest()
print("Basic account has ${}".format(basic_account.balance))
print()

Sample Output:

Basic account has $600
Basic account has $583
Basic account has $594.66

Part 2: Making a Child Class Override Parent Class Methods

  • Create a ChildrensAccount class
    • Children's bank accounts have an interest rate of Zero.
    • Every time accumulate_interest is executed on a Child's account the account always gets $10 added to the balance.

Sample Input:

childs_account = ChildrensAccount()
childs_account.deposit(34)
print("Child's account has ${}".format(childs_account.balance))
childs_account.withdraw(17)
print("Child's account has ${}".format(childs_account.balance))
childs_account.accumulate_interest()
print("Child's account has ${}".format(childs_account.balance))
print()

Sample Output:

Child's account has $34
Child's account has $17
Child's account has $27

Part 3: Making Overdraft Account With New Properties

  • Create an OverdraftAccount class
    • An overdraft account penalizes customers for trying to draw too much money out of their account.
    • Overdraft accounts are created with an overdraft_penalty property that defaults to $40.
    • Customer's aren't allowed to withdraw more money than they have in their account. If a customer tries to withdraw more than they have then the withdraw method returns False and their balance is deducted only by the amount of the overdraft_penalty.
    • Overdraft accounts don't accumulate interest if their balance is below zero, but return False instead.

Sample Input:

  overdraft_account = OverdraftAccount()
  overdraft_account.deposit(12)
  print("Overdraft account has ${}".format(overdraft_account.balance))
  overdraft_account.withdraw(17)
  print("Overdraft account has ${}".format(overdraft_account.balance))
  overdraft_account.accumulate_interest()
  print("Overdraft account has ${}".format(overdraft_account.balance))

Sample Output:

Overdraft account has $12
Overdraft account has $-28
Overdraft account has $-28

Licensing

All content is licensed under a CC­BY­NC­SA 4.0 license. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

python-bank-account-inheritance's People

Contributors

weston-bailey avatar brandiw avatar geluso avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.