Challenge 19 Solution


#1

StockHolding.h

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

@interface StocHolding : NSObject {
unsigned int purchaseSharePrice;
unsigned int currentSharePrice;
unsigned int numberOfShares;
}
@property unsigned int purchaseSharePrice, currentSharePrice, numberOfShares;
-(float)costInDollars;
-(float)valueInDollars;
@end
[/code]

StockHolding.m

[code]#import “StocHolding.h”

@implementation StocHolding
@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

  • (float)costInDollars {
    return purchaseSharePrice*numberOfShares;
    }
  • (float)valueInDollars {
    return currentSharePrice*numberOfShares;
    }
    -(NSString *)description {
    return [NSString stringWithFormat:@“Cost: %.2f Value: %.2f”,[self costInDollars], [self valueInDollars]];
    }
    -(void)dealloc {
    NSLog(@"%@", self);
    }
    @end[/code]

Portfolio.h

#import <Foundation/Foundation.h> @class StocHolding; @interface Portfolio : NSObject { int NumberOfPorfolio; NSMutableArray *stockHolding; } @property int NumberOfPortfolio; -(void)addStockHoldingObject:(StocHolding *)a; -(unsigned int)valueOfPortfolio; @end

Portfolio.m

[code]#import “Portfolio.h”
#import “StocHolding.h”

@implementation Portfolio
@synthesize NumberOfPortfolio;
-(void)addStockHoldingObject:(StocHolding *)a {
if (!stockHolding) {
stockHolding = [[NSMutableArray alloc]init];
}
[stockHolding addObject:a];
}
-(unsigned int)valueOfPortfolio {
unsigned int sum =0;
for (StocHolding *a in stockHolding) {
sum += [a valueInDollars];
}
return sum;
}
-(NSString *)description {
return [NSString stringWithFormat:@“Portfolio %i Value:%d”, [self NumberOfPortfolio], [self valueOfPortfolio]];
}

  • (void)dealloc {
    NSLog(@"%@", self);
    }
    @end[/code]

main.m

[code]#import <Foundation/Foundation.h>
#import “StocHolding.h”
#import "Portfolio.h"
int main(int argc, const char * argv[])
{

@autoreleasepool {
    NSMutableArray *PortfolioList = [[NSMutableArray alloc]init];
    for (int i = 0; i<5; i++) {
        Portfolio *porfolio = [[Portfolio alloc]init];
        [porfolio setNumberOfPortfolio:i+1];
        [PortfolioList addObject];
    }
    for (int i = 0; i<20; i++) {
        StocHolding *stock = [[StocHolding alloc]init];
        [stock setPurchaseSharePrice:100*(i+1)];
        [stock setCurrentSharePrice:90*(i+1)];
        [stock setNumberOfShares:5*(i+1)];
        NSUInteger randomIndex = random() % [PortfolioList count];
        Portfolio *Rportfolio = [PortfolioList objectAtIndex:randomIndex];
        [Rportfolio addStockHoldingObject:stock];
    }
    NSLog(@"Porfilios: %@", PortfolioList);
    PortfolioList = nil;
}
return 0;

}[/code]

This is the output:

2013-07-30 18:54:58.063 Challenge 19 Object instance variable[717:303] Porfilios: (
“Portfolio 1 Value:229500”,
“Portfolio 2 Value:509850”,
“Portfolio 3 Value:314550”,
“Portfolio 4 Value:112950”,
“Portfolio 5 Value:124650”
)
2013-07-30 18:54:58.066 Challenge 19 Object instance variable[717:303] Portfolio 1 Value:229500
2013-07-30 18:54:58.066 Challenge 19 Object instance variable[717:303] Cost: 8000.00 Value: 7200.00
2013-07-30 18:54:58.067 Challenge 19 Object instance variable[717:303] Cost: 18000.00 Value: 16200.00
2013-07-30 18:54:58.067 Challenge 19 Object instance variable[717:303] Cost: 84500.00 Value: 76050.00
2013-07-30 18:54:58.068 Challenge 19 Object instance variable[717:303] Cost: 144500.00 Value: 130050.00
2013-07-30 18:54:58.068 Challenge 19 Object instance variable[717:303] Portfolio 2 Value:509850
2013-07-30 18:54:58.069 Challenge 19 Object instance variable[717:303] Cost: 2000.00 Value: 1800.00
2013-07-30 18:54:58.069 Challenge 19 Object instance variable[717:303] Cost: 24500.00 Value: 22050.00
2013-07-30 18:54:58.070 Challenge 19 Object instance variable[717:303] Cost: 50000.00 Value: 45000.00
2013-07-30 18:54:58.070 Challenge 19 Object instance variable[717:303] Cost: 128000.00 Value: 115200.00
2013-07-30 18:54:58.071 Challenge 19 Object instance variable[717:303] Cost: 162000.00 Value: 145800.00
2013-07-30 18:54:58.071 Challenge 19 Object instance variable[717:303] Cost: 200000.00 Value: 180000.00
2013-07-30 18:54:58.072 Challenge 19 Object instance variable[717:303] Portfolio 3 Value:314550
2013-07-30 18:54:58.072 Challenge 19 Object instance variable[717:303] Cost: 4500.00 Value: 4050.00
2013-07-30 18:54:58.073 Challenge 19 Object instance variable[717:303] Cost: 32000.00 Value: 28800.00
2013-07-30 18:54:58.073 Challenge 19 Object instance variable[717:303] Cost: 60500.00 Value: 54450.00
2013-07-30 18:54:58.074 Challenge 19 Object instance variable[717:303] Cost: 72000.00 Value: 64800.00
2013-07-30 18:54:58.074 Challenge 19 Object instance variable[717:303] Cost: 180500.00 Value: 162450.00
2013-07-30 18:54:58.075 Challenge 19 Object instance variable[717:303] Portfolio 4 Value:112950
2013-07-30 18:54:58.075 Challenge 19 Object instance variable[717:303] Cost: 500.00 Value: 450.00
2013-07-30 18:54:58.076 Challenge 19 Object instance variable[717:303] Cost: 12500.00 Value: 11250.00
2013-07-30 18:54:58.076 Challenge 19 Object instance variable[717:303] Cost: 112500.00 Value: 101250.00
2013-07-30 18:54:58.077 Challenge 19 Object instance variable[717:303] Portfolio 5 Value:124650
2013-07-30 18:54:58.077 Challenge 19 Object instance variable[717:303] Cost: 40500.00 Value: 36450.00
2013-07-30 18:54:58.077 Challenge 19 Object instance variable[717:303] Cost: 98000.00 Value: 88200.00