(Swift 2.0) Create a simple IOS application that let user enter the data needed
ID: 3688268 • Letter: #
Question
(Swift 2.0) Create a simple IOS application that let user enter the data needed to calculate an amortization of a loan. Display the amortization (future value of the loan) on a graph using one of the charting libraries. The user must be able to type the values of:
- deposit
- interest rate
- number of compounding periods
- number of years invested
You can find a formula for the FV one the following website: http://www.moneychimp.com/articles/finworks/fmfutval.htm
Find and use one of the 5 most popular iOS Swift charting libraries.
Explanation / Answer
IOS Loan Calculator Application:
Its a loan calculator, where you fill in the principal amount, the interest rate, and the no. of years of the loan.
FV = P (1 + r / n)Yn
where P is the starting principal, r is the annual interest rate, Y is the number of years invested, and n is the number of compounding periods per year. FV is the future value, meaning the amount the principal grows to after Y years.
The app then calculates the monthly payment that you ought to pay, calculates the total interest paid throughout the loan and the total amount paid.
First off, open xcode and create a View based project.
Name it EasyLoanCalculator.
Please write code in EasyLoanCalculatorViewController.h and EasyLoanCalculatorViewController.m. The .h file is the header file where you write all your declarations. Including declarations for UI Controls like labels, textboxes. The .m file is where all code is.
In 'resources', you will see EasyLoanCalculatorViewController.xlb which is your Interface Builder file. You know, the one where you drag and drop UI controls in like for forms in VS. Click on it and Interface Builder (think VS only in design view) opens with a blank iPhone screen.
#import
@interface EasyLoanCalculatorViewController : UIViewController {
IBOutlet UITextField *tbxLoanAmount;
IBOutlet UITextField *annualInterestRate;
IBOutlet UITextField *noOfYears;
IBOutlet UILabel *monthlyPayment;
IBOutlet UILabel *totalInterest;
IBOutlet UILabel *totalPayment;
}
@property (nonatomic,retain) UITextField *tbxLoanAmount;
@property (nonatomic,retain) UITextField *annualInterestRate;
@property (nonatomic,retain) UITextField *noOfYears;
@property (nonatomic,retain) UILabel *monthlyPayment;
@property (nonatomic,retain) UILabel *totalInterest;
@property (nonatomic,retain) UILabel *totalPayment;
-(IBAction) bgTouched:(id) sender;
-(IBAction) btnCalculate:(id) sender;
@end
#import "EasyLoanCalculatorViewController.h"
@implementation EasyLoanCalculator3ViewController
@synthesize tbxLoanAmount;
@synthesize annualInterestRate;
@synthesize noOfYears;
@synthesize monthlyPayment;
@synthesize totalInterest;
@synthesize totalPayment;
-(IBAction) bgTouched:(id) sender{
[tbxLoanAmount resignFirstResponder];
[annualInterestRate resignFirstResponder];
[noOfYears resignFirstResponder];
}
-(IBAction) btnCalculate:(id) sender{
double loanAmount = [tbxLoanAmount.text doubleValue];
double intRate = [annualInterestRate.text doubleValue];
double years = [noOfYears.text doubleValue];
double r = intRate/1200; // to optimize to handle different payment periods
double n = years * 12;
double rPower = pow(1+r,n);
double paymentAmt = loanAmount * r * rPower / (rPower - 1);
double totalPaymentd = paymentAmt * n;
double totalInterestd = totalPaymentd - loanAmount;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
NSNumber *n1 = [NSNumber numberWithDouble:paymentAmt];
NSNumber *n2 = [NSNumber numberWithDouble:totalPaymentd];
NSNumber *n3 = [NSNumber numberWithDouble:totalInterestd];
NSString *paymentAmtString = [numberFormatter stringFromNumber:n1];
monthlyPayment.text = paymentAmtString;
NSString *totalInterestString =[numberFormatter stringFromNumber:n3];
totalInterest.text = totalInterestString;
NSString *totalPaymentString =[numberFormatter stringFromNumber:n2];
totalPayment.text = totalPaymentString;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[tbxLoanAmount release];
[annualInterestRate release];
[noOfYears release];
[monthlyPayment release];
[totalInterest release];
[totalPayment release];
[super dealloc];
}
@end
Now we need to link the Outlets to the UIControls that we have designed in our xib file.
Click on File's owner. Press and hold the Control key. Mouse click from File's owner to over the textbox. You should see a black box with drop down values of all the Outlets of textbox type that you have declared earlier. See below. Click on the Outlet which should correspond to that textbox. You have just linked your Outlet to the textbox.
Continue for the rest of the text boxes and labels. Having done that, we now proceed to link the button to the Action that we have implemented. i.e. link the Calculate button to the Calculate method.
Click on the button. Press and hold the Control key, and drag and hold to File's Owner. A black box with drop down values of the Events should appear. Select the correct event. In this case, we select the btnCalculate action.
You can think of Events or Actions as the same thing.
There you go, your Outlets and Actions are all properly linked. Just ensure that you save all your files with command-S. Build and Run with command-R and the iPhone simulator should appear with your loan application. =)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.