My Solution to Chap. 18 Challenge


#1

I believe this accomplishes what Aaron asked for.

//
// main.m
// Stock Calc 2
//
// Created by Nathan Annenberg on 9/2/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

#import “ForeignStockOps.h”

    int main (int argc, const char * argv[])
    {
        @autoreleasepool {
            //The first two instances is from a class representing a domestic U.S. stock
            StockOps *Grain_stock = [[StockOps alloc] init];
            StockOps *NaturalGasStock = [[StockOps alloc] init];
            
            //The last class and its instance is a fictional Israeli stock representing this nation's success in watering the Negev Desert
            ForeignStockOps *Irrig_Equipmt_Stock = [[ForeignStockOps alloc] init];
            
            //These first two U.S. stocks are initialized with their prices, share amounts and catchy name
            
            [Grain_stock setPurchasePrice:2.30];
            [Grain_stock setCurrentPrice:4.50];
            [Grain_stock setNumberShares:40];
            [Grain_stock setStockName:"Midwest Wheat"];
        
            
            [NaturalGasStock setPurchasePrice:12.10];
            [NaturalGasStock setCurrentPrice:10.56];
            [NaturalGasStock setNumberShares:90];
            [NaturalGasStock setStockName:"Methane Miracle"];
        
            //This Israeli stock not only is initialized with the above entities, but also added is the current conversion rate from Israeli new shekels to U.S. dollars
            [Irrig_Equipmt_Stock setPurchasePrice:45.10];
            [Irrig_Equipmt_Stock setCurrentPrice:49.51];
            [Irrig_Equipmt_Stock setNumberShares:450];
            [Irrig_Equipmt_Stock setConversionRate:.249];
            [Irrig_Equipmt_Stock setStockName:"Negev Pipes"];
    
            
            int i = 1;
            
            
            NSMutableArray *calcStocks = [[NSMutableArray alloc] initWithObjects:Grain_stock, NaturalGasStock, Irrig_Equipmt_Stock, nil];
            for (StockOps *items in calcStocks)  {
                if (i == 3) { // when we reach the Israeli stock, the third entry, the wording is changed to reflect currency conversion; more specifically, the purchase price and share price are in Israeli currency, but then are converted to U.S dollars when declaring the final cost and value
                NSLog(@"The purchase share price of stock %i, %s, is %.2f new Israeli shekels", i, [items StockName],[items purchasePrice]);
                NSLog(@"%s' current share price is %.2f new Israeli shekels",[items StockName], [items currentPrice]);
                NSLog(@"The number of shares of this stock is %i", [items numberShares]);                    
                NSLog(@"Converted to U.S. dollars:The cost of %s is $%.2f", [items StockName], [items costInDollars]);
                NSLog(@"Converted to U.S. dollars:The value of %s is $%.2f", [items StockName], [items valueInDollars]);
                      }
                else {//i.e. the U.S. stocks only which are the first two members of the array
                    NSLog(@"The purchase share price of Stock %i, %s, is $%.2f", i, [items StockName],[items purchasePrice]);
                NSLog(@"%s's current share price is $%.2f",[items StockName], [items currentPrice]);
                NSLog(@"The number of shares of this stock is %i", [items numberShares]); 
                NSLog(@"The cost of %s is $%.2f", [items StockName], [items costInDollars]);
                NSLog(@"The value of %s is $%.2f", [items StockName], [items valueInDollars]);
                }
                
                i++;
            }
            
        }
        return 0;
    }

//
// ForeignStockOps.h
// Stock Calc 2
//
// Created by Nathan Annenberg on 9/2/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “StockOps.h”

@interface ForeignStockOps : StockOps

-(float) costInDollars;
-(float) valueInDollars;

@end

//
// ForeignStockOps.m
// Stock Calc 2
//
// Created by Nathan Annenberg on 9/2/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “ForeignStockOps.h”

@implementation ForeignStockOps

  • (float) costInDollars
    {

    float convert_amount = [super costInDollars];
    return convert_amount * conversionRate; //conversion rate stated in main.m
    }

  • (float) valueInDollars
    {

    float convert_amount = [super valueInDollars];
    return convert_amount * conversionRate;
    }

@end

//
// StockOps.h
// Stock Calc 2
//
// Created by Nathan Annenberg on 9/2/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockOps : NSObject {

float purchasePrice;
float currentPrice;
int numberShares;
char *StockName; //We need a char pointer to allow for a string re: the name of the stock
float conversionRate;

}

@property float purchasePrice;
@property float currentPrice;
@property int numberShares;
@property char *StockName;
@property float conversionRate;

  • (float) costInDollars; //purchasePrice * numberShares
  • (float) valueInDollars; //currentPrice * numberShares

@end

//
// StockOps.m
// Stock Calc 2
//
// Created by Nathan Annenberg on 9/2/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “StockOps.h”

@implementation StockOps

@synthesize purchasePrice, currentPrice, numberShares, conversionRate, StockName;

  • (float) costInDollars {

    float ns = [self numberShares];
    return [self purchasePrice] * ns;

}

  • (float) valueInDollars {

    float ns = [self numberShares];
    return [self currentPrice] * ns;

}

@end

2012-09-03 01:15:04.144 Stock Calc 2[87872:707] The purchase share price of Stock 1, Midwest Wheat, is $2.30
2012-09-03 01:15:04.145 Stock Calc 2[87872:707] Midwest Wheat’s current share price is $4.50
2012-09-03 01:15:04.146 Stock Calc 2[87872:707] The number of shares of this stock is 40
2012-09-03 01:15:04.147 Stock Calc 2[87872:707] The cost of Midwest Wheat is $92.00
2012-09-03 01:15:04.148 Stock Calc 2[87872:707] The value of Midwest Wheat is $180.00
2012-09-03 01:15:04.149 Stock Calc 2[87872:707] The purchase share price of Stock 2, Methane Miracle, is $12.10
2012-09-03 01:15:04.149 Stock Calc 2[87872:707] Methane Miracle’s current share price is $10.56
2012-09-03 01:15:04.150 Stock Calc 2[87872:707] The number of shares of this stock is 90
2012-09-03 01:15:04.151 Stock Calc 2[87872:707] The cost of Methane Miracle is $1089.00
2012-09-03 01:15:04.152 Stock Calc 2[87872:707] The value of Methane Miracle is $950.40
2012-09-03 01:15:04.154 Stock Calc 2[87872:707] The purchase share price of stock 3, Negev Pipes, is 45.10 new Israeli shekels
2012-09-03 01:15:04.155 Stock Calc 2[87872:707] Negev Pipes’s current share price is 49.51 new Israeli shekels
2012-09-03 01:15:04.156 Stock Calc 2[87872:707] The number of shares of this stock is 450
2012-09-03 01:15:04.158 Stock Calc 2[87872:707] Converted to U.S. dollars:The cost of Negev Pipes is $5053.46
2012-09-03 01:15:04.158 Stock Calc 2[87872:707] Converted to U.S. dollars:The value of Negev Pipes is $5547.60


#2

You can query an object to see what it is. The I==3 is dangerous. You have no guarantee that the order of stocks in the array will remain the same through later changes.

Use something like

if ([items isKindOfClass: [BNRForeignStock class]])
{
// do BNRForeignStock stuff
}
else if ([items isKindOfClass: [BNRStock class]])
{
// do BNRStock stuff
} 
else
{
// unknown object, leave it alone!
}

And no matter how the array is changed the functionality will remain intact.

There are several ways to inspect an object to determine the class and it’s hierarchy.