JSStockHolding.h
[code]{
NSString *_companyName;
int _numberShares;
float _purchaseSharePrice;
float _currentSharePrice;
}
-
(NSString *)companyName;
-
(void)setCompanyName:(NSString *)name;
-
(int)numberShares;
-
(void)setNumberShares:(int)sharesCount;
-
(float)purchaseSharePrice;
-
(void)setPurchaseSharePrice:(float)purchasePrice;
-
(float)currentSharePrice;
-
(void)setCurrentSharePrice:(float)currentPrice;
-
(float)costDollars; // purchaseSharePrice * numberShares
-
(float)valueDollars; // currentSharePrice * numberShares[/code]
JSStockHolding.m
[code]- (NSString *)companyName
{
return _companyName;
}
-
(void)setCompanyName:(NSString *)name
{
_companyName = [name uppercaseString];
} -
(int)numberShares
{
return _numberShares;
} -
(void)setNumberShares:(int)sharesCount
{
_numberShares = sharesCount;
} -
(float)purchaseSharePrice
{
return _purchaseSharePrice;
} -
(void)setPurchaseSharePrice:(float)purchasePrice
{
_purchaseSharePrice = purchasePrice;
} -
(float)currentSharePrice
{
return _currentSharePrice;
} -
(void)setCurrentSharePrice:(float)currentPrice
{
_currentSharePrice = currentPrice;
} -
(float)costDollars
{
return [self purchaseSharePrice] * [self numberShares];
} -
(float)valueDollars
{
return [self currentSharePrice] * [self numberShares];
} -
(NSString *)description
{
NSString *description = [NSString stringWithFormat:@“Stock: %@\nShare Volume: %d\nPurchase Price: %.2f\nShare Price: %.2f\nCost: %.2f\nValue: %.2f”, [self companyName], [self numberShares], [self purchaseSharePrice], [self currentSharePrice], [self costDollars], [self valueDollars]];
return description;
}[/code]
Main.m
[code]NSMutableArray *stocks = [[NSMutableArray alloc] init];
JSStockHolding *google = [[JSStockHolding alloc] init];
[google setCompanyName:@“Google”];
[google setPurchaseSharePrice:2.30];
[google setCurrentSharePrice:4.50];
[google setNumberShares:40];
[stockList addObject:google];
JSStockHolding *microsoft = [[JSStockHolding alloc] init];
[microsoft setCompanyName:@“Microsoft”];
[microsoft setPurchaseSharePrice:12.19];
[microsoft setCurrentSharePrice:10.56];
[microsoft setNumberShares:90];
[stockList addObject:microsoft];
JSStockHolding *apple = [[JSStockHolding alloc] init];
[apple setCompanyName:@“Apple”];
[apple setPurchaseSharePrice:45.10];
[apple setCurrentSharePrice:49.51];
[apple setNumberShares:210];
[stockList addObject:apple];
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
NSString *reportDate = [dateFormatter stringFromDate:currentDate];
NSLog(@“FINANCIAL INVESTMENT ANALYSIS\n(Generated on %@)\n-----\n”, reportDate);
for (JSStockHolding *stock in stocks) {
NSLog(@"%@\n", stock);
}[/code]
Output
[quote]FINANCIAL INVESTMENTS ANALYSIS
(Generated on May 31, 2015)
Stock: GOOGLE
Share Volume: 40
Purchase Price: $2.30
Share Price: $4.50
Cost: $92.00
Value: $180.00
Stock: MICROSOFT
Share Volume: 90
Purchase Price: $12.19
Share Price: $10.56
Cost: $1097.10
Value: $950.40
Stock: APPLE
Share Volume: 210
Purchase Price: $45.10
Share Price: $49.51
Cost: $9471.00
Value: $10397.10[/quote]