Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Update ATM by debiting/crediting currencies<br>
- /// This method can be called with:<br>
- /// * all 4 arguments (cash in – cash out). currencyOut and currencyIn <b>must</b> be different<br>
- /// * the first 2 arguments (cash out only, paid for by credit card or bank account)<br>
- /// * the last 2 arguments (cash in only, to be credited to bank balance)<br>
- Method Update(currencyOut As FCE.CurrencyName = "", amountOut As %Numeric = 0, currencyIn As FCE.CurrencyName = "", amountIn As %Numeric = 0) As %Status
- {
- try {
- set status = $$$OK
- set (outChanged, inChanged) = 0
- // debit stock amount by amount taken out
- if (currencyOut '= "") && (amountOut > 0) {
- set outChanged = 1
- set stockAmountOut = ..CurrencyBalances.GetAt(currencyOut)
- do ..CurrencyBalances.SetAt(stockAmountOut - amountOut, currencyOut)
- set status = ..%ValidateObject()
- if '$$$ISOK(status) {
- set ex = ##class(%Exception.StatusException).CreateFromStatus(status)
- throw ex
- }
- }
- // credit stock amount by amount put in
- if (currencyIn '= "") && (amountIn > 0) {
- set inChanged = 1
- set stockAmountIn = ..CurrencyBalances.GetAt(currencyIn)
- do ..CurrencyBalances.SetAt(stockAmountIn + amountIn, currencyIn)
- set status = ..%ValidateObject()
- if '$$$ISOK(status) {
- set ex = ##class(%Exception.StatusException).CreateFromStatus(status)
- throw ex
- }
- }
- // create unforseen <DIVIDE> exception 10% of the time
- // if '$r(10) {write 1/0}
- } catch ex {
- #dim ex, exCustom as %Exception.StatusException // enable Studio Assist for the exception objects
- // return custom error status depending on value of ex.Code
- if (ex.Code = $$$ERRORCODE($$$DTMinVal)) {
- set status = $$$ERROR($$$GeneralError, "You are requesting too much from ATM: " _ currencyOut)
- set exCustom = ##class(%Exception.StatusException).CreateFromStatus(status)
- do exCustom.Log()
- } elseif (ex.Code = $$$ERRORCODE($$$DTMaxVal)) {
- set status = $$$ERROR($$$GeneralError, "You are putting too much into ATM: " _ currencyIn)
- set exCustom = ##class(%Exception.StatusException).CreateFromStatus(status)
- do exCustom.Log()
- } else { // or change unforeseen exception back into a status
- set status = ex.AsStatus()
- do ex.Log()
- }
- // reset ATM's currencyIn and currencyOut
- do:outChanged ..CurrencyBalances.SetAt(stockAmountOut, currencyOut)
- do:inChanged ..CurrencyBalances.SetAt(stockAmountIn, currencyIn)
- // write to console log, optionally triggering notification
- set consoleMsg = "ATM " _ ..SerialNumber _ " Stock Problem - Check Error Log for details: "
- set portalLink = "http://localhost:57772/csp/sys/op/UtilSysAppErrorNamespaces.csp"
- do ##class(%SYS.System).WriteToConsoleLog(consoleMsg _ portalLink, , 2)
- }
- return status
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement