Skip to content

Commit

Permalink
Add bank-account exercise files (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Sep 17, 2023
1 parent afed15d commit a33958d
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "bank-account",
"name": "Bank Account",
"uuid": "44b5048a-8e98-4300-8bd9-fce59dcc008b",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "binary-search",
"name": "Binary Search",
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/bank-account/.docs/instructions-append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Instructions append

Pyret doesn't support concurrency so that requirement can be set aside. Account operations are sequential on a single thread.
12 changes: 12 additions & 0 deletions exercises/practice/bank-account/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Instructions

Simulate a bank account supporting opening/closing, withdrawals, and deposits of money.
Watch out for concurrent transactions!

A bank account can be accessed in multiple ways.
Clients can make deposits and withdrawals using the internet, mobile phones, etc.
Shops can charge against the account.

Create an account that can be accessed from multiple threads/processes (terminology depends on your programming language).

It should be possible to close an account; operations against a closed account must fail.
17 changes: 17 additions & 0 deletions exercises/practice/bank-account/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"bank-account.arr"
],
"test": [
"bank-account-test.arr"
],
"example": [
".meta/example.arr"
]
},
"blurb": "Simulate a bank account supporting opening/closing, withdrawals, and deposits of money."
}
48 changes: 48 additions & 0 deletions exercises/practice/bank-account/.meta/example.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use context essentials2020

provide-types *

data Account:
| account() with:
method open(self):
open-account(0)
end,
method get-balance(self):
raise("account not open")
end,
method deposit(self, amount):
raise("account not open")
end,
method withdraw(self, amount):
raise("account not open")
end,
method close(self):
raise("account not open")
end
| open-account(balance :: NumInteger) with:
method get-balance(self):
self.balance
end,
method deposit(self, amount :: NumInteger):
if amount <= 0:
raise("amount must be greater than 0")
else:
open-account(self.balance + amount)
end
end,
method withdraw(self, amount :: NumInteger):
if amount <= 0:
raise("amount must be greater than 0")
else if amount > self.balance:
raise("amount must be less than balance")
else:
open-account(self.balance - amount)
end
end,
method close(self):
account()
end,
method open(self):
raise("account already open")
end
end
63 changes: 63 additions & 0 deletions exercises/practice/bank-account/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[983a1528-4ceb-45e5-8257-8ce01aceb5ed]
description = "Newly opened account has zero balance"

[e88d4ec3-c6bf-4752-8e59-5046c44e3ba7]
description = "Single deposit"

[3d9147d4-63f4-4844-8d2b-1fee2e9a2a0d]
description = "Multiple deposits"

[08f1af07-27ae-4b38-aa19-770bde558064]
description = "Withdraw once"

[6f6d242f-8c31-4ac6-8995-a90d42cad59f]
description = "Withdraw twice"

[45161c94-a094-4c77-9cec-998b70429bda]
description = "Can do multiple operations sequentially"

[f9facfaa-d824-486e-8381-48832c4bbffd]
description = "Cannot check balance of closed account"

[7a65ba52-e35c-4fd2-8159-bda2bde6e59c]
description = "Cannot deposit into closed account"

[a0a1835d-faae-4ad4-a6f3-1fcc2121380b]
description = "Cannot deposit into unopened account"

[570dfaa5-0532-4c1f-a7d3-0f65c3265608]
description = "Cannot withdraw from closed account"

[c396d233-1c49-4272-98dc-7f502dbb9470]
description = "Cannot close an account that was not opened"

[c06f534f-bdc2-4a02-a388-1063400684de]
description = "Cannot open an already opened account"

[0722d404-6116-4f92-ba3b-da7f88f1669c]
description = "Reopened account does not retain balance"

[ec42245f-9361-4341-8231-a22e8d19c52f]
description = "Cannot withdraw more than deposited"

[4f381ef8-10ef-4507-8e1d-0631ecc8ee72]
description = "Cannot withdraw negative"

[d45df9ea-1db0-47f3-b18c-d365db49d938]
description = "Cannot deposit negative"

[ba0c1e0b-0f00-416f-8097-a7dfc97871ff]
description = "Can handle concurrent transactions"
include = false
comment = "no concurrency support"
139 changes: 139 additions & 0 deletions exercises/practice/bank-account/bank-account-test.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use context essentials2020

include file("bank-account.arr")

#|
When working offline, all tests except the first one are skipped by default.
Once you get the first test running, unskip the next one until all tests pass locally.
Check the block comment below for further details.
|#

fun open-account-zero-balance():
check "Newly opened account has zero balance":
account().open().get-balance() is 0
end
end

fun single-deposit():
check "Single deposit":
account().open().deposit(100).get-balance() is 100
end
end

fun multiple-deposits():
check "Multiple deposits":
account().open().deposit(100).deposit(50).get-balance() is 150
end
end

fun withdraw-once():
check "Withdraw once":
account().open().deposit(100).withdraw(75).get-balance() is 25
end
end

fun withdraw-twice():
check "Withdraw twice":
account().open().deposit(100).withdraw(80).withdraw(20).get-balance() is 0
end
end

fun multiple-operations():
check "Can do multiple operations sequentially":
account()
^ _.open()
^ _.deposit(100)
^ _.deposit(110)
^ _.withdraw(200)
^ _.deposit(60)
^ _.withdraw(50)
^ _.get-balance() is 20
end
end

fun no-balance-for-closed-account():
check "Cannot check balance of closed account":
account().open().close().get-balance() raises "account not open"
end
end

fun no-deposit-for-closed-account():
check "Cannot deposit into closed account":
account().open().close().deposit(50) raises "account not open"
end
end

fun no-deposit-for-unopened-account():
check "Cannot deposit into unopened account":
account().deposit(50) raises "account not open"
end
end

fun no-withdraw-for-closed-account():
check "Cannot withdraw from closed account":
account().open().close().withdraw(50) raises "account not open"
end
end

fun no-close-unopened-account():
check "Cannot close an account that was not opened":
account().close() raises "account not open"
end
end

fun no-open-already-opened-account():
check "Cannot open an already opened account":
account().open().open() raises "account already open"
end
end

fun reopened-account-does-not-retain-balance():
check "Reopened account does not retain balance":
account().open().deposit(50).close().open().get-balance() is 0
end
end

fun no-withdraw-more-than-deposited():
check "Cannot withdraw more than deposited":
account().open().deposit(25).withdraw(50) raises "amount must be less than balance"
end
end

fun no-withdraw-negative-amount():
check "Cannot withdraw negative":
account().open().deposit(100).withdraw(-50) raises "amount must be greater than 0"
end
end

fun no-deposit-negative-amount():
check "Cannot deposit negative":
account().open().deposit(-50) raises "amount must be greater than 0"
end
end

#|
Code to run each test. Each line corresponds to a test above and whether it should be run.
To mark a test to be run, replace `false` with `true` on that same line after the comma.
test(test-a, true) will be run. test(test-a, false) will be skipped.
|#

data TestRun: test(run, active) end

[list:
test(open-account-zero-balance, true),
test(single-deposit, false),
test(multiple-deposits, false),
test(withdraw-once, false),
test(withdraw-twice, false),
test(multiple-operations, false),
test(no-balance-for-closed-account, false),
test(no-deposit-for-closed-account, false),
test(no-deposit-for-unopened-account, false),
test(no-withdraw-for-closed-account, false),
test(no-close-unopened-account, false),
test(no-open-already-opened-account, false),
test(reopened-account-does-not-retain-balance, false),
test(no-withdraw-more-than-deposited, false),
test(no-withdraw-negative-amount, false),
test(no-deposit-negative-amount, false)
].each(lam(t): when t.active: t.run() end end)
22 changes: 22 additions & 0 deletions exercises/practice/bank-account/bank-account.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use context essentials2020

provide-types *

data Account:
| account() with:
method open(self):
raise("please implement the open method")
end,
method close(self):
raise("please implement the close method")
end,
method deposit(self, amount):
raise("please implement the withdraw method")
end,
method withdraw(self, amount):
raise("please implement the withdraw method")
end,
method get-balance(self):
raise("please implement the get-balance method")
end
end

0 comments on commit a33958d

Please sign in to comment.