The question should be finished with OCaml. Please provide the running screenshot and properly formatted code. Define a function open_account that takes an initial password as an argument and creates a bank account that stores the password, the current balance of the account (initially 0), and provides four methods, with signatures as indicated in the bank_account type: update_pass takes the old password as the first argument and the new password as the second argument, and updates the stored password if the provided old password is correct. It does not need to check if the old password is the same as the new password. deposit allows a user to deposit money into the account if the user provides the correct password. retrieve allows a user to retrieve money from the account if the user provides the correct password and there are sufficient funds available in the account. show_balance allows the user to query the account balance if the user provides the correct password. The function follows the format of: let open_account (pass: password) : bank_account = raise NotImplemented For all functions, you should raise an error message if the password provided is incorrect (see wrong_pass in the prelude). If the user inputed the wrong password for 3 times in a row, then the account will be locked, and no more operation can be performed on the account (see account_locked in the prelude). An error should also be raised if the user tries to use a negative value of money (see negative_amount in the prelude) for deposit and retrieve, but only if the provided password is correct. Likewise, another error should also be raised if the user tries to withdraw more money than what is in their account (see not_enough_balance in the prelude), but only after passing the password check. The required exceptions are provided in the prelude, and you must use them to get full credit.
The question should be finished with OCaml. Please provide the running screenshot and properly formatted code.
Define a function open_account that takes an initial password as an argument and creates a bank account that stores the password, the current balance of the account (initially 0), and provides four methods, with signatures as indicated in the bank_account type:
- update_pass takes the old password as the first argument and the new password as the second argument, and updates the stored password if the provided old password is correct. It does not need to check if the old password is the same as the new password.
- deposit allows a user to deposit money into the account if the user provides the correct password.
- retrieve allows a user to retrieve money from the account if the user provides the correct password and there are sufficient funds available in the account.
- show_balance allows the user to query the account balance if the user provides the correct password.
The function follows the format of:
let open_account (pass: password) : bank_account =
raise NotImplemented
For all functions, you should raise an error message if the password provided is incorrect (see wrong_pass in the prelude). If the user inputed the wrong password for 3 times in a row, then the account will be locked, and no more operation can be performed on the account (see account_locked in the prelude).
An error should also be raised if the user tries to use a negative value of money (see negative_amount in the prelude) for deposit and retrieve, but only if the provided password is correct. Likewise, another error should also be raised if the user tries to withdraw more money than what is in their account (see not_enough_balance in the prelude), but only after passing the password check. The required exceptions are provided in the prelude, and you must use them to get full credit.
Here is an example of a sequence of function calls and the expected results with some annotations:
# let a = open_account "123";;
val a : bank_account = (* stored password is "123" and stored balance is 0 *)
{update_pass = <fun>;
retrieve = <fun>;
deposit = <fun>;
show_balance = <fun>}
# a.deposit "123" 500;;
- : unit = () (* stored balance is now 0 + 500 = 500 *)
# a.deposit "123" (-150);;
Exception: Msg "Money amount below 0". (* no updates since the deposit failed *)
# a.deposit "12" 50;;
Exception: Msg "Wrong password". (* no updates since the password was wrong *)
# a.update_pass "123" "234";;
- : unit = () (* stored password is now "234" and balance is still 500 *)
# a.deposit "123" 50;;
Exception: Msg "Wrong password". (* no updates since the password was wrong *)
# a.show_balance "234";;
- : int = 500 (* show_balance does not update anything *)
# a.deposit "234" 50;;
- : unit = () (* stored balance is now 500 + 50 = 550 *)
# a.show_balance "234";;
- : int = 550
# a.retrieve "234" 100;;
- : unit = () (* stored balance is now 550 - 100 = 450 *)
# a.retrieve "234" 500;;
Exception: Msg "Insufficient balance". (* no updates since the retrieve failed *)
# a.show_balance "234";;
- : int = 450
# a.deposit "12" 50;;
Exception: Msg "Wrong password". (* no updates since the password was wrong *)
# a.deposit "12" 50;;
Exception: Msg "Wrong password". (* no updates since the password was wrong *)
# a.deposit "12" 50;;
Exception: Msg "Wrong password". (* no updates since the password was wrong *)
# a.deposit "234" 50;;
Exception: Msg "Account locked, please contact bank". (* no updates even with correct password, for the account is now locked *)
The prelude is defined as:
exception Msgof string
type password = string
type bank_account = {
update_pass : password -> password -> unit;
retrieve : password -> int -> unit;
deposit : password -> int -> unit;
show_balance : password -> int;
}
(*Errors. Please use these.*)let negative_amount = Msg "Money amount below 0"
let wrong_pass = Msg "Wrong password"
let account_locked = Msg "Account locked, please contact bank"
let not_enough_balance = Msg "Insufficient balance"
Step by step
Solved in 2 steps