Chapter 19 program bmitime


#1

Im making the program in chapter 19 and it seems ready but its not working right.

My output is :

[quote]2013-07-15 19:59:15.071 BMITime[7984:303] Employees: (
"<Employee 0: $0 in assets>",
"<Employee 1: $153 in assets>",
"<Employee 2: $119 in assets>",
"<Employee 3: $68 in assets>",
"<Employee 4: $0 in assets>",
"<Employee 5: $136 in assets>",
"<Employee 6: $119 in assets>",
"<Employee 7: $34 in assets>",
"<Employee 8: $0 in assets>",
"<Employee 9: $136 in assets>"
)
2013-07-15 19:59:15.073 BMITime[7984:303] Giving up ownership of one employee
2013-07-15 19:59:15.073 BMITime[7984:303] Deallocating <Employee 5: $136 in assets>
(lldb) [/quote]

The Main.m is

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

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

@autoreleasepool {
    
    //create an array of employee objects
    NSMutableArray *employees = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++) {
        //create an instance of employee
        Employee *person = [[Employee alloc] init];
        
        //give the instance variables interesting values
        [person setWeightInKilos:90 + i];
        [person setHeightInMeters:1.8 - i/10.0];
        [person setEmployeeID:i];
        
        // put the employees in the employees array
        [employees addObject:person];
    }
    //create 10 assets
    for (int i = 0; i < 10; i++) {
        
        //create an asset
        Asset *asset = [[Asset alloc] init];
        
        //Give it an interesting Label
        NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
        [asset setLabel:currentLabel];
        [asset setResaleValue:i * 17];
        
        //get  a random number between 0 and 9 inclusive
        NSUInteger randomIndex = random() % [employees count];
        
        //find that employee
        Employee *randomEmployee = [employees objectAtIndex:randomIndex];
        
        //assign the asset to the employee
        [randomEmployee addAssetsObject:asset];
    }
    
    NSLog(@"Employees: %@", employees);
    
    NSLog(@"Giving up ownership of one employee");
    
    [employees removeObjectAtIndex:5];
    
    NSLog(@"Giving yp ownership of one employee");
    
    [employees removeObjectAtIndex:5];
    
    NSLog(@"Giving up ownership of array");
    employees = nil;
}
return 0;

}

[/code]

employee.h is

[code]#import <Foundation/Foundation.h>
#import “Person.h”
@class Asset;

@interface Employee : Person
{
int employeeID;
NSMutableArray *assets;
}
@property int employeeID;

  • (void)addAssetsObject: (Asset *)a;
  • (unsigned int)valueOfAssets;

@end[/code]

employee.m is

[code]#import “Employee.h”
#import “Asset.h”

@implementation Employee

@synthesize employeeID;

-(void)addAssetsObject:(Asset *) a
{
// Is asset null?
if (!assets) {
assets = [[NSMutableArray alloc] init];
}
[assets addObject:a];
}

-(unsigned int)valueOfAssets
{
// Sun up retale value
unsigned int sum = 0;
for (Asset *a in assets) {
sum += [a resaleValue];
}
return sum;
}

-(float)bodyMassIndex
{
float normalBMI = [super bodyMassIndex];
return normalBMI * 0.9;
}

-(NSString *)description
{
return [NSString stringWithFormat:@"<Employee %d: $%d in assets>", [self employeeID], [self valueOfAssets]];
}

-(void)dealloc
{
NSLog(@“Deallocating %@”, self);
}

@end
[/code]

Asset.h is

[code]#import <Foundation/Foundation.h>

@interface Asset : NSObject
{
NSString *label;
unsigned int resaleValue;
}
@property (strong) NSString *label;
@property unsigned int resaleValue;

@end
[/code]

asset.m is

[code]#import “Asset.h”

@implementation Asset

@synthesize label, resaleValue;

-(NSString *) description
{
return [NSString stringWithFormat:@"<%@", self];
}
-(void)dealloc
{
NSLog(@“Deallocating %@”, self);
}

@end
[/code]


#2

Whoops nvm found i had messed up in here: