main.m
//
// main.m
// BMITime
//
// Created by Adam G. on 5/24/14.
//
//
#import <Foundation/Foundation.h>
#import "BNREmployee.h"
#import "BNRAsset.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Create an array of BNREmployee objects
NSMutableArray *employeesArray = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++) {
// Create an instance of BNREmployee
BNREmployee *adam = [[BNREmployee alloc]init];
//Assign a first name to each Employee object
switch (i) {
case 0:
adam.firstName = @"Adam";
break;
case 1:
adam.firstName = @"Melony";
break;
case 2:
adam.firstName = @"Bryan";
break;
case 3:
adam.firstName = @"Gina";
break;
case 4:
adam.firstName = @"Natali";
break;
case 5:
adam.firstName = @"Snow";
break;
case 6:
adam.firstName = @"Aaron";
break;
case 7:
adam.firstName = @"Doug";
break;
case 8:
adam.firstName = @"Belinda";
break;
case 9:
adam.firstName = @"Helene";
break;
default:
break;
}
// Give the instance variables interesting values
adam.weightInKilos = 90 + i;
adam.heightInMeters = 1.8 - i/10.0;
adam.employeeId = i;
// Put each Adam object into the employees NSMutableArray
[employeesArray addObject:adam];
}
// create 10 assets
for (int i; i < 10; i++) {
// Create an assets
BNRAsset *asset = [[BNRAsset alloc] init];
// Get a random number between 0 and 9 inclusive
NSUInteger randomIndex = random() % [employeesArray count];
// Find that employee
BNREmployee *randomEmployee = [employeesArray objectAtIndex:randomIndex];
// Created a more meaningful label which includes the Employee's
NSString *currentLabel = [NSString stringWithFormat:@"%@'s Laptop. Laptop ID %d", randomEmployee.firstName, i];
asset.label = currentLabel;
asset.resaleValue = 350 + i * 17;
// Assign the asset to the employee
[randomEmployee addAsset:asset];
}
NSLog(@"Employees: %@", employeesArray);
// Remove 10 assets
for (int i = 0; i < 15; i++ ) {
// Get a random number between 0 and 9
NSUInteger randomIndex2 = random() % [employeesArray count];
// Get a random employee from the array
BNREmployee *randomEmployee2 = [employeesArray objectAtIndex:randomIndex2];
// Remove the assets from the employee
[randomEmployee2 removeAsset];
}
}
return 0;
}
BNREmployee.m
//
// BNREmployee.m
// BMITime
//
// Created by Adam G on 5/24/14.
//
//
#import "BNREmployee.h"
#import "BNRAsset.h"
@implementation BNREmployee
// Accessors for assets properties
- (void)setAssets:(NSArray *)a
{
_assets = [a mutableCopy];
}
- (NSArray *)assets
{
return [_assets copy];
}
- (void)addAsset:(BNRAsset *)a
{
// Is assets nil?
if (!_assets) {
// Create the array
_assets = [[NSMutableArray alloc]init];
}
[_assets addObject:a];
}
// Remove Asset Method
- (void)removeAsset
{
// Count how many _assets the person owns.
// Assign value to i then go through the switch/case statement
// The dealloc method will fire once the BNRAsset object is removed from the array _assets array.
// The BNRAsset dealloc method displays a NSLog message like
// "deallocating BNRAsset <Adam's Laptop. Laptop ID 3: $401>"
for (NSUInteger i = [_assets count]; i > 0; i--) {
switch (i) {
case 1:
[_assets removeObjectAtIndex:i - 1];
break;
case 2:
[_assets removeObjectAtIndex:i - 1];
break;
case 3:
[_assets removeObjectAtIndex:i - 1];
default:
[_assets removeObjectAtIndex:i - 1];
break;
}
}
}
- (unsigned int)valueOfAssets;
{
// Sum up the resale value of the assets
unsigned int sum = 0;
for (BNRAsset *a in _assets) {
sum += [a resaleValue];
}
return sum;
}
- (double)yearsOfEmployement
{
// Do I have a non-nil hireDate?
if (self.hireDate) {
// NSTimeIntervval is the same as double
NSDate *now = [NSDate date];
NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate];
return secs / 31557600.0; // Seconds per year
}
else {
return 0;
}
}
- (float)bodyMassIndex
{
// return 19.0;
float normalBMI = [super bodyMassIndex];
NSLog(@"NormalBMI is: %.2f", normalBMI);
return normalBMI * 0.9;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<Employee ID: %d Name: %@ owns $%d in assets>",
self.employeeId, self.firstName, [self valueOfAssets]];
}
- (void)dealloc
{
NSLog(@"\n\rdeallocating BNREmployee %@", self);
}
@end