开发者

Objective C:Cant understand the logic of looping

I am new to Objective C. Currently, I am trying out some examples in Objective C. I am not getting the correct output. I could not understand the logic behind the correct output which I have included here.

main.m

#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"

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

        Budget* europeBudget=[Budget new];
        NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10];

        [europeBudget createBudget:1000.00 withExchangeRate:1.2500];

        Transaction* aTransaction;
        aTransaction = [Transaction new];
        for(int n=1;n<2;n++){

                [aTransaction createTransaction:n*100 ofType:credit];
                [transactions addObject:aTransaction];

        }

        int n=1;
        while (n<3) {

                [aTransaction createTransaction:n*100 ofType:cash];
                [transactions addObject:aTransaction];
                n++;
        }

        do{
                [aTransaction createTransaction:n*100 ofType:credit];
                [transactions addObject:aTransaction];
                n++;
        }while (n<=3);



        for(Transaction *aaTransaction in transactions){
         switch ([aTransaction returnType]) {
                case cash:
                    [europeBudget spendDollars:[aaTransaction returnAmount]];
                    break;
                case credit:
                    [europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
                    break;
                default:
                    break;
         }
        }


        return 0;
}

BudObj.m

#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"

@implementation Budget

- (void) createBudget: (开发者_Go百科double) aBudget withExchangeRate: (float) anExchangeRate{
        budget = aBudget;
        exchangeRate = anExchangeRate;
}


- (void) spendDollars:(double)dollars{
    budget = budget - dollars;
        NSLog(@"Converting %0.2f US Dollars into Foreign Currency leaves $%0.2f",dollars,budget);
}

- (void) changeForeignCurrency:(double)foreignCurrency{
    exchangeTransaction = foreignCurrency * exchangeRate;
        budget = budget - exchangeTransaction;
        NSLog(@"Charging %0.2f in Foreign Currency leaves $%0.2f",foreignCurrency,budget);
}

@end

BudObj.h

#import <Foundation/Foundation.h>

@interface 
Budget : NSObject {

        float  exchangeRate;
        double budget;
        double exchangeTransaction;

}

- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
- (void) spendDollars: (double) dollars;
- (void) changeForeignCurrency: (double) foreignCurrency;

@end

Transaction.h

#import <Cocoa/Cocoa.h>

typedef enum{cash,credit} transactionType;

@interface Transaction : NSObject {

        transactionType type;
        double amount;

}

-(void)createTransaction:(double)theAmount ofType:(transactionType)theType;
-(double)returnAmount;
-(transactionType)returnType;

@end

Transaction.m

#import "Transaction.h"

@implementation Transaction

-(void)createTransaction:(double)theAmount ofType:(transactionType)theType{

        type=theType;
        amount=theAmount;

}

-(double)returnAmount{

        return amount;

}

-(transactionType)returnType{

        return type;

}

@end

OUTPUT:

2011-04-10 17:31:28.717 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $625.00
2011-04-10 17:31:28.719 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $250.00
2011-04-10 17:31:28.720 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $-125.00
2011-04-10 17:31:28.720 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $-500.00

But the expected output is

Converting 100.00 US dollars into foreign currency leaves $900.00
Charging 100.00 in foreign currency leaves $775.00
Charging 200.00 in foreign currency leaves $525.00
Charging 300.00 in foreign currency leaves $150.00
Converting 100.00 US dollars into foreign currency leaves $1900.00
Charging 100.00 in foreign currency leaves $1750.00


Your ultimate problem is you are using the same transaction object. You set aTransaction once on this line and never set it anywhere else. I do see from your comments it was once right though.

Transaction* aTransaction;
aTransaction = [Transaction new];

When you add the the transaction to the array all of them point to the same object and what ever change you make to what you are thinking is 1 aTransaction is really changing them all. So when it comes time to print you have multiple references to the same transaction all of type credit and you keep sending the same return amount.

Also remember that indexes are usually 0 based and int n = 1; may not get you the correct number elements you are looking for.

To achieve your expected output with as close to your orignal code as I can try this. (may need some tweaking)

#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"

int main(int argc, char *argv[]) {
        Budget* europeBudget=[[Budget alloc] init];

        [europeBudget createBudget:1000.00 withExchangeRate:1.2500];

        Transaction* aTransaction;
        for(int n=1;n<2;n++){

                aTransaction = [[Transaction alloc] init];
                [aTransaction createTransaction:n*100 ofType:cash];
                [transactions addObject:aTransaction];
                [aTransaction release];
        }

        int n=0;
        while (n<3) {
                aTransaction = [[Transaction alloc] init];
                [aTransaction createTransaction:n*100 ofType:credit];
                [transactions addObject:aTransaction];
                [aTransaction release];
                n++;
        }
        //n = 3 at this point
        do{
                aTransaction = [[Transaction alloc] init];
                [aTransaction createTransaction:n*100 ofType:cash];
                [transactions addObject:aTransaction];
                [aTransaction release];
                n++;
        }while (n<=3);

        //n = 4 at this point
        aTransaction = [[Transaction alloc] init];
        [aTransaction createTransaction:n*100 ofType:credit];
        [transactions addObject:aTransaction];
        [aTransaction release];

        for(Transaction *aaTransaction in transactions){
         switch ([aTransaction returnType]) {
                case cash:
                        [europeBudget spendDollars:[aaTransaction returnAmount]];
                        break;
                case credit:
                        [europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
                        break;
                default:
                        break;
         }
        }
        [transactions release];
        [europeBudget release];
        return 0;
}

Now I did not check that your dollar amounts are correct but this should print out the correct order of Converting/Charging. I also didn't assume there was Garbage Collection so I just added all the releases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