diff --git a/ContaPessoas/Contador.h b/ContaPessoas/Contador.h index 18b7c16..14f8e9c 100644 --- a/ContaPessoas/Contador.h +++ b/ContaPessoas/Contador.h @@ -6,13 +6,16 @@ // Copyright (c) 2015 Vinicius Miana. All rights reserved. // -@interface Contador : NSObject +@interface Contador : NSObject - (void)maisUmCueca; - (void)maisUmaGata; -(int)getBoys; -(int)getGirls; +-(int)getTotal; +-(id)init; ++(Contador *) instancia; @end diff --git a/ContaPessoas/Contador.m b/ContaPessoas/Contador.m index fa64ad8..0972ffd 100644 --- a/ContaPessoas/Contador.m +++ b/ContaPessoas/Contador.m @@ -1,7 +1,7 @@ // // Contador.m // ContaPessoas -// +// Sem comentários // Created by Vinicius Miana on 2/18/15. // Copyright (c) 2015 Vinicius Miana. All rights reserved. // @@ -11,12 +11,19 @@ @implementation Contador { - int boy; - int girl; + int boy; + int girl; + } +static Contador *_instancia; + ++(Contador *) instancia{ + if(_instancia == nil){ + _instancia = [[Contador alloc] init]; + } + return _instancia; } -(id)init { - self = [super init]; if (self) { boy = 0; girl = 0; @@ -39,6 +46,10 @@ -(int)getGirls { return girl; } +-(int)getTotal{ + return girl + boy; +} + @end diff --git a/ContaPessoas/FirstViewController.m b/ContaPessoas/FirstViewController.m index fc70dd5..128e95a 100644 --- a/ContaPessoas/FirstViewController.m +++ b/ContaPessoas/FirstViewController.m @@ -19,7 +19,7 @@ @implementation FirstViewController { - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador instancia]; // Do any additional setup after loading the view, typically from a nib. } diff --git a/ContaPessoas/SecondViewController.m b/ContaPessoas/SecondViewController.m index a2a66f5..15e9bb0 100644 --- a/ContaPessoas/SecondViewController.m +++ b/ContaPessoas/SecondViewController.m @@ -19,9 +19,12 @@ @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; - contador = [[Contador alloc] init]; + contador = [Contador instancia]; } +- (void)viewDidAppear:(BOOL)animated{ + [self click:self]; +} - (void)didReceiveMemoryWarning { @@ -32,7 +35,7 @@ - (void)didReceiveMemoryWarning { - (IBAction)click:(id)sender { _totalBoys.text = [NSString stringWithFormat: @"%d", [contador getBoys]]; _totalGirls.text = [NSString stringWithFormat: @"%d", [contador getGirls]]; - _total.text = [NSString stringWithFormat:@"%d", [contador getGirls] + [contador getBoys] ]; + _total.text = [NSString stringWithFormat:@"%d", [contador getTotal]]; } diff --git a/ContaPessoasTests/ContaPessoasTests.m b/ContaPessoasTests/ContaPessoasTests.m index 579bb3f..ae83307 100644 --- a/ContaPessoasTests/ContaPessoasTests.m +++ b/ContaPessoasTests/ContaPessoasTests.m @@ -21,6 +21,7 @@ - (void)testContaMeninos { Contador *c = [[Contador alloc] init]; [c maisUmCueca]; XCTAssert(([c getBoys] == 1), @"Pass"); + XCTAssert(([c getGirls] == 0), @"Pass"); } - (void)testContaMeninas { @@ -30,5 +31,12 @@ - (void)testContaMeninas { XCTAssert(([c getBoys] == 0), @"Pass"); } +- (void)testContaTotal{ + Contador *c = [[Contador alloc] init]; + [c maisUmaGata]; + [c maisUmCueca]; + + XCTAssert(([c getTotal] == 2), @"Pass"); +} @end diff --git a/ContaPessoasTests/ContadorTests.m b/ContaPessoasTests/ContadorTests.m index 67c06ac..5151fbd 100644 --- a/ContaPessoasTests/ContadorTests.m +++ b/ContaPessoasTests/ContadorTests.m @@ -37,4 +37,5 @@ - (void)testPerformanceExample { }]; } + @end