My Most Current solution for Top Holdings and Sorted Holding


#1

main.m

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


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        BNRPortfolio *myStockPortfolio = [[BNRPortfolio alloc]init];
        BNRStockHolding *stockTXRH = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockGSAT = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockAAPL = [[BNRStockHolding alloc]init];
        BNRStockHolding *stockMEOH = [[BNRStockHolding alloc]init];
        
        // This array holds the top most valuable stocks
        NSArray *topHoldings;
        
        // This array holds stock holdings sorted alphabetically by symbol
        NSArray *sortedHoldings;
        
         // Made this stock foreign, although not really foreign
        BNRForeignStockHolding *stockZNGA = [[BNRForeignStockHolding alloc]init];
        
        
 /*******************************************************************/
        
        
        // Made this stock foreign, although not really foreign
        
        stockZNGA.companyName = (@"Zynga");
        stockZNGA.symbol = @"ZNGA";
        stockZNGA.purchaseSharePrice = 2.30;
        stockZNGA.currentSharePrice = 4.50;
        stockZNGA.numberOfShares = 40;
        stockZNGA.conversionRate = 0.94;
        [myStockPortfolio addStock:stockZNGA];
        
                
 /*******************************************************************/
 
        
        stockAAPL.companyName = (@"Apple");
        stockAAPL.symbol = @"AAPL";
        stockAAPL.purchaseSharePrice = 10.50;
        stockAAPL.currentSharePrice = 99.62;
        stockAAPL.numberOfShares = 500;
        [myStockPortfolio addStock:stockAAPL];
        
        
/*******************************************************************/
        
        stockGSAT.companyName = (@"GlobalStar");
        stockGSAT.symbol = @"GSAT";
        stockGSAT.purchaseSharePrice = 2.30;
        stockGSAT.currentSharePrice = 4.50;
        stockGSAT.numberOfShares = 40;
        [myStockPortfolio addStock:stockGSAT];
        
        
/*******************************************************************/
        
        stockTXRH.companyName = (@"Texas Roadhouse");
        stockTXRH.symbol = @"TXRH";
        stockTXRH.purchaseSharePrice = 26.62;
        stockTXRH.currentSharePrice = 28.31;
        stockTXRH.numberOfShares = 2556;
        [myStockPortfolio addStock:stockTXRH];
        
/*******************************************************************/
       
        stockMEOH.companyName = (@"Methanex Corporation");
        stockMEOH.symbol = @"MEOH";
        stockMEOH.purchaseSharePrice = 5.50;
        stockMEOH.currentSharePrice = 58.33;
        stockMEOH.numberOfShares = 1820;
        [myStockPortfolio addStock:stockMEOH];
        
        
/*******************************************************************/
        
        
        
        
        // Displays information for each stock in your portfolio.
        // Also displays the current value of the portfolio
        
        // Put a break here and Step Over for each line to test
        NSLog(@"%@", myStockPortfolio);
        
        // Put the top three valuable stocks in topHoldings array,
        // Step into method to see what happens line by line
        topHoldings = [myStockPortfolio topThreeStocks];
        
        // Stocks sorted alphabetically by symbol
        // Step into method to see what happens line by line
        sortedHoldings = [myStockPortfolio sortedHoldings];
        
        NSLog(@"\n*******************************************************************\nTop Three Most Valuable Stocks:\n");
        
        // Iterate through the array and log out information
        
        for (BNRStockHolding *stock in topHoldings)
        {
            NSLog(@"%@", stock);
        }
        
        
        NSLog(@"\n\n*******************************************************************\nSorted Alphabetically:\n");
        // Iterate through the array to make sure stocks print out alphabetized
        for (BNRStockHolding *sortedStocks in sortedHoldings)
        {
            NSLog(@"%@", sortedStocks);
        }
        NSLog(@"\n*******************************************************************");
       
        
        
 
        
        /**********************************************************************************************/
    // This section removes stocks from the portfolio
        
        [myStockPortfolio removeStock:stockTXRH];
        
        // Show the portfolio info again to see if stock is removed
        NSLog(@"%@", myStockPortfolio);
        
        
        // Remove stock
        [myStockPortfolio removeStock:stockGSAT];
        
        // Check portfolio again
        NSLog(@"%@", myStockPortfolio);
        ;
        
        // Remove stock
        [myStockPortfolio removeStock:stockAAPL];
        
        
        // Check portfolio again
        NSLog(@"%@", myStockPortfolio);
       
        
        // Remove stock
        [myStockPortfolio removeStock:stockZNGA];
        
        NSLog(@"%@", myStockPortfolio);
   
        
    }
 return 0;
}

BNRPortfolio.h

#import <Foundation/Foundation.h>
@class BNRStockHolding;

