% 
const BankAccount <- typeobject BankAccount
    operation deposit[Integer]
    operation withdraw[Integer] -> [Integer]
    function fetchBalance[] -> [Integer]
end BankAccount

const BAClass <- class BAClass
    var balance: Integer <-0
    export operation deposit[d: Integer]
       balance <- balance + d
    end deposit       
    export operation withdraw[amount: Integer] -> [r:Integer]
       if balance < 0 then
          r <- 0
       elseif amount > balance then
          r <-  balance
       end if
       balance <- balance - r
       r <- amount
    end withdraw

    export function fetchBalance[] -> [r:Integer]
       r <- balance
    end fetchBalance
end BAClass

const Prog1 <- object Prog1
   operation PrintElements[arr: array.of[Any]]
      (locate self)$stdout.PutString["PrintElements: \n"]
   end PrintElements

  process
    var home: Node <- locate self
    var ba: BankAccount
    var i: Integer <- 0
    var count: Integer
    var a: array.of[Any] <- array.of[Any].create[0]
    var x: Any
    a.addUpper[17]
    ba <- BAClass.create
    ba.deposit[250]
    a.addUpper[ba]
    a.addUpper["Emerald"]
    ba <- BAClass.create
    ba.deposit[300]
    a.addUpper[ba]

% Insert your code here

    count <- a.upperbound - a.lowerbound + 1
    i <- a.lowerbound
    loop
    exit when (count <= 0)
       home$stdout.PutString["Index " || i.asstring]
       x <- a[i]
       if (typeof x) *> BankAccount then
           home$stdout.PutString[" balance: " ||(view x as BankAccount).fetchBalance[].asstring]
       end if
       home$stdout.PutString[" " || "\n"]
       i <- i+1
       count <- count-1
    end loop
  end process
end Prog1
