0 Replies Latest reply on May 3, 2007 3:10 PM by swodiddy

    Calculations

    swodiddy Level 1
      Hi there, I took the TipCalculator2.as from the flash 8 Samples folder and modified it to be a 401k calculator. Everything seems to be working & calculating correctly except when I select the radio for "Per pay period" and then change the "Percent to Contribute" value, it reverts back to calculating it "annually" instead of "Per Pay Period" which simply devides annual by 24.

      Can someone take a look at this code and help me determine why it reverts back to calculating it annually instaed of per pay period even though i've selected the radiobutton to calculate it by pay period?

      Do I need to seperate the functions? I'm confused....

      Here is a sample to view/test with:

      http://home.comcast.net/~shane.swoboda/four01k.html

      And here is my class file:

      //
      // four01k.as
      //
      // This is an example of event handling
      // and V2 components of Flash MX Professional 2004
      //

      import mx.controls.TextInput;
      import mx.controls.RadioButton;
      import mx.controls.RadioButtonGroup;

      class four01k extends MovieClip
      {
      // Declarations for the various components
      var subtotal:TextInput;
      var gratuity:TextInput;
      var increase:TextInput;
      var contribLabel:TextInput;
      var combinedLabel:TextInput;
      var radioGroup:RadioButtonGroup;
      var percentRadio1:RadioButton;
      var percentRadio24:RadioButton;

      // Initialization routine
      // Add event handlers for the subtotal text input and
      // the percentage radio buttons.
      public function onLoad():Void
      {
      subtotal.addEventListener("change", this);
      increase.addEventListener("change", this);
      gratuity.addEventListener("change", this);
      contribLabel.addEventListener("change", this);
      combinedLabel.addEventListener("change", this);
      percentRadio1.addEventListener("click", this);
      percentRadio24.addEventListener("click", this);
      }

      // Utility routine for converting a numeric amount
      // to a dollars-and-cents string
      private function formatCurrency(value:Number):String
      {
      var cents:Number = Math.round(value*100);
      var result:String = Math.floor(cents/100) + ".";
      cents %= 100;
      if (cents < 10) {
      result += "0";
      }
      result += String(cents);
      return result;
      }

      // Recalculate the gratuity text field based on the
      // subtotal input by the user, and the current
      // tip percentage
      private function calculate():Void
      {
      var percent:Number = Number(increase.text)/Number(100);
      var atotalinvested:Number = Number(subtotal.text) * percent;
      var atotalannual:Number = atotalinvested / 1;
      var hipercent = (.06);
      var contribpercent:Number = Number(increase.text);
      if (contribpercent >= (.07)) {
      contribpercent = hipercent;
      }
      var totalcontrib:Number = Number(subtotal.text) * contribpercent / (1);
      var totalcombined:Number = totalcontrib * .50;
      var atip:Number = totalcombined;
      if (isNaN(atip)) {
      contribLabel.text = "";
      } else {
      contribLabel.text = formatCurrency(atip);
      }
      var btip:Number = atotalannual;
      if (isNaN(btip)) {
      gratuity.text = "";
      } else {
      gratuity.text = formatCurrency(btip);
      }
      var empcont:Number = Number(gratuity.text);
      var corpcont:Number = Number(contribLabel.text);
      var totalcont:Number = Number(empcont+corpcont);

      combinedLabel.text = formatCurrency(totalcont);
      }

      private function calculatesemi():Void
      {
      var hipercent = (.06);
      var contribpercent:Number = Number(increase.text);
      if (contribpercent >= (.07)) {
      contribpercent = hipercent;
      }
      var totalcontrib:Number = Number(subtotal.text) * contribpercent / (24);
      var totalcombined:Number = totalcontrib * .50;
      var atip:Number = totalcombined;
      if (isNaN(atip)) {
      contribLabel.text = "";
      } else {
      contribLabel.text = formatCurrency(atip);
      }

      var percent:Number = Number(increase.text)/Number(100);
      var totalinvested:Number = Number(subtotal.text) * percent;
      var totalsemi:Number = totalinvested / 24;
      var dtip:Number = totalsemi;
      if (isNaN(dtip)) {
      gratuity.text = "";
      } else {
      gratuity.text = formatCurrency(dtip);
      }

      var empcont:Number = Number(gratuity.text);
      var corpcont:Number = Number(contribLabel.text);
      var totalcont:Number = Number(empcont+corpcont);

      combinedLabel.text = formatCurrency(totalcont);
      }


      // Event handler for "change"
      // If user changes subtotal, recalculate gratuity
      public function change(event:Object):Void
      {
      if (event.target == increase, subtotal, gratuity); {
      calculatesemi(), calculate();
      }
      }

      // Event handler for "click"
      // If user clicks a different percentage, recalculate gratuity
      public function click(event:Object):Void
      {
      switch (event.target) {
      case percentRadio1:
      calculate();
      break;
      }

      switch (event.target) {
      case percentRadio24:
      calculatesemi();
      break;
      }
      }
      }