{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Object Oriented Programming Challenge\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": 40, "metadata": {}, "outputs": [], "source": [ "class Account:\n", " \n", " def __init__(self, owner, balance):\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, amount):\n", " if amount <= 0:\n", " print('Deposits must be greater than 0.')\n", " else:\n", " self.balance += amount\n", " print(\"Deposit Accepted\")\n", " \n", " def withdraw(self, amount):\n", " if amount <= 0:\n", " print('Withdrawls must be greater than 0.')\n", " elif self.balance - amount < 0:\n", " print('Funds Unavailable!')\n", " else:\n", " self.balance -= amount\n", " print('Withdrawal Accepted')\n", " \n", " \n", " " ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "# 1. Instantiate the class\n", "acct1 = Account('Jose',100)" ] }, { "cell_type": "code", "execution_count": 42, "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": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jose'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 3. Show the account owner attribute\n", "acct1.owner" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 4. Show the account balance attribute\n", "acct1.balance" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deposits must be greater than 0.\n" ] } ], "source": [ "# 5. Make a series of deposits and withdrawals\n", "acct1.deposit(-50)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Withdrawls must be greater than 0.\n" ] } ], "source": [ "acct1.withdraw(-75)" ] }, { "cell_type": "code", "execution_count": 47, "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!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }