BNRStockHolding.h
[quote]#import <Foundation/Foundation.h>
@interface BNRStockHolding : NSObject
//Instances variables
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
}
//Getters and Setters
-(float)purchaseSharePrice;
-(void)setPurchaseSharePrice:(float)purchaseSharePrice;
-(float)currentSharePrice;
-(void)setCurrentSharePrice:(float)currentSharePrice;
-(int)numberOfShares;
-(void)setNumberOfShares:(int)numberOfShares;
//Instance Methods
-(float)costInDollars; //PurchaseSharePrice * numberOfShares
-(float)valueInDollars; //currentSharePrice * numberOfShares
@end
[/quote]
BNRStockHolding.m
[quote]#import “BNRStockHolding.h”
@implementation BNRStockHolding
//Purchase Share Price Implementation
-(float)purchaseSharePrice{
return _purchaseSharePrice;
}
-(void)setPurchaseSharePrice:(float)purchaseSharePrice {
_purchaseSharePrice = purchaseSharePrice;
}
//Current Share Price Implementation
-(float) currentSharePrice {
return _currentSharePrice;
}
-(void)setCurrentSharePrice: (float)currentSharePrice {
_currentSharePrice = currentSharePrice;
}
//Number of Shares Implementation
-(int) numberOfShares {
return _numberOfShares;
}
-(void)setNumberOfShares: (int) numberOfShares {
_numberOfShares = numberOfShares;
}
//Cost and value in dollars Methods Implementation
-(float)costInDollars {
return _purchaseSharePrice * _numberOfShares;
}
-(float)valueInDollars {
return _currentSharePrice * _numberOfShares;
}
@end
[/quote]
main.m
[quote]#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”
int main(int argc, const char * argv[])
{
@autoreleasepool {
//Creating Objects
//Stocks of Felix
BNRStockHolding *felixStocks = [[BNRStockHolding alloc]init];
//Setting values
[felixStocks setPurchaseSharePrice:2.30];
[felixStocks setCurrentSharePrice:4.50];
[felixStocks setNumberOfShares:40];
//Stocks of Mirka
BNRStockHolding *mirkaStocks = [[BNRStockHolding alloc]init];
//Setting values
[mirkaStocks setPurchaseSharePrice:12.19];
[mirkaStocks setCurrentSharePrice:10.56];
[mirkaStocks setNumberOfShares:90];
//Stocks of Kimberly
BNRStockHolding *kimberlyStocks = [[BNRStockHolding alloc]init];
//Setting values
[kimberlyStocks setPurchaseSharePrice:45.10];
[kimberlyStocks setCurrentSharePrice:49.51];
[kimberlyStocks setNumberOfShares:210];
//Creating the Array
NSMutableArray *stockArray = [[NSMutableArray alloc]init];
//Adding objects to the array
[stockArray addObject:felixStocks];
[stockArray addObject:mirkaStocks];
[stockArray addObject:kimberlyStocks];
//Array counter
NSUInteger objectCount = [stockArray count];
//for Loop
for (int i = 0; i < objectCount; i++) {
BNRStockHolding *valueOfStocks = [stockArray objectAtIndex:i];
//Output for Felix
if (i == 0) {
NSLog(@" The value in dollars of the stocks of Felix is %.2f, and the cost in dollars was %.2f\n",[valueOfStocks valueInDollars], [valueOfStocks costInDollars]);
}
//Output for Mirka
if (i == 1) {
NSLog(@" The value in dollars of the stocks of Mirka is %.2f, and the cost in dollars was %.2f\n",[valueOfStocks valueInDollars], [valueOfStocks costInDollars]);
}
//Output for Kimberly
if (i == 2) {
NSLog(@" The value in dollars of the stocks of Kimberly is %.2f, and the cost in dollars was %.2f\n",[valueOfStocks valueInDollars], [valueOfStocks costInDollars]);
}
}
}
return 0;
}
[/quote]