Skip to content

Instantly share code, notes, and snippets.

@tmshv
Created November 12, 2013 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmshv/7424110 to your computer and use it in GitHub Desktop.
Save tmshv/7424110 to your computer and use it in GitHub Desktop.
Hexlet:: OS X #2, 10 http://hexlet.org/lesson/osx_2_10/
//
// City.h
// Hexlet Metropolis
//
// Created by Roman Timashev on 10/11/13.
// Copyright (c) 2013 Roman Timashev's Company. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface City : NSObject{
NSString* name;
int age;
int population;
}
-(void) setName:(NSString*) cityName age:(int) cityAge population:(int) cityPopulation;
-(NSString*) getName;
-(int) getAge;
-(int) getPopulation;
-(void) nextDay;
@end
//
// City.m
// Hexlet Metropolis
//
// Created by Roman Timashev on 10/11/13.
// Copyright (c) 2013 Roman Timashev's Company. All rights reserved.
//
#import "City.h"
@implementation City{
}
-(void) setName:(NSString *)cityName age:(int)cityAge population:(int)cityPopulation{
name = cityName;
age = cityAge;
population = cityPopulation;
}
-(NSString*) getName{
return name;
}
-(int) getAge{
return age;
}
-(int) getPopulation{
return population;
}
-(void) nextDay{
population += arc4random_uniform(population * 0.1);
population -= arc4random_uniform(population * 0.1);
}
@end
//
// main.m
// Hexlet Metropolis
//
// Created by Roman Timashev on 10/11/13.
// Copyright (c) 2013 Roman Timashev's Company. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "City.h"
#import "Metropolis.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
City *city = [[City alloc] init];
[city setName:@"Vice City" age:300 population:500000];
for(int i=0; i<10; i++){
[city nextDay];
NSLog(@"%@ population %i", [city getName], [city getPopulation]);
}
Metropolis *polis = [[Metropolis alloc] init];
[polis createCity:@"Saint Petersburg" withPopulation:5500000 atIndex:0];
[polis createCity:@"Pushkin" withPopulation:100000 atIndex:1];
[polis createCity:@"Cronshtadt" withPopulation:50000 atIndex:2];
[polis createCity:@"Gatchina" withPopulation:60000 atIndex:3];
[polis createCity:@"Pavlovsk" withPopulation:30000 atIndex:4];
[polis createCity:@"Petergoph" withPopulation:15000 atIndex:5];
[polis createCity:@"Shlisselburg" withPopulation:15000 atIndex:6];
[polis createCity:@"Vigorg" withPopulation:100000 atIndex:7];
[polis createCity:@"Kuzmolovo" withPopulation:3000 atIndex:8];
[polis createCity:@"Vsevolozhsk" withPopulation:25000 atIndex:9];
for(int i=0; i<[polis numberOfCities]; i++){
[[polis cityAtIndex:i] nextDay];
City *city = [polis cityAtIndex:i];
NSLog(@"%@ population %i", [city getName], [city getPopulation]);
}
}
return 0;
}
//
// Metropolis.h
// Hexlet Metropolis
//
// Created by Roman Timashev on 10/11/13.
// Copyright (c) 2013 Roman Timashev's Company. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "City.h"
@interface Metropolis : NSObject
-(void) createCity: (NSString*) name withPopulation: (int) population atIndex: (int) index;
-(City*) cityAtIndex:(int) index;
-(int) numberOfCities;
@end
//
// Metropolis.m
// Hexlet Metropolis
//
// Created by Roman Timashev on 10/11/13.
// Copyright (c) 2013 Roman Timashev's Company. All rights reserved.
//
#import "Metropolis.h"
@implementation Metropolis{
NSMutableArray *cities;
}
-(id) init{
self = [super init];
cities = [[NSMutableArray alloc] initWithCapacity:10];
return self;
}
-(void) createCity:(NSString *)name withPopulation:(int)population atIndex:(int)index{
City *city = [[City alloc] init];
[city setName:name age:0 population:population];
[cities insertObject:city atIndex:index];
}
-(City*) cityAtIndex:(int)index{
return cities[index];
}
-(int) numberOfCities{
return (int)[cities count];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment