Stuck, any help?


#1

main.m

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

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

    @autoreleasepool {
        
        BNRPortfolio *p = [[BNRPortfolio alloc] init];
        
        NSMutableArray *stocks = [[NSMutableArray alloc] init];
        
        BNRStockHolding *person1 = [[BNRStockHolding alloc] init];
        person1.purchasedSharePrice = 2.30;
        person1.currentSharePrice = 4.50;
        person1.numberOfShares = 40;
        person1.symbol = @"ABC";
        [p addHolding:person1];
        [stocks addObject:person1];
        
        BNRStockHolding *person2 = [[BNRStockHolding alloc] init];
        person2.purchasedSharePrice = 2.30;
        person2.currentSharePrice = 4.50;
        person2.numberOfShares = 40;
        person2.symbol = @"DEF";
        [p addHolding:person2];

        [stocks addObject:person2];
        
        BNRForeignStockHolding *person3 = [[BNRForeignStockHolding alloc] init];
        person3.purchasedSharePrice = 2.30;
        person3.currentSharePrice = 4.50;
        person3.numberOfShares = 40;
        person3.conversionRate = 0.94;
        person3.symbol = @"GHI";
        [p addHolding:person3];

        [stocks addObject:person2];
        [stocks addObject:person3];
        
        
        for (BNRStockHolding *a in stocks) {
            float cost = [a costInDollars];
            float value = [a valueInDollars];
            NSString *symbo = [a symbol];
            NSLog(@"Cost: %f, And Value: %f with symbol: %@", cost, value, symbo);
            
        }
        
        
        NSLog(@"Adding All Portfolios: %@", [p sort]);

//        [p removeHolding:person2];        
//        NSLog(@"Removing Persone2 from Portfolios list and now the %@", p);
    }
    return 0;
}

BNRStockHolding.m

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

@class BNRPortfolio;
@implementation BNRStockHolding

  • (float)purchasedSharePrice
    {
    return _purchasedSharePrice;
    }

  • (void)setPurchasedSharePrice:(float)p
    {
    _purchasedSharePrice = p;
    }

  • (float)currentSharePrice
    {
    return _currentSharePrice;
    }

  • (void)setCurrentSharePrice:(float)c
    {
    _currentSharePrice = c;
    }

  • (int)numberOfShares
    {
    return _numberOfShares;
    }

  • (void)setNumberOfShares:(int)n
    {
    _numberOfShares = n;
    }

  • (float)costInDollars
    {
    float p = [self purchasedSharePrice];
    int n = [self numberOfShares];
    return p * n;
    }

  • (float)valueInDollars
    {
    float c = [self currentSharePrice];
    int n = [self numberOfShares];
    return c * n;
    }

@end
[/code]

BNRForeignStockHolding.m

[code]#import “BNRForeignStockHolding.h”

@implementation BNRForeignStockHolding

  • (float)costInDollars
    {
    float conCost = [super costInDollars];
    float rate = [self conversionRate];
    return conCost * rate;
    }

-(float)valueInDollars
{
float conValue = [super valueInDollars];
float rate = [self conversionRate];
return conValue * rate;
}

@end
[/code]

BNRPortfolio.m

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

@interface BNRPortfolio ()

{
NSMutableArray *_holdings;
}

@end

@implementation BNRPortfolio

  • (void)setHoldings:(NSArray *)holdings
    {
    _holdings = [holdings mutableCopy];
    }

-(NSArray *)holdings
{
return [_holdings copy];
}

-(void)addHolding:(BNRStockHolding *)stock
{
if (!_holdings) {
_holdings = [[NSMutableArray alloc] init];
}
[_holdings addObject:stock];
}

-(float)totalValue
{
float sum = 0.0;
for (BNRStockHolding *stock in _holdings) {
sum += [stock valueInDollars];
}
return sum;
}

-(NSArray *)sort
{
NSMutableArray *arrai = [[NSMutableArray alloc] init];
NSSortDescriptor *sortValue = [NSSortDescriptor sortDescriptorWithKey:@“symbol” ascending:YES];
[arrai sortUsingDescriptors:@[sortValue]];
return arrai;
}

-(NSString *)description
{
return [NSString stringWithFormat:@“total value: %f with sorted array: %@”, self.totalValue, self.sort];
}
@end[/code]

What should i do am not getting the sorted array ?


#2

[quote]-(NSArray *)sort { NSMutableArray *arrai = [[NSMutableArray alloc] init]; NSSortDescriptor *sortValue = [NSSortDescriptor sortDescriptorWithKey:@"symbol" ascending:YES]; [arrai sortUsingDescriptors:@[sortValue]]; return arrai; }
What should i do am not getting the sorted array ?
[/quote]
You are only sorting an empty array! Sort the array that has the stocks in it.


#3

[quote=“ibex10”][quote]-(NSArray *)sort { NSMutableArray *arrai = [[NSMutableArray alloc] init]; NSSortDescriptor *sortValue = [NSSortDescriptor sortDescriptorWithKey:@"symbol" ascending:YES]; [arrai sortUsingDescriptors:@[sortValue]]; return arrai; }
What should i do am not getting the sorted array ?
[/quote]
You are only sorting an empty array! Sort the array that has the stocks in it.[/quote]

My bad!, thanks for clearing that out.