"Imagine we are a bank and need to establish a way for us to track the transactions with customers.\n",
"In a very simple scenario, we need an account for each customer, that has the following properties:\n",
"\n",
"* It is associated with a customer with their name.\n",
"* For simplicity, we only consider euros (i.e. no cents, just integer values).\n",
"* Customers can put money into the account, withdraw money and ask for the balance.\n",
"* The bank does not offer credit or overdraft.\n",
"\n",
"Implement this as a class.\n",
"First, think about why the above requirements are ambiguous and not sufficient (and then come up with a way to mitigate this.)\n",
"Then, think about which cases you need to consider in your implementation.\n",
"\n",
"\n",
"We need to consider:\n",
"* \"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.\n",
"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.\n",
"* We do not have any specification for gender assignments. Ignore for now.\n",
"* When a withdrawal is made, we need to check that:\n",
" * There are sufficient funds in the account\n",
" * The withdrawal is a positive integer number\n",
" * We can only withdraw the full value of the balance at most, since there is no overdraft/credit\n",
"* When depositing money, we need to check that:\n",
" * The deposit is a positive integer number."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Account():\n",
" def __init__(self, name, balance = 0):\n",
" self._name = name\n",
" self._balance = int(balance)\n",
" if self._balance < 0:\n",
" self._balance = 0\n",
"\n",
" def balance(self):\n",
" return self._balance\n",
"\n",
" def name(self):\n",
" return self._name\n",
"\n",
" def deposit(self, amount):\n",
" if type(amount) == int and amount > 0:\n",
" self._balance = self._balance + amount\n",
" else:\n",
" print('Invalid deposit')\n",
"\n",
" def withdrawal(self, amount):\n",
" if type(amount) == int and amount > 0 and amount < self._balance:\n",
" self._balance = self._balance - amount\n",
" return self._balance\n",
" else:\n",
" print('Invalid withdrawal')\n",
" return None\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opened account for Smith\n",
"Current balance: 0\n",
"Current balance: 10\n",
"Invalid deposit\n",
"Invalid deposit\n",
"Invalid deposit\n",
"I have now 5 euros in my hand\n",
"Current balance: 5\n",
"Invalid withdrawal\n",
"I have now None euros in my hand\n",
"Invalid withdrawal\n",
"I have now None euros in my hand\n",
"Invalid withdrawal\n",
"I have now None euros in my hand\n",
"Invalid withdrawal\n",
"I have now None euros in my hand\n"
]
}
],
"source": [
"account = Account('Smith')\n",
"print('Opened account for {}'.format(account.name()))\n",
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
"Imagine we are a bank and need to establish a way for us to track the transactions with customers.\n",
"In a very simple scenario, we need an account for each customer, that has the following properties:\n",
"\n",
"* It is associated with a customer with their name.\n",
"* For simplicity, we only consider euros (i.e. no cents, just integer values).\n",
"* Customers can put money into the account, withdraw money and ask for the balance.\n",
"* The bank does not offer credit or overdraft.\n",
"\n",
"Implement this as a class.\n",
"First, think about why the above requirements are ambiguous and not sufficient (and then come up with a way to mitigate this.)\n",
"Then, think about which cases you need to consider in your implementation.\n",
"\n",
"\n",
"We need to consider:\n",
"* \"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.\n",
"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.\n",
"* We do not have any specification for gender assignments. Ignore for now.\n",
"* When a withdrawal is made, we need to check that:\n",
" * There are sufficient funds in the account\n",
" * The withdrawal is a positive integer number\n",
" * We can only withdraw the full value of the balance at most, since there is no overdraft/credit\n",
"* When depositing money, we need to check that:\n",
" * The deposit is a positive integer number."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"class Account():\n",
" def __init__(self, name, balance = 0):\n",
" self._name = name\n",
" self._balance = int(balance)\n",
" if self._balance < 0:\n",
" self._balance = 0\n",
"\n",
" def balance(self):\n",
" return self._balance\n",
"\n",
" def name(self):\n",
" return self._name\n",
"\n",
" def deposit(self, amount):\n",
" if type(amount) == int and amount > 0:\n",
" self._balance = self._balance + amount\n",
" else:\n",
" print('Invalid deposit')\n",
"\n",
" def withdrawal(self, amount):\n",
" if type(amount) == int and amount > 0 and amount < self._balance:\n",
" self._balance = self._balance - amount\n",
" return self._balance\n",
" else:\n",
" print('Invalid withdrawal')\n",
" return None\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Opened account for Smith\n",
"Current balance: 0\n",
"Current balance: 10\n",
"Invalid deposit\n",
"Invalid deposit\n",
"Invalid deposit\n",
"I have now 5 euros in my hand\n",
"Current balance: 5\n",
"Invalid withdrawal\n",
"I have now None euros in my hand\n",
"Invalid withdrawal\n",
"I have now None euros in my hand\n",
"Invalid withdrawal\n",
"I have now None euros in my hand\n",
"Invalid withdrawal\n",
"I have now None euros in my hand\n"
]
}
],
"source": [
"account = Account('Smith')\n",
"print('Opened account for {}'.format(account.name()))\n",
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