Thursday, 26 July 2012

# P1: Investment Calculator on iOS

Project Background
Develop an Application to calculate Future Values (Simple Future Value and Compound Future Value) for the given Principal amount, Period in years and rate of interest.
Reference Formulas:
    Simple Future Value = principal + (principal * interest * period);
    Compound Future Value = principal * [ (interest + 1) ^ period ]

Project Setup
  • Create View Based Project
  • XCode will create necessary ViewController header (.h), implementation (.m) and View (.xib) files.

  • Update above .h and .m files with below code 
ViewController.h
-----------------------
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate> {
    IBOutlet UILabel *i;
    UISlider *slider;
    IBOutlet UILabel *futureValueSimple;
    IBOutlet UILabel *futureValueCompound;
    IBOutlet UITextField *months;
    IBOutlet UITextField *principal;
}

@property (nonatomic, retain) UILabel *i;
@property (nonatomic, retain) UILabel *futureValueSimple;
@property (nonatomic, retain) UILabel *futureValueCompound;
@property (nonatomic, retain) UITextField *months;
@property (nonatomic, retain) UITextField *principal;

-(IBAction)valueChange:(id)sender;
-(IBAction)iChange:(id)sender;
-(void)updateFutureValue;

@end

ViewController.m
-----------------------
#import "ViewController.h"

@implementation ViewController

@synthesize i;
@synthesize futureValueSimple;
@synthesize futureValueCompound;
@synthesize months;
@synthesize principal;

-(IBAction)valueChange:(UIStepper *)sender {
    int m = [sender value];
    
    [months setText : [NSString stringWithFormat:@"%d",m]];
    
    [self updateFutureValue];
}

-(IBAction)iChange:(UISlider*) sender {
    
    double interest = [sender value];
    
    [i setText:[NSString stringWithFormat:@"%.2f", interest]];
    
    [self updateFutureValue];
    
}

//Delegate Method from <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    //dismiss keyboard    
    [theTextField resignFirstResponder];
    [self updateFutureValue];
    return YES;
}

- (void)updateFutureValue {
    
    float interest =  [[i text] floatValue] / 100.0
    int n =  [[months text] intValue];
    float p =  [[principal text] floatValue];
    
    float fs = p + (p * interest * n);
    float fc = p * pow((interest + 1), n);
    
    //fv = pv(i + 1)^m
    [futureValueSimple setText:[NSString stringWithFormat:@"%.2f", fs]];
    [futureValueCompound setText:[NSString stringWithFormat:@"%.2f", fc]];
    
    return;
}

- (void)dealloc {
    [i release];
    [futureValueSimple release];
    [futureValueCompound release];
    [months release];
    [principal release];
    [super dealloc];
}

ViewController.xib

Connections
  • All Relevant Connections between Code and View
  • Method Connections

































  • Similarly do other UI connections
Output

No comments:

Post a Comment