Creating an IF statement in a subclass - Is it possible?


#1

Is it possible to create an If statement within the ForeignStockHolding subclass? What I wanted to do was create an If statement that would check to see if my variable for the conversion rate was null (or 0), and would then set the conversion rate to 1.


#2

Ok - so this is my workaround for now. I included a bunch of If statements in Main.m that would look at the conversion rates and then correct ones that weren’t valid (in this instance, invalid = 0 or null). I’m still wondering if this type of action can be completed by code included in the subclass .h file.

[code]//
// main.m
// Stocks
//
// Created by Chris H on 1/23/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “ForeignStockHolding.h”

int main (int argc, const char * argv[])
{

@autoreleasepool {
    
    // insert code here...
    NSLog(@"Hello, World!");
    
    ForeignStockHolding *sHolder1 = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *sHolder2 = [[ForeignStockHolding alloc] init];
    StockHolding *sHolder3 = [[ForeignStockHolding alloc] init];
    
    [sHolder1 setPurchaseSharePrice:2.30]; 
    // "set" is a specific command and the following instance variable is case sensitive
    [sHolder1 setCurrentSharePrice:4.50];
    [sHolder1 setNumberOfShares:40];
    [sHolder1 setConversionRate:1.8];
    
    [sHolder2 setPurchaseSharePrice:12.19];
    [sHolder2 setCurrentSharePrice:10.56];
    [sHolder2 setNumberOfShares:90];
    [sHolder2 setConversionRate:1.3];
    
    [sHolder3 setPurchaseSharePrice:45.10];
    [sHolder3 setCurrentSharePrice:49.51];
    [sHolder3 setNumberOfShares:210];
    
    NSArray *sList = [NSArray arrayWithObjects:sHolder1, sHolder2, sHolder3, nil];

// The following code looks at the conversionRate to determine if it’s valid and prints out which sHolder has a valid rate
for (int i = 0; i < [sList count]; i++) {
if ([[sList objectAtIndex:i] conversionRate]) {
NSLog(@“sHolder%d has a valid conversionRate”, i + 1);
}
else {
NSLog(@“sHolder%d does not have a valid conversionRate”, i + 1);
}
}

    NSLog(@"-------");

// This statement then fixes the conversionRates that aren’t valid, and changes it/them to 1
for (int i = 0; i < [sList count]; i++) {
if ([[sList objectAtIndex:i] conversionRate] == 0) {
[[sList objectAtIndex:i] setConversionRate:1];
}
i++;
}

// Finally, this final round of statements prints out what the shares were bought at and their worth now, and then the conversion rate and newly measured convertedValue
for (int i = 0; i < [sList count]; i++) {

        NSLog(@"Group %d shares was bought at %f and is now worth %f", i + 1, [[sList objectAtIndex:i] costInDollars], [[sList objectAtIndex:i] valueInDollars]);
        NSLog(@"Group %d has a conversion rate of %f. It's value is measured at %f", i + 1, [[sList objectAtIndex:i] conversionRate], [[sList objectAtIndex:i] valueInDollars]);
    }     
}
return 0;

}

[/code]


#3

When you get to the chapter on initializers, you can override the initializer to set the conversion rate to 1.0 by default.

But for now, your solution looks fine.


#4

Interesting. Had the same thought of validating conversionRate. Here’s my approach, which seems to work:

[code]//
// main.m
// challenge8

#import <Foundation/Foundation.h>
#include “StockHolding.h”
#include “ForeignStockHolding.h”

#define NSLog(FORMAT, …) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##VA_ARGS] UTF8String]);
// above #define overrides/redefines NSLog to remove the timestamp and project address/title for formatting purposes.

int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSMutableArray *myArray = [[NSMutableArray alloc] init];
// Create an array and add self

    // Create an instance of Stockholding
    StockHolding *stock1 = [[StockHolding alloc] init];
    stock1.purchaseSharePrice = 2.30;
    stock1.currentSharePrice = 4.50;
    stock1.numberOfShares= 40;
    
    StockHolding *stock2 = [[StockHolding alloc] init];
    stock2.purchaseSharePrice = 12.19;
    stock2.currentSharePrice = 10.56;
    stock2.numberOfShares= 90; 
    
    StockHolding *stock3 = [[StockHolding alloc] init];
    stock3.purchaseSharePrice = 45.10;
    stock3.currentSharePrice = 49.51;
    stock3.numberOfShares= 210;
    
    ForeignStockHolding *foreignstock1 = [[ForeignStockHolding alloc] init];
    foreignstock1.purchaseSharePrice = 35.13;
    foreignstock1.currentSharePrice = 26.46;
    foreignstock1.numberOfShares= 246;
    foreignstock1.conversionRate = .88;
    
    ForeignStockHolding *foreignstock2 = [[ForeignStockHolding alloc] init];
    foreignstock2.purchaseSharePrice = 11.11;
    foreignstock2.currentSharePrice = 55.26;
    foreignstock2.numberOfShares= 134;
    foreignstock2.conversionRate = 1.76;
    
    [myArray addObject:stock1];
    [myArray addObject:stock2];
    [myArray addObject:stock3];
    [myArray addObject:foreignstock1];
    [myArray addObject:foreignstock2];
    
        for (StockHolding *s in myArray) 
        { 
            int k;
            k++;
            NSLog(@"===============================");
            NSLog(@"DATA SUMMARY FOR STOCK %d", k);
            NSLog(@"===============================");
            NSLog(@"Stock %d cost per:\t$%9.2f ", k, s.purchaseSharePrice);                
            NSLog(@"Stock %d price per:\t$%9.2f ", k, s.currentSharePrice);
            NSLog(@"_______________________________\n");
            NSLog(@"Shares of stock %d:%9d", k, s.numberOfShares);
            NSLog(@"Cost of stock %d:\t$%9.2f ", k, s.costInDollars);
            NSLog(@"Value  of stock %d:\t$%9.2f ",k, s.valueInDollars);
            if (s.conversionRate) 
            {
                NSLog(@"Conversion rate:\t%10.2f ", s.conversionRate);
            }
            NSLog(@"Profit or loss:\t\t$%9.2f ", s.profitInDollars);
            NSLog(@"_______________________________");
            NSLog(@"===============================\n\n");
        }
                                           
}

}[/code]


#5

Is there a specific reason to validate the conversion rate in main.m If all the math is done within each instance of ForeignStockHolding?