Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. /// Update ATM by debiting/crediting currencies<br>
  2. /// This method can be called with:<br>
  3. /// * all 4 arguments (cash in – cash out). currencyOut and currencyIn <b>must</b> be different<br>
  4. /// * the first 2 arguments (cash out only, paid for by credit card or bank account)<br>
  5. /// * the last 2 arguments (cash in only, to be credited to bank balance)<br>
  6. Method Update(currencyOut As FCE.CurrencyName = "", amountOut As %Numeric = 0, currencyIn As FCE.CurrencyName = "", amountIn As %Numeric = 0) As %Status
  7. {
  8.     try {
  9.         set status = $$$OK
  10.         set (outChanged, inChanged) = 0
  11.         // debit stock amount by amount taken out
  12.         if (currencyOut '= "") && (amountOut > 0) {
  13.             set outChanged = 1
  14.             set stockAmountOut = ..CurrencyBalances.GetAt(currencyOut)
  15.             do ..CurrencyBalances.SetAt(stockAmountOut - amountOut, currencyOut)
  16.             set status = ..%ValidateObject()
  17.             if '$$$ISOK(status) {
  18.                 set ex = ##class(%Exception.StatusException).CreateFromStatus(status)
  19.                 throw ex
  20.             }
  21.         }
  22.         // credit stock amount by amount put in
  23.         if (currencyIn '= "") && (amountIn > 0) {
  24.             set inChanged = 1
  25.             set stockAmountIn = ..CurrencyBalances.GetAt(currencyIn)
  26.             do ..CurrencyBalances.SetAt(stockAmountIn + amountIn, currencyIn)
  27.             set status = ..%ValidateObject()
  28.             if '$$$ISOK(status) {
  29.                 set ex = ##class(%Exception.StatusException).CreateFromStatus(status)
  30.                 throw ex
  31.             }
  32.         }      
  33.         // create unforseen <DIVIDE> exception 10% of the time
  34.         // if '$r(10) {write 1/0}
  35.     } catch ex {
  36.         #dim ex, exCustom as %Exception.StatusException // enable Studio Assist for the exception objects
  37.         // return custom error status depending on value of ex.Code
  38.         if (ex.Code = $$$ERRORCODE($$$DTMinVal)) {
  39.             set status = $$$ERROR($$$GeneralError, "You are requesting too much from ATM: " _ currencyOut)
  40.             set exCustom = ##class(%Exception.StatusException).CreateFromStatus(status)
  41.             do exCustom.Log()
  42.         } elseif (ex.Code = $$$ERRORCODE($$$DTMaxVal)) {
  43.             set status = $$$ERROR($$$GeneralError, "You are putting too much into ATM: " _ currencyIn)
  44.             set exCustom = ##class(%Exception.StatusException).CreateFromStatus(status)
  45.             do exCustom.Log()
  46.         } else { // or change unforeseen exception back into a status
  47.             set status = ex.AsStatus()
  48.             do ex.Log()
  49.         }
  50.         // reset ATM's currencyIn and currencyOut
  51.         do:outChanged ..CurrencyBalances.SetAt(stockAmountOut, currencyOut)
  52.         do:inChanged ..CurrencyBalances.SetAt(stockAmountIn, currencyIn)
  53.         // write to console log, optionally triggering notification
  54.         set consoleMsg = "ATM " _ ..SerialNumber _ " Stock Problem - Check Error Log for details: "
  55.         set portalLink = "http://localhost:57772/csp/sys/op/UtilSysAppErrorNamespaces.csp"
  56.         do ##class(%SYS.System).WriteToConsoleLog(consoleMsg _ portalLink, , 2)
  57.     }
  58.     return status
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement