Skip to content
Snippets Groups Projects
Commit bcb79fbb authored by Ulrich Kerzel's avatar Ulrich Kerzel
Browse files

move example for accounts (classes) into separate file

parent e886a50b
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
---
### Exercise
Imagine we are a bank and need to establish a way for us to track the transactions with customers.
In a very simple scenario, we need an account for each customer, that has the following properties:
* It is associated with a customer with their name.
* For simplicity, we only consider euros (i.e. no cents, just integer values).
* Customers can put money into the account, withdraw money and ask for the balance.
* The bank does not offer credit or overdraft.
Implement this as a class.
First, think about why the above requirements are ambiguous and not sufficient (and then come up with a way to mitigate this.)
Then, think about which cases you need to consider in your implementation.
We need to consider:
* "name" is not detailed enough: Do we mean the full name, just the surname or the given name, any middle names? Also, depending on the culture, the concept of a name may be different from what we expect in our western world.
For simplicity we just go with the surname as a single identifier. Obviously, this will create problems down the line, as many people share the same surname.
* We do not have any specification for gender assignments. Ignore for now.
* When a withdrawal is made, we need to check that:
* There are sufficient funds in the account
* The withdrawal is a positive integer number
* We can only withdraw the full value of the balance at most, since there is no overdraft/credit
* When depositing money, we need to check that:
* The deposit is a positive integer number.
%% Cell type:code id: tags:
``` python
class Account():
def __init__(self, name, balance = 0):
self._name = name
self._balance = int(balance)
if self._balance < 0:
self._balance = 0
def balance(self):
return self._balance
def name(self):
return self._name
def deposit(self, amount):
if type(amount) == int and amount > 0:
self._balance = self._balance + amount
else:
print('Invalid deposit')
def withdrawal(self, amount):
if type(amount) == int and amount > 0 and amount < self._balance:
self._balance = self._balance - amount
return self._balance
else:
print('Invalid withdrawal')
return None
```
%% Cell type:code id: tags:
``` python
account = Account('Smith')
print('Opened account for {}'.format(account.name()))
print('Current balance: {}'.format(account.balance()))
# make a deposit
account.deposit(10)
print('Current balance: {}'.format(account.balance()))
# try to make an invalid deposit -- should produce an error message
account.deposit(-10)
# try to make an invalid deposit -- should produce an error message
account.deposit(10.5)
# try to make an invalid deposit -- should produce an error message
account.deposit('ten')
# withdraw some money
my_money = account.withdrawal(5)
print('I have now {} euros in my hand'.format(my_money))
print('Current balance: {}'.format(account.balance()))
# try to make an invalid withdrawal -- should produce an error message and we have "nothing" in our hand
my_money = account.withdrawal(-10)
print('I have now {} euros in my hand'.format(my_money))
# try to make an invalid withdrawal -- should produce an error message and we have "nothing" in our hand
my_money = account.withdrawal(10.5)
print('I have now {} euros in my hand'.format(my_money))
# try to make an invalid withdrawal -- should produce an error message and we have "nothing" in our hand
my_money = account.withdrawal('ten')
print('I have now {} euros in my hand'.format(my_money))
# try to make an invalid withdrawal -- should produce an error message and we have "nothing" in our hand
my_money = account.withdrawal(500)
print('I have now {} euros in my hand'.format(my_money))
```
%% Output
Opened account for Smith
Current balance: 0
Current balance: 10
Invalid deposit
Invalid deposit
Invalid deposit
I have now 5 euros in my hand
Current balance: 5
Invalid withdrawal
I have now None euros in my hand
Invalid withdrawal
I have now None euros in my hand
Invalid withdrawal
I have now None euros in my hand
Invalid withdrawal
I have now None euros in my hand
%% Cell type:code id: tags:
``` python
##
## print the word 'run' from the string 'nurse'
##
my_word = 'nurse'
print(my_word[2::-1])
```
%% Output
run
%% Cell type:code id: tags:
``` python
##
## Fibonacci Numbers --- For Loop
##
Fibonacci = [0, 1]
for i in range(2,10,1):
Fib = Fibonacci[i-1] + Fibonacci[i-2]
Fibonacci.append(Fib)
print('The Fibonacci numbers are: {}'.format(Fibonacci))
```
%% Output
The Fibonacci numbers are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
%% Cell type:code id: tags:
``` python
##
## Fibonacci Numbers --- While Loop
##
Fibonacci = [0, 1]
while len(Fibonacci) < 10:
Fib = Fibonacci[-1] + Fibonacci[-2]
Fibonacci.append(Fib)
print('The Fibonacci numbers are: {}'.format(Fibonacci))
```
%% Cell type:code id: tags:
``` python
##
## Fibonacci Numbers --- Function
##
def compute_Fibonacci(n_numbers):
return_list = [0, 1]
for i in range(2,n_numbers,1):
Fib = return_list[i-1] + return_list[i-2]
return_list.append(Fib)
return return_list
Fib = compute_Fibonacci(n_numbers=10)
print('The Fibonacci numbers are: {}'.format(Fib))
```
%% Output
The Fibonacci numbers are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
%% Cell type:markdown id: tags:
---
### Exercise
Imagine we are a bank and need to establish a way for us to track the transactions with customers.
In a very simple scenario, we need an account for each customer, that has the following properties:
* It is associated with a customer with their name.
* For simplicity, we only consider euros (i.e. no cents, just integer values).
* Customers can put money into the account, withdraw money and ask for the balance.
* The bank does not offer credit or overdraft.
Implement this as a class.
First, think about why the above requirements are ambiguous and not sufficient (and then come up with a way to mitigate this.)
Then, think about which cases you need to consider in your implementation.
We need to consider:
* "name" is not detailed enough: Do we mean the full name, just the surname or the given name, any middle names? Also, depending on the culture, the concept of a name may be different from what we expect in our western world.
For simplicity we just go with the surname as a single identifier. Obviously, this will create problems down the line, as many people share the same surname.
* We do not have any specification for gender assignments. Ignore for now.
* When a withdrawal is made, we need to check that:
* There are sufficient funds in the account
* The withdrawal is a positive integer number
* We can only withdraw the full value of the balance at most, since there is no overdraft/credit
* When depositing money, we need to check that:
* The deposit is a positive integer number.
%% Cell type:code id: tags:
``` python
class Account():
def __init__(self, name, balance = 0):
self._name = name
self._balance = int(balance)
if self._balance < 0:
self._balance = 0
def balance(self):
return self._balance
def name(self):
return self._name
def deposit(self, amount):
if type(amount) == int and amount > 0:
self._balance = self._balance + amount
else:
print('Invalid deposit')
def withdrawal(self, amount):
if type(amount) == int and amount > 0 and amount < self._balance:
self._balance = self._balance - amount
return self._balance
else:
print('Invalid withdrawal')
return None
```
%% Cell type:code id: tags:
``` python
account = Account('Smith')
print('Opened account for {}'.format(account.name()))
print('Current balance: {}'.format(account.balance()))
# make a deposit
account.deposit(10)
print('Current balance: {}'.format(account.balance()))
# try to make an invalid deposit -- should produce an error message
account.deposit(-10)
# try to make an invalid deposit -- should produce an error message
account.deposit(10.5)
# try to make an invalid deposit -- should produce an error message
account.deposit('ten')
# withdraw some money
my_money = account.withdrawal(5)
print('I have now {} euros in my hand'.format(my_money))
print('Current balance: {}'.format(account.balance()))
# try to make an invalid withdrawal -- should produce an error message and we have "nothing" in our hand
my_money = account.withdrawal(-10)
print('I have now {} euros in my hand'.format(my_money))
# try to make an invalid withdrawal -- should produce an error message and we have "nothing" in our hand
my_money = account.withdrawal(10.5)
print('I have now {} euros in my hand'.format(my_money))
# try to make an invalid withdrawal -- should produce an error message and we have "nothing" in our hand
my_money = account.withdrawal('ten')
print('I have now {} euros in my hand'.format(my_money))
# try to make an invalid withdrawal -- should produce an error message and we have "nothing" in our hand
my_money = account.withdrawal(500)
print('I have now {} euros in my hand'.format(my_money))
```
%% Output
Opened account for Smith
Current balance: 0
Current balance: 10
Invalid deposit
Invalid deposit
Invalid deposit
I have now 5 euros in my hand
Current balance: 5
Invalid withdrawal
I have now None euros in my hand
Invalid withdrawal
I have now None euros in my hand
Invalid withdrawal
I have now None euros in my hand
Invalid withdrawal
I have now None euros in my hand
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment