0

The code is supposed to be a basic objc program that contains a list of purchased stocks, foreign and domestic. Right now, I have to put them into a portfolio and sum it up. I realized that I could not sum them up because I was getting an error with my BNRForeign header and implementation files. For the rest of the code, please see here (my other question that was answered): Trying to create a portfolio of stocks with a total value in Objective-C, but unsure of the approach

So I'm working on some code for a stock problem from a book that I'm reading, which was partly answered in another question I posted yesterday. I have an entirely new question branching from that now.

For my if condition, I want to iterate through an array, but I'm not sure how to implement this.

My main.m:

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

        BNRStockHolding *stock0 = [[BNRStockHolding alloc]init];
        BNRStockHolding *stock1 = [[BNRStockHolding alloc]init];
        BNRStockHolding *stock2 = [[BNRStockHolding alloc]init];
        BNRForeignStockHolding *stock3 = [[BNRForeignStockHolding alloc]init];

        stock0.purchaseSharePrice=2.30;
        stock0.currentSharePrice=4.50;
        stock0.numberOfShares=40;

        stock1.purchaseSharePrice=12.19;
        stock1.currentSharePrice=10.56;
        stock1.numberOfShares=90;

        stock2.purchaseSharePrice=45.10;
        stock2.currentSharePrice=49.51;
        stock2.numberOfShares=210;

        stock3.purchaseSharePrice=43.05;
        stock3.currentSharePrice=28.31;
        stock3.numberOfShares=15;

        NSMutableArray *stocks = [NSMutableArray arrayWithObjects:stock0, stock1, stock2, stock3, nil];
        for (BNRForeignStockHolding *s in stocks) {
            float a = s.purchaseSharePrice;
            float b = s.currentSharePrice;
            int c = s.numberOfShares;
            float d = s.costInDollars;
            float e = s.valueInDollars;
            float f = s.foreignCostInDollars;
            float g = s.foreignValueInDollars;

            if () {
                NSLog(@"\n Purchase Price: %.2f\n Current Price: %.2f\n Number of Shares %d\n Cost in Dollars %f\n Value in Dollars %f\n", a, b, c, d, e);
            }
            else {
                NSLog(@"\n Purchase Price: %.2f\n Current Price: %.2f\n Number of Shares %d\n Cost in Dollars %f\n Value in Dollars %f\n", a, b, c, f, g);
            }

            }
    }
    return 0;
}

Basically, for stock0, stock1, and stock2, I need NSLog to apply functions a, b, c, d, e. For stock3, I need NSLog to apply functions a, b ,c, f, g.

I tried things like this:

if (stock(i), i=0, i<=2) {

NSLog(@"etc", a, b, c, d, e);
}
else {
NSLog(@"etc", a, b, c, f, g);
}

Xcode keeps stating that stock(i) and i are undefined implicit functions and that they are undeclared. I understand this, but since I am new to programming, I am unsure of how to define them so that I may iterate through my array.

Thanks for your help!

Community
  • 1
  • 1
Yistorian
  • 49
  • 10

2 Answers2

0

Your for has to look like that:

    for (BNRStockHolding * s in stocks) {
      //Common assingments
      if([s isKindOfClass:[BNRForeignStockHolding class]]){
          BNRForeignStockHolding * aux = (BNRForeignStockHolding*)s;
          float f = aux.foreignCostInDollars;
          float g = aux.foreignValueInDollars;
          NSLog(@"\n Purchase Price: %.2f\n Current Price: %.2f\n Number of Shares %d\n Cost in Dollars %f\n Value in Dollars %f\n", a, b, c, d, e);
      } else if([s isKindOfClass:[BNRStockHolding class]]){
          NSLog(@"\n Purchase Price: %.2f\n Current Price: %.2f\n Number of Shares %d\n Cost in Dollars %f\n Value in Dollars %f\n", a, b, c, f, g);
      }
   }
peig
  • 418
  • 3
  • 11
  • Thx, I'll try that. I'm new to objective-c and Xcode, which has its own quirks. – Yistorian Feb 16 '15 at 08:51
  • okay, it partially worked like my other attempts...one problem though: Xcode builds successfully, but it throws some runtime errors such as "uncaught exception at "memory address" and NSException." I'm going through the functions in the Xcode docs, but I'm having a hard time finding something that will work well. I wouldn't write the program like this, but the book has this as a challenge and I'm wondering if the set-up is just poor because it seems like the best option at this point would be to merge BNRForeign and BNRStock into one, remove the if/else, and just separate the NSLog print outs. – Yistorian Feb 16 '15 at 08:53
  • Okay, right now, I'm getting a runtime error on float f = foreignCostInDollars; that states Thread 1 SIGABRT. – Yistorian Feb 16 '15 at 08:59
  • There are a problem in you "for" you are telling that every class is BNRForeignStockHolding but most of them are BNRStockHolding. – peig Feb 16 '15 at 09:01
  • I figured that may be an issue, but I am unsure of how to include all the stocks into one array as they are from different classes. I know this may sound like a dumb question, but I'm new to objective-c and its syntax confuses me at times. I figured since my BNRForeign implementation file is a subclass of the BNRStock header, that calling the BNRForeign would also call the BNRStock functions since the BNRForeign stocks are simply stocks with an additional calculation (conversion rate), but otherwise treated the same way. The code works well, but is there a better way to write this array? – Yistorian Feb 16 '15 at 09:18
  • The array is quite ok (it doesn't have to be mutable but it is ok). The problem is that is that the main class doesn't have the subclass objects because it is not this class.... You can do the for with the main class instead the subclass and then ask if s isKindOfClass the subclass, do a casting and use subclass variables and methods. – peig Feb 16 '15 at 09:30
0

Generalising, your question is not really "do something for stock3" but rather "do something based on whether the stock is an instance of BNRForeignStockHolding or not".

When you have an instance of a class you can test if it is a particular class using the method isMemberOfClass: (note: there is a related method isKindOfClass: which tests for a being a class or any of its subclasses).

So you can test if s is an instance of BNRForeignStockHolding using:

[s isMemberOf:[BNRForeignStockHolding class]]

Using this you can rewrite your code to use just two variables, d and e, setting them according to the type of s:

float d, e;
if ([s isMemberOf:[BNRForeignStockHolding class]])
{
   // BNRForeignStockHolding
   d = s.foreignCostInDollars;
   e = s.foreignValueInDollars;
}
else
{
   // BNRStockHolding
   d = s.costInDollars;
   e = s.valueInDollars;
}

and now you can just use the variables a, b, c, d and e and it does not matter which, if any, of your stocks are foreign the code will work.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Thank You so much, this worked and solved my problem! It's exactly what I wanted to do, but could not figure out how. I honestly did not know that I could define e, d twice, but not have a conflict. Now I know that I can separate definitions within an if/else function. Thanks for the thorough explanation, as well as the example. The code is clean and not verbose at all. – Yistorian Feb 16 '15 at 09:13
  • Sorry there was a typo, fixed. You only declare the variables `d` and `e` once, but assign them different values according to the conditional test. – CRD Feb 16 '15 at 09:36
  • No problem, I caught the typo when I was testing the code...xcode freaked out and I realized what happened, lol. Thx again. – Yistorian Feb 16 '15 at 09:38
  • If you have the time, could you take a look at my other question related to the same code, I'm having a hard time with Xcode and my header files, thx again. It won't accept one header as a subclass of another header even though I have imported the other header file: http://stackoverflow.com/questions/28522441/trying-to-create-a-portfolio-of-stocks-with-a-total-value-in-objective-c-but-un – Yistorian Feb 16 '15 at 09:47