Is this the correct and best way to do?


#1

Hello!

Like most of us here, I am new to Objective-C and I am having a hard time to get the all _variable, setVariable, etc stuff.
I try to follow the example from the book, I also want to check what other people wrote here but… I dont want to copy/paste a code from someone just because its working… I NEED to understand! Please Help.

My .h file

#import <Foundation/Foundation.h>

@interface BNRStockHolding : NSObject
{
    float _currentSharePrice;
    float _purchaseSharePrice;
    int _numberOfShares;
}

-(float)currentSharePrice;
-(void)setCurrentSharePrice:(float)csp;
-(float)purchaseSharePrice;
-(void)setPurchaseSharePrice:(float)psp;
-(int)numberOfShares;
-(void)setNumberOfShares:(int)nos;

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

@end

My .m file

#import "BNRStockHolding.h"

@implementation BNRStockHolding

-(float)currentSharePrice
{
    return _currentSharePrice;
}

-(void)setCurrentSharePrice:(float)csp
{
    _currentSharePrice = csp;
}

-(float)purchaseSharePrice
{
    return _purchaseSharePrice;
}

-(void)setPurchaseSharePrice:(float)psp
{
    _purchaseSharePrice = psp;
}

-(int)numberOfShares
{
    return _numberOfShares;
}

-(void)setNumberOfShares:(int)nos
{
    _numberOfShares = nos;
}

-(float)costInDollars
{
    float psp = [self purchaseSharePrice];
    int nos = [self numberOfShares];
    return psp * nos;  // purchaseSharePrice * numberOfShares
}

-(float)valueInDollars
{
    float csp = [self currentSharePrice];
    int nos = [self numberOfShares];
    return csp * nos; // currentSharePrice * numberOfShares
}

@end

After that, I know there is the .main() to work on but there… I have no idea how to do it either…
:cry: :cry: :cry: :cry: :cry:

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

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

@autoreleasepool {
    
    NSMutableArray *dowJones = [[NSMutableArray alloc] init];
    
    
    NSMutableArray *nasdaq = [[NSMutableArray alloc] init];
    
    
    NSMutableArray *sp = [[NSMutableArray alloc] init];
}
return 0;

}

[/code]

Thank you for your help!


#2

I suggest you go back and review the BNRPerson class example from earlier in the chapter when working in main.m.

Look at the line where an instance of BNRPerson is created.

“BNRPerson *mikey = [[BNRPerson alloc] init]”

Hint: you will need 3 instances of the BNRStockHoldings, one for each stock.

Good luck!


#3

Judging by your code in main, it seems you might not be grasping the concept. In your code, you are creating three arrays; you only need one. We aren’t supposed to create 3 stock markets, we’re supposed to create 3 stocks. Create an empty object for each stock then fill each stock with its properties. Each stock has a price, current price, etc. (these are the stocks’ properties). Once the stocks are created, create an array and store each stock in the array. Go through the array using a for loop and display each of the properties of the stocks plus use the methods available for each stock to display the calculated values.

There are plenty of code examples in this forum you can review. I just thought I might explain it in a way that might make better sense. If it’s confusing and needs clarification, let me know.