I’d appreciate any advice.
Header
#import <Foundation/Foundation.h>
@interface BNRStockHolding : NSObject
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
NSString *_stockName;
}
-(float)purchaseSharePrice;
- (void)setPurchaseSharePrice:(float)p;
-(float)currentSharePrice;
- (void)setCurrentSharePrice:(float)c;
-(int)numberOfShares;
- (void)setNumberOfShares:(int)n;
- (NSString *)stockName;
- (void)setStockName:(NSString *)name;
- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares
@end
Implementation
#import "BNRStockHolding.h"
@implementation BNRStockHolding
// Getter method for purchaseSharePrice
- (float)purchaseSharePrice
{
return _purchaseSharePrice;
}
// Setter method
- (void)setPurchaseSharePrice:(float)p
{
_purchaseSharePrice = p;
}
// Getter method for currentSharePrice
- (float)currentSharePrice
{
return _currentSharePrice;
}
// Setter method
- (void)setCurrentSharePrice:(float)c
{
_currentSharePrice = c;
}
// Getter method for numberOfShares
- (int)numberOfShares
{
return _numberOfShares;
}
// Setter method
- (void)setNumberOfShares:(int)n
{
_numberOfShares = n;
}
// Getter method for stockName
- (NSString *)stockName
{
return _stockName;
}
// Setter method
- (void)setStockName:(NSString *)name
{
_stockName = name;
}
- (float)costInDollars
{
// purchaseSharePrice * numberOfShares
float p = [self purchaseSharePrice];
return [self numberOfShares] * p;
}
- (float)valueInDollars
{
// currentSharePrice * numberOfShares
float p = [self currentSharePrice];
return [self numberOfShares] * p;
}
@end
Main
#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// (1) Create an instance of BNRStockHolding called yahoo
BNRStockHolding *yahoo = [[BNRStockHolding alloc] init];
// Give values to instance variables using setters
[yahoo setPurchaseSharePrice:5.5];
[yahoo setCurrentSharePrice:15.7];
[yahoo setNumberOfShares:42];
[yahoo setStockName:@("YHOO")];
// (2) Create an instance of BNRStockHolding called apple
BNRStockHolding *apple = [[BNRStockHolding alloc] init];
// Give values to instance variables using setters
[apple setPurchaseSharePrice:80];
[apple setCurrentSharePrice:600];
[apple setNumberOfShares:102];
[apple setStockName:@("AAPL")];
// (3) Create an instance of BNRStockHolding called google
BNRStockHolding *google = [[BNRStockHolding alloc] init];
// Give values to instance variables using setters
[google setPurchaseSharePrice:50];
[google setCurrentSharePrice:700];
[google setNumberOfShares:50];
[google setStockName:@("GOOG")];
// (4) Fill an array called 'myStocks' with three instances of BNRStockHolding
NSMutableArray *myStocks = [NSMutableArray array];
[myStocks addObject:yahoo];
[myStocks addObject:apple];
[myStocks addObject:google];
// (5) Iterate through the array and print out each value
for (BNRStockHolding *s in myStocks)
{
// Calculate the money spent on stocks
float cost = [s costInDollars];
float value = [s valueInDollars];
NSString *stock = [s stockName];
// Print the money spent on stocks and the current value
NSLog(@"I spent $%.2f on %@ stock and now its valued at $%.2f", cost, stock, value);
}
}
return 0;
}