Completed Challenge, but how to make it simpler?


#1

I think i did this right but it seems like i could be shortening something to make it easier…

MAIN

[code]#import “StockHolding.h”

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

@autoreleasepool {
    
    //Create 3 Instance of StockHolding
    StockHolding *stockholding1 = [[StockHolding alloc] init];
    StockHolding *stockholding2 = [[StockHolding alloc] init];
    StockHolding *stockholding3 = [[StockHolding alloc] init];
    
    //Give the instance variables values for 1
    [stockholding1 setPurchaseSharePrice:2.30];
    [stockholding1 setCurrentSharePrice:4.50];
    [stockholding1 setNumberOfShares:40];
    
    //Give the instance variables values for 2
    [stockholding2 setPurchaseSharePrice:12.19];
    [stockholding2 setCurrentSharePrice:10.56];
    [stockholding2 setNumberOfShares:90];
    
    //Give the instance variables values for 3
    [stockholding3 setPurchaseSharePrice:45.10];
    [stockholding3 setCurrentSharePrice:49.51];
    [stockholding3 setNumberOfShares:210];
    
    //Declare the NSMutableArray for the three sets of stocks
    NSMutableArray * stocks = [[NSMutableArray alloc] init];
    
    [stocks addObject:stockholding1];
    [stocks addObject:stockholding2];
    [stocks addObject:stockholding3];
    
    for(StockHolding *n in stocks)
    {
    
    
    //Call the methods 
    float cost = [n costInDollars];
    float value = [n valueInDollars];
    NSLog(@"Bought stock for $%.2f, It is now at $%.2f, I have %d shares, They cost me $%.2f, Now they are worth $%.2f",
          [n purchaseSharePrice], [n currentSharePrice], [n numberOfShares], cost, value);
        
        
    }
    
    
    
}
return 0;

}[/code]

Stockholding.h

[code]// The class StockHoldinf inherits all the instance variables
//and methods defined by the class NSObject
@interface StockHolding : NSObject
{
//It has three instance variables
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}
//We are now replacing the decleration of the setter and getter methods with the @property
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

//These are the two methods for your calculation

  • (float)costInDollars; // purchaseSharePrice * numberOfShares
  • (float)valueInDollars; // currentSharePrice * numberOfShares
    @end
    [/code]

Stockholding.m

[code]@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

  • (float)costInDollars
    {
    return purchaseSharePrice * numberOfShares;
    }
  • (float)valueInDollars
    {
    return currentSharePrice * numberOfShares;
    }
    @end
    [/code]

And this is what it printed to the console

2012-02-09 00:49:50.034 Stocks[2362:707] Bought stock for $2.30, It is now at $4.50, I have 40 shares, They cost me $92.00, Now they are worth $180.00 2012-02-09 00:49:50.036 Stocks[2362:707] Bought stock for $12.19, It is now at $10.56, I have 90 shares, They cost me $1097.10, Now they are worth $950.40 2012-02-09 00:49:50.036 Stocks[2362:707] Bought stock for $45.10, It is now at $49.51, I have 210 shares, They cost me $9471.00, Now they are worth $10397.10

Any ideas on how to simplify would be greatly appreciated, thanks.


#2

I think that solution looks great. I wouldn’t simplify it at all.


#3

Oh ok good, this is my first time ever programming so i get confused from time to time. Thanks


#4

Surplusman,
Thank you for your post. I successfully completed this task and only did it using one set of values that I set for cost, value, and number of shares. When I saw your code it reminded me that I needed to do three, and that I should practice the knowledge we learned earlier about arrays! So I modified mine. Thanks!


#5

When you add the objects to the array you could initWithObjects?


#6

Quick question about solutions that I didn’t want to start a new thread for.

It seems that you only get the converted rate of final prices, the prices you bought and sold for were not converted, is that correct?


#7

[quote=“LucasKA”]Quick question about solutions that I didn’t want to start a new thread for.

It seems that you only get the converted rate of final prices, the prices you bought and sold for were not converted, is that correct?[/quote]

This is correct… but there is nothing stopping you from converting the other prices… you should get the same answer in the end


#8

I have a very similar solution and only saved two lines by removing the assignments to cost and value and referencing the method directly in NSLog. It does make the NSLog variables a bit more standardized looking. :slight_smile: