
/* Generated by Interface Builder */
/* Modified by Eric Tremblay */

#import <appkit/Form.h>
#import "Calculator.h"


@implementation Calculator

#define CANADA 0
#define USA 1

int Country = USA; /*  Default country */
int dummy; /* Use as you wish */
    
- init
{
   [super init];
   return self;
 }

- appDidInit:sender;
/* This method is executed right after the application was initilized but before
 * the application receives it's first event.
 */
{
  
    [self getDefaults:sender]; /*This will reset/display the proper default rates for the country*/

    return self;
}
 
 
- calculate:sender
{
    [inputForm selectTextAt:0];
  
        {
	/* Declare variables */
	float CANADAdollar;
	float BritainPound;
	float FranceFranc;
	float GermanyMark;
	float ItalyLira;
	float JapanYen;
	float MexicoPeso;
	float SwitzerlandFranc;
	float GreeceDrachma;
	float OtherCurrency;
	
	/* Do the conversion */
	CANADAdollar = ([inputForm floatValue]/[canadarate floatValue]);
	BritainPound = ([inputForm floatValue]/[britainrate floatValue]);
	FranceFranc = ([inputForm floatValue]/[francerate floatValue]);
	GermanyMark = ([inputForm floatValue]/[germanyrate floatValue]);
	ItalyLira = ([inputForm floatValue]/[italyrate floatValue]);
	JapanYen = ([inputForm floatValue]/[japanrate floatValue]);
	MexicoPeso = ([inputForm floatValue]/[mexicorate floatValue]);
	SwitzerlandFranc = ([inputForm floatValue]/[switzerlandrate floatValue]);
	GreeceDrachma = ([inputForm floatValue]/[greecerate floatValue]);
	OtherCurrency = ([inputForm floatValue]/[otherrate floatValue]);
	
	/* Display the result */
	[outputForm setFloatValue:CANADAdollar at:0];
	[outputForm setFloatValue:BritainPound at:1];
	[outputForm setFloatValue:FranceFranc at:2];
	[outputForm setFloatValue:GermanyMark at:3];
	[outputForm setFloatValue:ItalyLira at:4];
	[outputForm setFloatValue:JapanYen at:5];
	[outputForm setFloatValue:MexicoPeso at:6];
	[outputForm setFloatValue:SwitzerlandFranc at:7];
	[outputForm setFloatValue:GreeceDrachma at:8];
	[outputForm setFloatValue:OtherCurrency at:9];
	}
	return self;
}


- getDefaults:sender
/* This method sets and displays all the values depending on what
 *  Country was selected
 */
{    
     
      if (Country == CANADA)
      
      {  /* open the If canada */
      
      /* Default values for Today's rates (April 25, 96) */
      float CANADAdollar=1.3621;  /* In this case the CANADAdollar is a US dollar */
      float BritainPound=2.0645;
      float FranceFranc=0.2654;
      float GermanyMark=0.8966;
      float ItalyLira=0.000878;
      float JapanYen=0.01278;
      float MexicoPeso=0.1838;
      float SwitzerlandFranc=1.1078;
      float GreeceDrachma=0.00562;
      float OtherCurrency=2;
        
      /* This assigns the default values in their proper form in the Today's Rates panel */
      [canadarate setFloatValue:CANADAdollar];
      [britainrate setFloatValue:BritainPound];
      [francerate setFloatValue:FranceFranc];
      [germanyrate setFloatValue:GermanyMark];
      [italyrate setFloatValue:ItalyLira];
      [japanrate setFloatValue:JapanYen];
      [mexicorate setFloatValue:MexicoPeso];
      [switzerlandrate setFloatValue:SwitzerlandFranc];
      [greecerate setFloatValue:GreeceDrachma];
      [otherrate setFloatValue:OtherCurrency];
      
      /* This displays the default values in their proper form in the Today's Rates panel */
      [canadarate display];
      [britainrate display];
      [francerate display];
      [germanyrate display];
      [italyrate display];
      [japanrate display];
      [mexicorate display];
      [switzerlandrate display];
      [greecerate display];
      [otherrate display];
   
      } /* close the if canada */

   
      if (Country == USA) 
      
      {  /* open the If usa */
   
      /* Default values for Today's rates (April 25,96) */
      /* The rates below are for one american dollar in foreign currency */
      /* Example one US dollar equals = 106.60 Japanese Yen */
      float CANADAdollar=0.73287;
      float BritainPound=1.5153;
      float FranceFranc=0.19450;
      float GermanyMark=0.65671;
      float ItalyLira=0.0006449;
      float JapanYen=0.009381;
      float MexicoPeso=0.1349;
      float SwitzerlandFranc=0.81142;
      float GreeceDrachma=0.004134;
      float OtherCurrency=2;
   
      /* This assigns the default values in their proper form in the Today's Rates panel */
      [canadarate setFloatValue:CANADAdollar];
      [britainrate setFloatValue:BritainPound];
      [francerate setFloatValue:FranceFranc];
      [germanyrate setFloatValue:GermanyMark];
      [italyrate setFloatValue:ItalyLira];
      [japanrate setFloatValue:JapanYen];
      [mexicorate setFloatValue:MexicoPeso];
      [switzerlandrate setFloatValue:SwitzerlandFranc];
      [greecerate setFloatValue:GreeceDrachma];
      [otherrate setFloatValue:OtherCurrency];
 
       /* Changes the labels in the Currency Converter panel and in the Today's rates
        * panel when the COUNTRY is set to USA. 
        */
       [inputForm setTitle:"U.S. Dollar :" at:0]; /* Changes the label in Currency Converter panel */
       [outputForm setTitle:"Canada:" at:0]; /* Changes the label in Currency Converter panel */
       [canadarate setTitle:"Canada:" at:0]; /* Changes the label in the Today's rates panel */
       [inputForm display];
       [outputForm display];
       [canadarate display];

        } /* close If usa */
      
      return self;  /* End getDefaults method */
 }


- setCanada:sender
/* This method changes the Country to CANADA and re-labels the panels */
{
    Country = CANADA;  /*  Sets Country to Canada */ 
     /* Changes the labels to be used by a Canadian.*/
     [inputForm setTitle:"Canadian Dollar:" at:0];
     [outputForm setTitle:"  U.S.A. :" at:0];
     [canadarate setTitle:"  U.S.A. :" at:0];
     [inputForm display];
     [outputForm display];
     [canadarate display];     
     [self getDefaults:sender];  /* This will reset/display the proper default rates this country */
     
     return self; /* Ends setCanada method */
} 

- setUSA:sender
 /* This method changes the Country to USA and re-lables the panels */
 {
     Country = USA;/* Sets Country to USA */
     /* Changes the labels to be use by an American. */
     [inputForm   setTitle:"U.S. Dollar:" at:0];
     [outputForm setTitle:"Canada:" at:0];
     [canadarate setTitle:"Canada:" at:0];
     [inputForm display];
     [outputForm display];
     [canadarate display];
     [self getDefaults:sender];  /* This will reset/display the proper default rates for this country */
       
      return self;  /* Ends setUSA method */
 }

- findRate:sender
/* This method will find the exchange rate.  When given X number of one currency
 *  and X number of another currency
 */
 {
   	
	float findRate; /* number variable */

        /* Calculate the exchange rate */
	findRate = ([firstCurrency floatValue]/[anotherCurrency floatValue]);

        /* Sets the value for the number findRate in the "exchange is:" form in the FindRate panel */
       [findRateOutput setFloatValue:findRate];
	
	/* Displays the result in the "exchange is:" form in the FindRate panel */
        [findRateOutput display];
 
        return self;  /* End findRate method */
 }

- findValue:sender
/* This method will find the value of a currency when given X number of currency and
 *  the exchange rate of this currency.
*/
 {
   	
	float findValue; /* number variable */

        /* Calculate the value */
	findValue = ([youHave floatValue]*[valueRate floatValue]);

        /* Sets the value for the number findValue in the "value is:" form in the findValue panel */
       [findValueOutput setFloatValue:findValue];
	
	/* Displays the results in the "value is:" form in the findValue panel */
        [findValueOutput display];
 
        return self;  /* End of findValue method */
 }

- closeInfo:sender
/* This method closes the Info panel and opens and displays the MoreInfo panel */
{
      [Info performClose:sender]; /* This closes the Info panel */
      [MoreInfo orderFront:sender]; /* This displays the MoreInfo panel */
      return self; /* End of closeInfo method */
 }
       
 
@end

