Displaying output in "method" instead of main


#1

Hi guys,
i just finished my challenge and start wondering what is the best way to print the information. I wrote a method for the Portfolio class “displayPortfolio” where I’m adding my stock values and printing the result. Is this a correct objective-c way or not? Or should I instead move the output into the main?

Here are my code:

#import "Portfolio.h"
#import "StockHolding.h"

@implementation Portfolio
@synthesize stocks, portfolioName;

-(void) displayPortfolio{
    
    float total; //the total value of this portfolio
    
    for(StockHolding *s in [self stocks])
    {
        
        NSLog(@"Company %@ has a value of %.2f",[s companyName], [s valueInDollars]);
        total += [s valueInDollars]; //sum the values
        
    }
    
    NSLog(@"The %@ Portfolio has a total value of %.2f",[self portfolioName], total);
    
}

-(void) addStockToPortfolio:(StockHolding *)s
{
    //There are no stock array yet so create a new one
    if(!stocks){
        stocks = [[NSMutableArray alloc]init];
    }
    
    //add stock to list
    [stocks addObject:s];
}


@end
#import <Foundation/Foundation.h>
#import "ForeignStockHolding.h"
#import "Portfolio.h"

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

    @autoreleasepool {
        
        //create three stocks
        StockHolding *apple = [[StockHolding alloc]init];
        StockHolding *microsoft = [[StockHolding alloc]init];
        StockHolding *ibm = [[StockHolding alloc]init];
        
        ForeignStockHolding *appleGer = [[ForeignStockHolding alloc] init];
        
        
        //set values
        [apple setPurchaseSharePrice:2.30];
        [apple setCurrentSharePrice:4.50];
        [apple setNumberOfShares:40];
        [apple setCompanyName:@"Apple"];
        
        [appleGer setPurchaseSharePrice:2.30];
        [appleGer setCurrentSharePrice:4.50];
        [appleGer setNumberOfShares:40];
        [appleGer setConversionRate:1.50];
        [appleGer setCompanyName:@"Apple Germany"];
        
        
        [microsoft setPurchaseSharePrice:12.19];
        [microsoft setCurrentSharePrice:10.56];
        [microsoft setNumberOfShares:90];
        [microsoft setCompanyName:@"Microsoft"];
        
        [ibm setPurchaseSharePrice:45.10];
        [ibm setCurrentSharePrice:49.51];
        [ibm setNumberOfShares:210];
        [ibm setCompanyName:@"IBM"];
        
        //create a portfolio
        Portfolio *port = [[Portfolio alloc]init];
        
        //add stocks to portfolio
        [port addStockToPortfolio:apple];
        [port addStockToPortfolio];
        [port addStockToPortfolio:microsoft];
        [port addStockToPortfolio:ibm];
        
        //give portfolio a name
        [port setPortfolioName:@"IT Companies"];
        
        //show portfolio
        [port displayPortfolio];
        
        
    }
    return 0;
}

Cheers!


#2

I think you’re on the right track. I would suggest that instead of displaying anything from the class, you turn that into a description method that generates the same string and returns it; then, you could invoke NSLog() from main().

In a larger environment where this code would have a lifespan greater than a few days, you wouldn’t want a data management class to be writing output; you’d want to be able to take that string and display it in a GUI, or write it to a log, or send it to another server, or…


#3

Ok thanks for your suggestion. I changed my code now i return the total value via a portfolio function and print the data in the main.

#import <Foundation/Foundation.h>
#import "ForeignStockHolding.h"
#import "Portfolio.h"

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

    @autoreleasepool {
        
        //create three stocks
        StockHolding *apple = [[StockHolding alloc]init];
        StockHolding *microsoft = [[StockHolding alloc]init];
        StockHolding *ibm = [[StockHolding alloc]init];
        
        ForeignStockHolding *appleGer = [[ForeignStockHolding alloc] init];
        
        
        //set values
        [apple setPurchaseSharePrice:2.30];
        [apple setCurrentSharePrice:4.50];
        [apple setNumberOfShares:40];
        [apple setCompanyName:@"Apple"];
        
        [appleGer setPurchaseSharePrice:2.30];
        [appleGer setCurrentSharePrice:4.50];
        [appleGer setNumberOfShares:40];
        [appleGer setConversionRate:1.50];
        [appleGer setCompanyName:@"Apple Germany"];
        
        
        [microsoft setPurchaseSharePrice:12.19];
        [microsoft setCurrentSharePrice:10.56];
        [microsoft setNumberOfShares:90];
        [microsoft setCompanyName:@"Microsoft"];
        
        [ibm setPurchaseSharePrice:45.10];
        [ibm setCurrentSharePrice:49.51];
        [ibm setNumberOfShares:210];
        [ibm setCompanyName:@"IBM"];
        
        //create a portfolio
        Portfolio *port = [[Portfolio alloc]init];
        
        //add stocks to portfolio
        [port addStockToPortfolio:apple];
        [port addStockToPortfolio];
        [port addStockToPortfolio:microsoft];
        [port addStockToPortfolio:ibm];
        
        //give portfolio a name
        [port setPortfolioName:@"IT Companies"];
        
        for(StockHolding *s in [port stocks])
        {
            NSLog(@"The company %@ has a value of %.2f:",[s companyName], [s valueInDollars]);
        }
        
        NSLog(@"The Portfolio %@ has a value of %.2f", [port portfolioName], [port getTotalValue]);
        
        
        
    }
    return 0;
}