Challenge 19 Complete


#1

I stole/borrowed code/ideas from several members. Here is a complete solution to challenge 19. I am sure there are better and more efficient solutions.

Stock.h

[code]//
// StockHolding.h

#import <Foundation/Foundation.h>

// The Class StockHolding inherits all the instance variables
// and methods defined by the class NSObject

@interface StockHolding : NSObject

{
//It has 3 instance variables

float purchaseSharePrice, currentSharePrice;
int numberOfShares;

}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

//These 2 method calculate cost in Dollars and Value in Dollars

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

@end
[/code]

Stock.m

[code] //
// StockHolding.m

#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice,currentSharePrice,numberOfShares;

  • (float)costInDollars
    {
    return [self purchaseSharePrice]*[self numberOfShares]; //I did not have to use self here… just wanted to try self out
    }
  • (float)valueInDollars
    {
    return currentSharePrice*numberOfShares; //I could have used self here… did not feel like using self
    }
    @end
    [/code]
    Foreign Stock.h

[code]//
// ForeignStockHolding.h

#import <Foundation/Foundation.h>
#import “StockHolding.h” //notice we added #import StockHolding

@interface ForeignStockHolding : StockHolding // notice we added : StockHolding instead of NSObject

{
float conversionRate;
}

@property float conversionRate;

@end
[/code]

ForeignStock.m

[code]//
// ForeignStockHolding.h

#import <Foundation/Foundation.h>
#import “StockHolding.h” //notice we added #import StockHolding

@interface ForeignStockHolding : StockHolding // notice we added : StockHolding instead of NSObject

{
float conversionRate;
}

@property float conversionRate;

@end
[/code]

Portfolio.h

[code]//
// Portfolio.h

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

@interface Portfolio : NSObject

{
float currentValue;
NSMutableArray *assets;
}

  • (float) currentValue;
  • (void) addAsset: (StockHolding *) a;

@end[/code]

main.m

[code]//
// main.m

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

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

@autoreleasepool {
    
    
    // create an instance of Portfolio
    
    Portfolio *myPortfolio = [[Portfolio alloc] init];
    
    // create an array to hold each postion and the data related to each position ...one  two three four
    NSMutableArray *holdings=[[NSMutableArray alloc] init];
    
    //create 2 instances of stockholding and 
    StockHolding *one = [[StockHolding alloc] init];
    StockHolding *two = [[StockHolding alloc] init];
    
    //  create 2 instances of foreign stock holding
    ForeignStockHolding *three = [[ForeignStockHolding alloc] init];
    ForeignStockHolding *four = [[ForeignStockHolding alloc] init];
    
    //set values for stockholding instances
    [one setPurchaseSharePrice:10];
    [one setCurrentSharePrice:25];
    [one setNumberOfShares:1000];
    [two setPurchaseSharePrice:20];
    [two setCurrentSharePrice:50];
    [two setNumberOfShares:1000];
    //set values for foreignStockholding instances
    [three setPurchaseSharePrice:30];
    [three setCurrentSharePrice:60];
    [three setNumberOfShares:1000];
    [three setConversionRate: 1.5];
    [four setPurchaseSharePrice:50];
    [four setCurrentSharePrice:90];
    [four setNumberOfShares:1000];
    [four setConversionRate: 1.5];
    
    
    //fill in with instances of stockholding
    [holdings addObject:one];
    [holdings addObject:two];
    [holdings addObject:three];
    [holdings addObject:four];
    
    
    // add stocks to portfolio
    for (StockHolding *a in holdings)
    {
        [myPortfolio addAsset: a];
    }
    
    float totalValue = 0;
    //Iterate through the array and printout data
    for (StockHolding *thisStock in holdings)
    {
        totalValue = (totalValue + [thisStock valueInDollars]);
        NSLog(@"This stock has %d shares purchased for $%.2f each at a cost of $%.2f", [thisStock numberOfShares], [thisStock purchaseSharePrice], [thisStock costInDollars]);
        NSLog(@"It's current share price is $%.2f and is currently worth $%.2f.", [thisStock currentSharePrice], [thisStock valueInDollars]);
        
    }
    
    NSLog(@"The current value of my holdings is %.2f.", totalValue);
    NSLog(@"The current value of my portfolio is %.2f.", [myPortfolio currentValue]);//should be equal to my holdings
    
    
    
}
return 0;

}

[/code]


#2

Thanks for the walkthrough and great comments.
However I can see that you’ve missed adding portfolio.m, and I’m stuck on how defining addAsset in that file. :stuck_out_tongue_winking_eye:


#3

Here is my solution to Portfolio.m:

[code]#import “Portfolio.h”

@implementation Portfolio

@synthesize currentValue;
  • (void) addAsset: (StockHolding *) a
    {
    //is asset nil?
    if (!assets){
    assets = [[NSMutableArray alloc] init];
    }
    [assets addObject:a];
    }

@end
[/code]

Do you have something similar?