@interface BNRPortfolio : NSObject


- (float) valueOfPortfolio;
- (void) addStock:(BNRStockHolding *)stock;
- (void) removeStock:(BNRStockHolding *)stock;
- (NSArray *) topThreeStocks;
- (NSArray *)sortedHoldings;

@end

BNRPortfolio.m

#import "BNRPortfolio.h"
#import "BNRStockHolding.h"
#import "BNRForeignStockHolding.h"


// Class Extension
@interface BNRPortfolio ()

// Property is private.  Visible only within implementation file.
@property (nonatomic) NSMutableArray *holdings;

@end

@implementation BNRPortfolio


// Add stocks to the portfolio

- (void) addStock:(BNRStockHolding *)stock
{
    if (self.holdings == nil)
    {
        self.holdings = [[NSMutableArray alloc]init];
    }
    
    [self.holdings addObject:stock];
    
}

- (void) removeStock:(BNRStockHolding *) stock
{
    [self.holdings removeObject:stock];
    
}

- (float) valueOfPortfolio
{
    float sum = 0;
    
    for (BNRStockHolding *stock in self.holdings)
    {
        sum += [stock valueInDollars];
    }
    
    return sum;
}

- (NSString *) description
{
    
    NSLog(@"\nPortfolio Information:");
    
    for (BNRStockHolding *stock in self.holdings)
    {
        // Displays the custom descriptions for both
        // BNRStockHolding and BNRForeignStockholding
        NSLog(@"%@", stock);
        
        
    }
    
    return [NSString stringWithFormat:@"\nThe current value of your portfolio is: $%.2f", [self valueOfPortfolio]];
}

- (NSArray *)topThreeStocks
{
    
    // Create a Descriptor for the array,sort by valueInDollars
    NSSortDescriptor *topThree = [NSSortDescriptor sortDescriptorWithKey:@"valueInDollars" ascending:NO];
    
    // Apply the Descriptor to the holdings array, Descriptor sorts array by method valueInDollars
    [self.holdings sortUsingDescriptors:@[topThree]];
    
    // Create a new array from self.holdings array.
    // NSIndexSet is used to abstract the first 3 stocks from the holdings array
    
    NSArray *topStocks = [[NSArray alloc] initWithArray:
                        [self.holdings objectsAtIndexes:
                         [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)]]];
       
    
    return topStocks;
        
}

- (NSArray *)sortedHoldings
{
    // A Descriptor that sorts the array alphabetically by ticker symbol
    NSSortDescriptor *sortBySymbol = [NSSortDescriptor sortDescriptorWithKey:@"symbol" ascending:YES];
    
    // Apply the Descriptor to the holdings array, Descriptor sorts array by symbol
    [self.holdings sortUsingDescriptors:@[sortBySymbol]];
    
    
    
    return self.holdings;
}
    

 @end

BNRStockHolding.h

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject

@property (nonatomic) NSString *companyName;
@property (nonatomic) NSString *symbol;
@property (nonatomic) float purchaseSharePrice;
@property (nonatomic) float currentSharePrice;
@property (nonatomic) int   numberOfShares;


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

@end

BNRStockHolding.m

#import "BNRStockHolding.h"

@implementation BNRStockHolding

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

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

- (NSString *)description
{
    return [NSString stringWithFormat:@"\n\n      Company Name: %@\n      Stock Symbol: %@\n  Number of Shares: %d\nPurchase Share Price: %.2f\n Current Share Price: %.2f\n     Cost In Dollars: %.2f\n    Value in Dollars: %.2f\n",
            self.companyName, self.symbol, self.numberOfShares,
            self.purchaseSharePrice, self.currentSharePrice,
            [self costInDollars], [self valueInDollars]];
    
}

@end

BNRForeignStockHolding.h

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"

@interface BNRForeignStockHolding : BNRStockHolding

@property (nonatomic) float conversionRate;

@end

BNRForeignStockHolding.m

#import "BNRForeignStockHolding.h"

@implementation BNRForeignStockHolding


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

- (float) valueInDollars
{
    return (self.currentSharePrice * self.conversionRate) * self.numberOfShares;
}

// Description is overriden

- (NSString *)description
{
    return [NSString stringWithFormat:@"\n\n      Company Name: %@\n      Stock Symbol: %@\n  Number of Shares: %d\nPurchase Share Price: %.2f\n Current Share Price: %.2f\n     Cost In Dollars: %.2f\n    Value in Dollars: %.2f\n     Conversion Rate: %.2f\n",
          self.companyName, self.symbol, self.numberOfShares,
          (self.purchaseSharePrice * self.conversionRate) , (self.currentSharePrice * self.conversionRate),
          [self costInDollars], [self valueInDollars], self.conversionRate];
    
}

@end