Skip to content

Commit 5cedc1d

Browse files
committed
added OOP Challenge
1 parent 0f14403 commit 5cedc1d

File tree

4 files changed

+780
-0
lines changed

4 files changed

+780
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Object Oriented Programming Challenge\n",
8+
"\n",
9+
"For this challenge, create a bank account class that has two attributes:\n",
10+
"\n",
11+
"* owner\n",
12+
"* balance\n",
13+
"\n",
14+
"and two methods:\n",
15+
"\n",
16+
"* deposit\n",
17+
"* withdraw\n",
18+
"\n",
19+
"As an added requirement, withdrawals may not exceed the available balance.\n",
20+
"\n",
21+
"Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn."
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 1,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"class Account:\n",
31+
" pass"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 2,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"# 1. Instantiate the class\n",
41+
"acct1 = Account('Jose',100)"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 3,
47+
"metadata": {},
48+
"outputs": [
49+
{
50+
"name": "stdout",
51+
"output_type": "stream",
52+
"text": [
53+
"Account owner: Jose\n",
54+
"Account balance: $100\n"
55+
]
56+
}
57+
],
58+
"source": [
59+
"# 2. Print the object\n",
60+
"print(acct1)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 4,
66+
"metadata": {},
67+
"outputs": [
68+
{
69+
"data": {
70+
"text/plain": [
71+
"'Jose'"
72+
]
73+
},
74+
"execution_count": 4,
75+
"metadata": {},
76+
"output_type": "execute_result"
77+
}
78+
],
79+
"source": [
80+
"# 3. Show the account owner attribute\n",
81+
"acct1.owner"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 5,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"data": {
91+
"text/plain": [
92+
"100"
93+
]
94+
},
95+
"execution_count": 5,
96+
"metadata": {},
97+
"output_type": "execute_result"
98+
}
99+
],
100+
"source": [
101+
"# 4. Show the account balance attribute\n",
102+
"acct1.balance"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": 6,
108+
"metadata": {},
109+
"outputs": [
110+
{
111+
"name": "stdout",
112+
"output_type": "stream",
113+
"text": [
114+
"Deposit Accepted\n"
115+
]
116+
}
117+
],
118+
"source": [
119+
"# 5. Make a series of deposits and withdrawals\n",
120+
"acct1.deposit(50)"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 7,
126+
"metadata": {},
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"Withdrawal Accepted\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"acct1.withdraw(75)"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 8,
143+
"metadata": {},
144+
"outputs": [
145+
{
146+
"name": "stdout",
147+
"output_type": "stream",
148+
"text": [
149+
"Funds Unavailable!\n"
150+
]
151+
}
152+
],
153+
"source": [
154+
"# 6. Make a withdrawal that exceeds the available balance\n",
155+
"acct1.withdraw(500)"
156+
]
157+
},
158+
{
159+
"cell_type": "markdown",
160+
"metadata": {},
161+
"source": [
162+
"## Good job!"
163+
]
164+
}
165+
],
166+
"metadata": {
167+
"kernelspec": {
168+
"display_name": "Python 3",
169+
"language": "python",
170+
"name": "python3"
171+
},
172+
"language_info": {
173+
"codemirror_mode": {
174+
"name": "ipython",
175+
"version": 3
176+
},
177+
"file_extension": ".py",
178+
"mimetype": "text/x-python",
179+
"name": "python",
180+
"nbconvert_exporter": "python",
181+
"pygments_lexer": "ipython3",
182+
"version": "3.6.2"
183+
}
184+
},
185+
"nbformat": 4,
186+
"nbformat_minor": 2
187+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Object Oriented Programming Challenge - Solution\n",
8+
"\n",
9+
"For this challenge, create a bank account class that has two attributes:\n",
10+
"\n",
11+
"* owner\n",
12+
"* balance\n",
13+
"\n",
14+
"and two methods:\n",
15+
"\n",
16+
"* deposit\n",
17+
"* withdraw\n",
18+
"\n",
19+
"As an added requirement, withdrawals may not exceed the available balance.\n",
20+
"\n",
21+
"Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn."
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 1,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"class Account:\n",
31+
" def __init__(self,owner,balance=0):\n",
32+
" self.owner = owner\n",
33+
" self.balance = balance\n",
34+
" \n",
35+
" def __str__(self):\n",
36+
" return f'Account owner: {self.owner}\\nAccount balance: ${self.balance}'\n",
37+
" \n",
38+
" def deposit(self,dep_amt):\n",
39+
" self.balance += dep_amt\n",
40+
" print('Deposit Accepted')\n",
41+
" \n",
42+
" def withdraw(self,wd_amt):\n",
43+
" if self.balance >= wd_amt:\n",
44+
" self.balance -= wd_amt\n",
45+
" print('Withdrawal Accepted')\n",
46+
" else:\n",
47+
" print('Funds Unavailable!')"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 2,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"# 1. Instantiate the class\n",
57+
"acct1 = Account('Jose',100)"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 3,
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"name": "stdout",
67+
"output_type": "stream",
68+
"text": [
69+
"Account owner: Jose\n",
70+
"Account balance: $100\n"
71+
]
72+
}
73+
],
74+
"source": [
75+
"# 2. Print the object\n",
76+
"print(acct1)"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 4,
82+
"metadata": {},
83+
"outputs": [
84+
{
85+
"data": {
86+
"text/plain": [
87+
"'Jose'"
88+
]
89+
},
90+
"execution_count": 4,
91+
"metadata": {},
92+
"output_type": "execute_result"
93+
}
94+
],
95+
"source": [
96+
"# 3. Show the account owner attribute\n",
97+
"acct1.owner"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 5,
103+
"metadata": {},
104+
"outputs": [
105+
{
106+
"data": {
107+
"text/plain": [
108+
"100"
109+
]
110+
},
111+
"execution_count": 5,
112+
"metadata": {},
113+
"output_type": "execute_result"
114+
}
115+
],
116+
"source": [
117+
"# 4. Show the account balance attribute\n",
118+
"acct1.balance"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 6,
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"name": "stdout",
128+
"output_type": "stream",
129+
"text": [
130+
"Deposit Accepted\n"
131+
]
132+
}
133+
],
134+
"source": [
135+
"# 5. Make a series of deposits and withdrawals\n",
136+
"acct1.deposit(50)"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 7,
142+
"metadata": {},
143+
"outputs": [
144+
{
145+
"name": "stdout",
146+
"output_type": "stream",
147+
"text": [
148+
"Withdrawal Accepted\n"
149+
]
150+
}
151+
],
152+
"source": [
153+
"acct1.withdraw(75)"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 8,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"name": "stdout",
163+
"output_type": "stream",
164+
"text": [
165+
"Funds Unavailable!\n"
166+
]
167+
}
168+
],
169+
"source": [
170+
"# 6. Make a withdrawal that exceeds the available balance\n",
171+
"acct1.withdraw(500)"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"## Good job!"
179+
]
180+
}
181+
],
182+
"metadata": {
183+
"kernelspec": {
184+
"display_name": "Python 3",
185+
"language": "python",
186+
"name": "python3"
187+
},
188+
"language_info": {
189+
"codemirror_mode": {
190+
"name": "ipython",
191+
"version": 3
192+
},
193+
"file_extension": ".py",
194+
"mimetype": "text/x-python",
195+
"name": "python",
196+
"nbconvert_exporter": "python",
197+
"pygments_lexer": "ipython3",
198+
"version": "3.6.2"
199+
}
200+
},
201+
"nbformat": 4,
202+
"nbformat_minor": 2
203+
}

0 commit comments

Comments
 (0)