// Program to calculate interest on investment
// Uses GraphicApplication, though should use Application
// as no canvas needed.  Change when it is fixed.

dialect "objectdraw"

def interesting: GraphicApplication = object {
  inherit graphicApplicationSize (300 @ 300)
  
  // labels for text fields for principal, interest, time, and final amount
  def principalLabel: TextBox = textBoxWith ("Starting Principal: ")
  def interestLabel: TextBox = textBoxWith ("Interest Rate: ")
  def amountLabel: TextBox = textBoxWith ("Final Amount: ")
  def yearsLabel: TextBox = textBoxWith (" Years:")
  
  // Text fields for principal, interest, and time
  def interestField: NumberField = numberFieldLabeled ("0")
  def principalField: NumberField = numberFieldLabeled ("0")
  def yearsField: NumberField = numberFieldLabeled ("0")
  
  // slot for answer to how much have after getting interest
  def amountField: TextBox = textBoxWith ("0")

  
  // If the user changes the entry to any field, recalculate
  // amount gained
  interestField.onChangeDo {evt: Event -> recalculateAmount}
  principalField.onChangeDo {evt: Event -> recalculateAmount}
  yearsField.onChangeDo {evt: Event -> recalculateAmount}

  // Set up column of labels for text fields
  def col1: Container = emptyContainer
  col1.arrangeVertical
  col1.append (principalLabel)
  col1.append (interestLabel)
  col1.append (yearsLabel)
  col1.append (amountLabel)
  
  // set up column of text fields (and one label)
  def col2: Container = emptyContainer
  col2.arrangeVertical
  col2.append (principalField)
  col2.append (interestField)
  col2.append (yearsField)
  col2.append (amountField)
  
  // Add both columns to bottom of window
  append (col1)
  append (col2)
  
  // Calculate the amount obtained from the principal @ interest rate in year
  method recalculateAmount -> Done {
    var finalAmount: Number := principalField.number

    for (1 .. yearsField.number) do {yearNum: Number ->
      finalAmount := finalAmount * (1 + interestField.number / 100)
    }
    amountField.contents:= finalAmount.asString
  }
  
  startGraphics

 
}