{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Object Oriented Programming Challenge - Solution\n", "\n", "For this challenge, create a bank account class that has two attributes:\n", "\n", "* owner\n", "* balance\n", "\n", "and two methods:\n", "\n", "* deposit\n", "* withdraw\n", "\n", "As an added requirement, withdrawals may not exceed the available balance.\n", "\n", "Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "class Account:\n", " def __init__(self,owner,balance=0):\n", " self.owner = owner\n", " self.balance = balance\n", " \n", " def __str__(self):\n", " return f'Account owner: {self.owner}\\nAccount balance: ${self.balance}'\n", " \n", " def deposit(self,dep_amt):\n", " self.balance += dep_amt\n", " print('Deposit Accepted')\n", " \n", " def withdraw(self,wd_amt):\n", " if self.balance >= wd_amt:\n", " self.balance -= wd_amt\n", " print('Withdrawal Accepted')\n", " else:\n", " print('Funds Unavailable!')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# 1. Instantiate the class\n", "acct1 = Account('Jose',100)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Account owner: Jose\n", "Account balance: $100\n" ] } ], "source": [ "# 2. Print the object\n", "print(acct1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jose'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 3. Show the account owner attribute\n", "acct1.owner" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 4. Show the account balance attribute\n", "acct1.balance" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deposit Accepted\n" ] } ], "source": [ "# 5. Make a series of deposits and withdrawals\n", "acct1.deposit(50)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Withdrawal Accepted\n" ] } ], "source": [ "acct1.withdraw(75)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funds Unavailable!\n" ] } ], "source": [ "# 6. Make a withdrawal that exceeds the available balance\n", "acct1.withdraw(500)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Good job!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }