Inheritance Challenge


#1

Hi every one, i’d like you to write me a message if the code is very well done or not with the best practices that you recommend. Thanks.

[code]BNRForeignStockHolding.h

#import <Foundation/Foundation.h>
#import “BNRStockHolding.h”

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOFShare

@end[/code]

[code]BNRForeignStockHolding.h

#import “BNRForeignStockHolding.h”

@implementation BNRForeignStockHolding

  • (float)costInDollars{
    if(self.conversionRate){
    return [self purchaseSharePrice] * [self conversionRate];
    }else{
    return [self purchaseSharePrice] * [self numberOfShares];
    }

}

  • (float)valueInDollars{
    if(self.conversionRate){
    return [self currentSharePrice] * [self conversionRate];
    }else{
    return [self currentSharePrice] * [self numberOfShares];
    }
    }

@end[/code]

[code]BNRStockHolding.h
#import “BNRStockHolding.h”

@implementation BNRStockHolding

  • (float)costInDollars
    {
    return [self purchaseSharePrice] * [self numberOfShares];
    }

  • (float)valueInDollars
    {
    return [self currentSharePrice] * [self numberOfShares];
    }

@end[/code]

[code]BNRStockHolding.m
#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOFShare

@end[/code]

[code]main.m
#import <Foundation/Foundation.h>
//#import “BNRStockHolding.h”

#import “BNRForeignStockHolding.h”

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

@autoreleasepool {
    
    BNRForeignStockHolding *almacen1 = [[BNRForeignStockHolding alloc] init];
    BNRForeignStockHolding *almacen2 = [[BNRForeignStockHolding alloc] init];
    BNRForeignStockHolding *almacen4 = [[BNRForeignStockHolding alloc] init];
    
    // Mount of products on almacen1
    [almacen1 setPurchaseSharePrice:2.30];
    [almacen1 setCurrentSharePrice:4.50];
    [almacen1 setNumberOfShares:40];
    [almacen1 costInDollars];
    [almacen1 numberOfShares];
    
    // Mount of productos on almacen2
    [almacen2 setPurchaseSharePrice:2.30];
    [almacen2 setCurrentSharePrice:4.50];
    [almacen2 setNumberOfShares:40];
    [almacen2 costInDollars];
    [almacen2 numberOfShares];
    
    [almacen4 setPurchaseSharePrice:2.30];
    [almacen4 setCurrentSharePrice:4.50];
    [almacen4 setNumberOfShares:40];
    [almacen4 setConversionRate:0.94];
    [almacen4 costInDollars];
    [almacen4 numberOfShares];
    
    
    NSMutableArray *temp = [NSMutableArray arrayWithObjects:almacen1, almacen2, almacen4, nil];
    
    for(BNRForeignStockHolding *s in temp){
        float a = [s purchaseSharePrice];
        float b = [s currentSharePrice];
        int c = [s numberOfShares];
        float d = [s costInDollars];
        float e = [s valueInDollars];
        float f = [s conversionRate];
        
        if (f){
            NSLog(@"\n Purchase Prices: %.2f \n Current Share Price: %.2f \n Number Shares: %d \n Cost in dollars: %.2f \n Values in dollars: %.2f \n conversion %.2f", a, b, c, d, e, f);
        }else{
            NSLog(@"\n Purchase Prices: %.2f \n Current Share Price: %.2f \n Number Shares: %d \n Cost in dollars: %.2f \n Values in dollars: %.2f", a, b, c, d, e);
        }
        
    }
    
}
return 0;

}[/code]


#2

You could use more informative variables names inside the for loop. It is hard to tell what those names a, b, c, etc. signify.

Also, next time, please post your code between the code tags:

// Like this

#3

Hi,

There is a lot that is unnecessary in your code. I started to reply to each piece but actually I think it will be better to just share my solution. Not saying that my approach is 100% the “best” way but I think you will find it much more in line with the ideas taught. Pay attention to what is “included” as you have some unnecessary stuff there. You also have unnecessary stuff in the array and you aren’t using the override as intended along with the functionality that your BNR superlcass already provides.

BNRStockHolding.h

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int numberOfShares;

-(float)costInDollars;
-(float)valueIndollars;

@end

BNRStockHolding.m

#import "BNRStockHolding.h"

@implementation BNRStockHolding

-(float)costInDollars
{
    return self.purchaseSharePrice * self.numberOfShares;
}

-(float)valueIndollars
{
    return self.currentSharePrice * self.numberOfShares;
}
@end

BNRForeignStockHolding.h

#import "BNRStockHolding.h"

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;

@end

BNRForeignStockHolding.m

#import "BNRForeignStockHolding.h"

@implementation BNRForeignStockHolding


-(float)costInDollars
{
    return [super costInDollars] * self.conversionRate;
}

-(float)valueIndollars
{
    return [super valueIndollars] * self.conversionRate;
}

@end

main.m

#import <Foundation/Foundation.h>
#include "BNRForeignStockHolding.h"

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

    @autoreleasepool {
        
        BNRStockHolding *holding1 = [[BNRStockHolding alloc]init];
        BNRStockHolding *holding2 = [[BNRStockHolding alloc]init];
        BNRStockHolding *holding3 = [[BNRStockHolding alloc]init];
        BNRForeignStockHolding *fHolding1 = [[BNRForeignStockHolding alloc]init];
        BNRForeignStockHolding *fHolding2 = [[BNRForeignStockHolding alloc]init];
        
        
        [holding1 setPurchaseSharePrice:2.30];
        [holding1 setCurrentSharePrice:4.50];
        [holding1 setNumberOfShares:40];
        
        [holding2 setPurchaseSharePrice:12.19];
        [holding2 setCurrentSharePrice:10.56];
        [holding2 setNumberOfShares:90];
        
        [holding3 setPurchaseSharePrice:45.10];
        [holding3 setCurrentSharePrice:49.51];
        [holding3 setNumberOfShares:210];
        
        [fHolding1 setPurchaseSharePrice:2.30];
        [fHolding1 setCurrentSharePrice:4.50];
        [fHolding1 setNumberOfShares:40];
        [fHolding1 setConversionRate:0.94];
        
        [fHolding2 setPurchaseSharePrice:12.19];
        [fHolding2 setCurrentSharePrice:10.56];
        [fHolding2 setNumberOfShares:90];
        [fHolding2 setConversionRate:0.50];
        
        NSArray *stockArray = @[holding1,holding2,holding3,fHolding1,fHolding2];
        
        for (BNRStockHolding *x in stockArray)
        {
            float value = [x valueIndollars] - [x costInDollars];
            
            NSLog(@"Stock holding is worth $%.2f",value);
        }
        
        
    }
    return 0;
}

#4

Hi how are you, thanks for you replay, and i’ll do the changes to improve my code.


#5

What is the “nonatomic” assignment? It isn’t covered in the book.


#6

Nonatomic essentially means the property is not thread safe. If you are not writing a program that takes advantage of multi-threading then using nonatomic will be faster. Atomic is the default behavior which is why you need to write nonatomic each time.


#7

Nonatomic essentially means the property is not thread safe. If you are not writing a program that takes advantage of multi-threading then using nonatomic will be faster. Atomic is the default behavior which is why you need to write nonatomic each time.[/quote]

Thanks for clarifying.

Is it necessary to include this in the solution for the challenge, or do you think it serves for the most part to obscure the lesson by complicating the solution?


#8

Nonatomic essentially means the property is not thread safe. If you are not writing a program that takes advantage of multi-threading then using nonatomic will be faster. Atomic is the default behavior which is why you need to write nonatomic each time.[/quote]

Thanks for clarifying.

Is it necessary to include this in the solution for the challenge, or do you think it serves for the most part to obscure the lesson by complicating the solution?[/quote]

Using nonatomic should be standard practice and not excluded. In fact you will run into some issues if you don’t include it when you should. It is only one word and you should get used to writing it each time regardless of the challenge, lesson, etc.