I don't really know if this is correct but it seemed to work


#1

Please tell me how I could improve this program to be more efficient, fluent, and/or simpler.
Your advice is greatly appreciated.

Stockholding.h

[code]#import <Foundation/Foundation.h>
#import “Portfolio.h”

@interface StockHolding : Portfolio {
NSMutableArray *portfolios;
}

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

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

@property int stockHoldingID;

-(void)addPortfoliosObject:(Portfolio *)p;
-(float)valueOfPortfolios;

@end
[/code]

Stockholding.m

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

@implementation StockHolding

-(void)addPortfoliosObject:(Portfolio *)p {
// if portfolios nil
if (!portfolios) {
portfolios = [[NSMutableArray alloc] init];
}
[portfolios addObject:p];
}

-(float)valueOfPortfolios {
// sum up the value of stockholdings
float sum = 0;

for (Portfolio *p in portfolios) {
    sum += [p currentValue];
}
return sum;

}

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

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

@end[/code]

Portfolio.h

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

@interface Portfolio : NSObject

@property (strong) NSString *label;
@property float currentValue;

@end[/code]

Portfolio.m

#import "Portfolio.h"

@implementation Portfolio

-(NSString *)description {
    return [NSString stringWithFormat:@"< Current value of %@, $%f >", [self label], [self currentValue]];
}

-(void)dealloc {
    NSLog(@"deallocating: %@", self);
}

@end

main.m

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “Portfolio.h”

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

@autoreleasepool {
    
    // create an array of Stockholding objects
    NSMutableArray *stockholdings = [[NSMutableArray alloc] init];
    
    
    for (int i = 0; i < 10; i++) {
        // create an instance of Stockholding
        StockHolding *stocks = [[StockHolding alloc] init];
        
        // Give the instance variables interesting values
        [stocks setCurrentSharePrice:5 + i];
        [stocks setPurchaseSharePrice:10 + i];
        [stocks setNumberOfShares:(10*i) + 10];
        [stocks setStockHoldingID:i];
        [stocks setCurrentValue:[stocks valueInDollars]];
        
        // Give it an interesting label
        NSString *currentLabel = [NSString stringWithFormat:@"Portfolio: %d", i];
        [stocks setLabel:currentLabel];
        
        
        // Put the stocks in the stockholdings array
        [stockholdings addObject:stocks];
        [stocks setCurrentValue:[stocks valueInDollars]];

        
        NSLog(@"Portfolio holds stock %@, with current value %.2f", [stocks label], [stocks valueInDollars]);
    }

    
    NSLog(@"Stockholdings: %@", stockholdings);
    
    NSLog(@"Giving up ownership of one stockholding");
    
    [stockholdings removeObjectAtIndex:5];
    
    NSLog(@"Giving up ownership of array");
    
    stockholdings = nil;
    
    
}
return 0;

}[/code]


#2

I wonder: it seems the program is not using the methods you created in stockholding class, regarding array *portfolios.
In the output, do you get output of the total value of the portfolio? Or only the values of each separate stock?