This solution took me about an hour, but I’m happy I was able to implement a number of the prior lessons.
The use is prompted for the number of stocks for which they would like to determine cost and value.
Once all of the purchase prices, current share prices and numbers of shares are collected the user sees the cost/value analysis.
[code]//
// BNRStockHolding.h
// Stocks
//
// Created by Richard Pereira1 on 9/24/14.
// Copyright © 2014 BIg Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BNRStockHolding : NSObject
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
}
// Instance variables
-
(float)purchaseSharePrice;
-
(void)setPurchaseSharePrice:(float) p;
-
(float)currentSharePrice;
-
(void)setCurrentSharePrice:(float) c;
-
(int)numberOfShares;
-
(void)setnumberOfShares:(int) ns;
//Instance methods
- (float)costInDollars;
- (float)valueInDollars;
@end[/code]
[code]//
// BNRStockHolding.m
// Stocks
//
// Created by Richard Pereira1 on 9/24/14.
// Copyright © 2014 BIg Nerd Ranch. All rights reserved.
//
#import “BNRStockHolding.h”
@implementation BNRStockHolding
-
(float)purchaseSharePrice
{
return _purchaseSharePrice;
} -
(void)setPurchaseSharePrice:(float) p
{
_purchaseSharePrice = p;
} -
(float)currentSharePrice
{
return _currentSharePrice;
} -
(void)setCurrentSharePrice:(float) c
{
_currentSharePrice = c;
} -
(int)numberOfShares
{
return _numberOfShares;
} -
(void)setnumberOfShares:(int)ns
{
_numberOfShares = ns;
} -
(float)costInDollars
{
return _purchaseSharePrice * _numberOfShares;
} -
(float)valueInDollars
{
return _currentSharePrice * _numberOfShares;
}
@end[/code]
[code]//
// main.m
// Stocks
//
// Created by Richard Pereira1 on 9/24/14.
// Copyright © 2014 BIg Nerd Ranch. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <readline/readline.h>
#import “BNRStockHolding.h”
int main(int argc, const char * argv[]) {
@autoreleasepool {
//Create a Mutable Array that will hold some number of BNRStockHolding instances
NSMutableArray *stockHoldings = [NSMutableArray array];
// How many stocks
NSLog(@"For how many stocks would you like to calculate Cost and value?");
NSString *stockCount = [NSString stringWithUTF8String:readline(NULL)];
NSInteger numberOfStocks = [stockCount integerValue];
// For each stock get the pruchase price, current price and number of shares
for (int i = 1; i <= numberOfStocks; i++) {
NSLog(@"\n\nWhat was the purchase price of stock # %d ?", i);
NSString *purchasePrice = [NSString stringWithUTF8String:readline(NULL)];
NSLog(@"\nWhat is the current price of stock # %d ?", i);
NSString *currentPrice = [NSString stringWithUTF8String:readline(NULL)];
NSLog(@"\nHow many shares of stock # %d do you own?", i);
NSString *shares = [NSString stringWithUTF8String:readline(NULL)];
int numberOfShares = (int)[shares integerValue];
//Create an instance of BNRStockHolding
BNRStockHolding *aStock = [[BNRStockHolding alloc] init];
//Set the values as read
[aStock setPurchaseSharePrice:[purchasePrice floatValue]];
[aStock setCurrentSharePrice:[currentPrice floatValue]];
[aStock setnumberOfShares:numberOfShares];
//Add this instance to the Mutable Array
[stockHoldings addObject:aStock];
}
//Now Loop through the Mutable Array and print out the purchase price, current price, number of shares, cost and value
for (BNRStockHolding *h in stockHoldings) {
//Get the cost and value using the instance methods
float cost = [h costInDollars];
float value = [h valueInDollars];
NSLog(@"\n\nThe stock you purchased at $%.2f is now priced at $%.2f.\n You purchased %d shares.\n The cost of this purchase was $%.2f and the current value is $%.2f\n\n", [h purchaseSharePrice], [h currentSharePrice], [h numberOfShares], cost, value);
}
}
return 0;
}[/code]
Output: (remember the duplication as data is entered, so the number of stocks is 3 NOT ‘33’)
[b]2014-09-24 17:46:51.675 Stocks[24308:303] For how many stocks would you like to calculate Cost and value?
33
2014-09-24 17:47:04.712 Stocks[24308:303]
What was the purchase price of stock # 1 ?
22…33
2014-09-24 17:47:17.976 Stocks[24308:303]
What is the current price of stock # 1 ?
44…55
2014-09-24 17:47:21.960 Stocks[24308:303]
How many shares of stock # 1 do you own?
4400
2014-09-24 17:47:27.640 Stocks[24308:303]
What was the purchase price of stock # 2 ?
1122…1199
2014-09-24 17:47:32.376 Stocks[24308:303]
What is the current price of stock # 2 ?
1100…5566
2014-09-24 17:47:38.304 Stocks[24308:303]
How many shares of stock # 2 do you own?
9900
2014-09-24 17:47:39.790 Stocks[24308:303]
What was the purchase price of stock # 3 ?
4455…1100
2014-09-24 17:47:44.512 Stocks[24308:303]
What is the current price of stock # 3 ?
4499…5511
2014-09-24 17:47:52.696 Stocks[24308:303]
How many shares of stock # 3 do you own?
221100
2014-09-24 17:47:55.616 Stocks[24308:303]
The stock you purchased at $2.30 is now priced at $4.50.
You purchased 40 shares.
The cost of this purchase was $92.00 and the current value is $180.00
2014-09-24 17:47:55.617 Stocks[24308:303]
The stock you purchased at $12.19 is now priced at $10.56.
You purchased 90 shares.
The cost of this purchase was $1097.10 and the current value is $950.40
2014-09-24 17:47:55.618 Stocks[24308:303]
The stock you purchased at $45.10 is now priced at $49.51.
You purchased 210 shares.
The cost of this purchase was $9471.00 and the current value is $10397.10[/b]
Program ended with exit code: 0