From 364409db20730309638ef0d959dc3bbdd3c733bc Mon Sep 17 00:00:00 2001 From: David Spivak Date: Thu, 6 Dec 2012 10:23:43 -0500 Subject: [PATCH 1/3] Initial commit (to verify github connection) --- CashReegister/ViewController.m | 1 + 1 file changed, 1 insertion(+) diff --git a/CashReegister/ViewController.m b/CashReegister/ViewController.m index 274f7b0..d2689cf 100644 --- a/CashReegister/ViewController.m +++ b/CashReegister/ViewController.m @@ -16,6 +16,7 @@ @implementation ViewController - (void)viewDidLoad { + //if you can see this, initial commit worked [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } From a06e5985ec972a8f5a52ca24cd245486a9d04432 Mon Sep 17 00:00:00 2001 From: David Spivak Date: Thu, 6 Dec 2012 15:13:11 -0500 Subject: [PATCH 2/3] Updated Everything --- CashReegister.xcodeproj/project.pbxproj | 8 ++ CashReegister/Currency.h | 15 +++ CashReegister/Currency.m | 15 +++ CashReegister/ViewController.h | 9 +- CashReegister/ViewController.m | 113 ++++++++++++++++++ .../en.lproj/MainStoryboard.storyboard | 53 +++++++- 6 files changed, 207 insertions(+), 6 deletions(-) create mode 100644 CashReegister/Currency.h create mode 100644 CashReegister/Currency.m diff --git a/CashReegister.xcodeproj/project.pbxproj b/CashReegister.xcodeproj/project.pbxproj index 6eb688d..fee313c 100644 --- a/CashReegister.xcodeproj/project.pbxproj +++ b/CashReegister.xcodeproj/project.pbxproj @@ -20,6 +20,7 @@ 4EA04E8115E5212500021A01 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4EA04E6215E5212500021A01 /* Foundation.framework */; }; 4EA04E8915E5212500021A01 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4EA04E8715E5212500021A01 /* InfoPlist.strings */; }; 4EA04E8C15E5212500021A01 /* CashReegisterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4EA04E8B15E5212500021A01 /* CashReegisterTests.m */; }; + 5811C1EB1670F43F00BF93DB /* Currency.m in Sources */ = {isa = PBXBuildFile; fileRef = 5811C1EA1670F43F00BF93DB /* Currency.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -52,6 +53,8 @@ 4EA04E8815E5212500021A01 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 4EA04E8A15E5212500021A01 /* CashReegisterTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CashReegisterTests.h; sourceTree = ""; }; 4EA04E8B15E5212500021A01 /* CashReegisterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CashReegisterTests.m; sourceTree = ""; }; + 5811C1E91670F43F00BF93DB /* Currency.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Currency.h; sourceTree = ""; }; + 5811C1EA1670F43F00BF93DB /* Currency.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Currency.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -116,6 +119,8 @@ 4EA04E7215E5212500021A01 /* MainStoryboard.storyboard */, 4EA04E7515E5212500021A01 /* ViewController.h */, 4EA04E7615E5212500021A01 /* ViewController.m */, + 5811C1E91670F43F00BF93DB /* Currency.h */, + 5811C1EA1670F43F00BF93DB /* Currency.m */, 4EA04E6715E5212500021A01 /* Supporting Files */, ); path = CashReegister; @@ -261,6 +266,7 @@ 4EA04E6D15E5212500021A01 /* main.m in Sources */, 4EA04E7115E5212500021A01 /* AppDelegate.m in Sources */, 4EA04E7715E5212500021A01 /* ViewController.m in Sources */, + 5811C1EB1670F43F00BF93DB /* Currency.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -434,6 +440,7 @@ 4EA04E9115E5212500021A01 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 4EA04E9215E5212500021A01 /* Build configuration list for PBXNativeTarget "CashReegisterTests" */ = { isa = XCConfigurationList; @@ -442,6 +449,7 @@ 4EA04E9415E5212500021A01 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/CashReegister/Currency.h b/CashReegister/Currency.h new file mode 100644 index 0000000..b233f5b --- /dev/null +++ b/CashReegister/Currency.h @@ -0,0 +1,15 @@ +// +// Currency.h +// CashReegister +// +// Created by Brandon Salazar on 12/6/12. +// Copyright (c) 2012 Dealermatch. All rights reserved. +// + +#import + +@interface Currency : NSObject +@property(strong,nonatomic)NSNumber *value; +@property(strong,nonatomic)NSString *name; + +@end diff --git a/CashReegister/Currency.m b/CashReegister/Currency.m new file mode 100644 index 0000000..09e8ccf --- /dev/null +++ b/CashReegister/Currency.m @@ -0,0 +1,15 @@ +// +// Currency.m +// CashReegister +// +// Created by Brandon Salazar on 12/6/12. +// Copyright (c) 2012 Dealermatch. All rights reserved. +// + +#import "Currency.h" + +@implementation Currency +@synthesize value; +@synthesize name; + +@end diff --git a/CashReegister/ViewController.h b/CashReegister/ViewController.h index 1da168b..f3c3d72 100644 --- a/CashReegister/ViewController.h +++ b/CashReegister/ViewController.h @@ -7,7 +7,14 @@ // #import +#import "Currency.h" -@interface ViewController : UIViewController +@interface ViewController : UIViewController +@property (weak, nonatomic) IBOutlet UITextField *txtPrice; +@property (weak, nonatomic) IBOutlet UITextField *txtCash; +- (IBAction)btnChange:(id)sender; +@property (weak, nonatomic) IBOutlet UIScrollView *scrChange; +@property(strong,nonatomic)NSArray *arrCurrency; +@property(assign,nonatomic)int scrollHeight; @end diff --git a/CashReegister/ViewController.m b/CashReegister/ViewController.m index d2689cf..a12af67 100644 --- a/CashReegister/ViewController.m +++ b/CashReegister/ViewController.m @@ -16,13 +16,46 @@ @implementation ViewController - (void)viewDidLoad { + self.scrollHeight = 0; + self.arrCurrency = [[NSArray alloc]initWithObjects: + [self getCurrencyWithValue:100.00 andIdentify:@"ONE HUNDRED"], + [self getCurrencyWithValue:50.00 andIdentify:@"FIFTY"], + [self getCurrencyWithValue:20.00 andIdentify:@"TWENTY"], + [self getCurrencyWithValue:10.00 andIdentify:@"TEN"], + [self getCurrencyWithValue:5.00 andIdentify:@"FIVE"], + [self getCurrencyWithValue:2.00 andIdentify:@"TWO"], + [self getCurrencyWithValue:1.00 andIdentify:@"ONE"], + [self getCurrencyWithValue:0.50 andIdentify:@"HALF DOLLAR"], + [self getCurrencyWithValue:0.25 andIdentify:@"QUARTER"], + [self getCurrencyWithValue:0.10 andIdentify:@"DIME"], + [self getCurrencyWithValue:0.05 andIdentify:@"NICKEL"], + [self getCurrencyWithValue:0.01 andIdentify:@"PENNY"],nil]; //if you can see this, initial commit worked + [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } +-(BOOL)textFieldShouldReturn:(UITextField *)textField +{ + [textField resignFirstResponder]; + return YES; +} + + +-(Currency*)getCurrencyWithValue:(float)v andIdentify:(NSString*)s +{ + Currency *c = [[Currency alloc]init]; + c.value = [NSNumber numberWithFloat:v]; + c.name = [NSString stringWithString:s]; + return c; +} + - (void)viewDidUnload { + [self setTxtPrice:nil]; + [self setTxtCash:nil]; + [self setScrChange:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. } @@ -32,4 +65,84 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } +-(NSString*)convertNumber:(NSString*)input +{ + + NSMutableCharacterSet *decimalAndDot = [NSMutableCharacterSet characterSetWithCharactersInString:@"."]; + [decimalAndDot formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]]; + NSString *formattedInput = [[input componentsSeparatedByCharactersInSet:[decimalAndDot invertedSet]]componentsJoinedByString:@""]; + NSMutableArray *decimalComponents = [NSMutableArray arrayWithArray: [formattedInput componentsSeparatedByString:@"."]]; + if([decimalComponents count] > 1) + { + NSArray *temp = [NSArray arrayWithObjects:[decimalComponents objectAtIndex:0],[decimalComponents objectAtIndex:1], nil]; + [decimalComponents removeObjectAtIndex:0]; + [decimalComponents replaceObjectAtIndex:0 withObject:[temp componentsJoinedByString:@"."]]; + formattedInput = [decimalComponents componentsJoinedByString:@""]; + } + return formattedInput; + + //NSString *formattedPhone =[[input componentsSeparatedByCharactersInSet:[decimalAndDot invertedSet]]componentsJoinedByString:@""]; + + +} + + +-(float)roundStringValue:(NSString*)s +{ + int temp = [s floatValue] * 100 + .5; + float rounded = temp / 100; + return rounded; +} + +-(void)displayResults:(NSString*)s +{ + CGSize maximumLabelSize = CGSizeMake(320,9999); + CGSize expectedLabelSize = [s sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping]; + UILabel *lblChange = [[UILabel alloc]initWithFrame:CGRectMake(0, self.scrollHeight, self.scrChange.frame.size.width, expectedLabelSize.height)]; + [lblChange setFont:[UIFont fontWithName:@"Helvetica" size:16]]; + [lblChange setText:s]; + [lblChange setNumberOfLines:0]; + [lblChange setLineBreakMode:NSLineBreakByWordWrapping]; + [self.scrChange addSubview:lblChange]; + self.scrollHeight = self.scrollHeight + 30 + lblChange.frame.size.height; + [self.scrChange setContentSize:CGSizeMake(self.scrChange.frame.size.width, self.scrollHeight)]; +} + +- (IBAction)btnChange:(id)sender +{ + float price = [self roundStringValue: [self convertNumber:self.txtPrice.text]]; + float cash = [self roundStringValue: [self convertNumber:self.txtCash.text]]; + float owed = cash - price; + if(owed > 0) + { + NSMutableString *allChange = [[NSMutableString alloc]initWithString:@""]; + for(Currency *c in self.arrCurrency) + { + BOOL done = NO; + while(!done) + { + + if(owed - [c.value floatValue] > 0) + { + [allChange appendFormat:@", %@",c.name ]; + owed = owed - [c.value floatValue]; + } + else + { + done = YES; + } + } + } + [self displayResults:allChange]; + } + else if(owed == 0) + { + [self displayResults:@"Exact Change"]; + } + else + { + [self displayResults:[NSString stringWithFormat:@"You still owe $%f more",-owed]]; + } + +} @end diff --git a/CashReegister/en.lproj/MainStoryboard.storyboard b/CashReegister/en.lproj/MainStoryboard.storyboard index e8d72ab..0c7c600 100644 --- a/CashReegister/en.lproj/MainStoryboard.storyboard +++ b/CashReegister/en.lproj/MainStoryboard.storyboard @@ -1,21 +1,64 @@ - + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From b0179594b6d47642535b677b644093a989161315 Mon Sep 17 00:00:00 2001 From: David Spivak Date: Thu, 6 Dec 2012 16:48:32 -0500 Subject: [PATCH 3/3] Completed Assignment --- CashReegister/Currency.h | 2 +- CashReegister/Currency.m | 2 +- CashReegister/ViewController.m | 58 ++++++++++++++----- .../en.lproj/MainStoryboard.storyboard | 4 +- CashReegisterTests/CashReegisterTests.m | 1 + 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/CashReegister/Currency.h b/CashReegister/Currency.h index b233f5b..a197835 100644 --- a/CashReegister/Currency.h +++ b/CashReegister/Currency.h @@ -2,7 +2,7 @@ // Currency.h // CashReegister // -// Created by Brandon Salazar on 12/6/12. +// Created by David Spivak on 12/6/12. // Copyright (c) 2012 Dealermatch. All rights reserved. // diff --git a/CashReegister/Currency.m b/CashReegister/Currency.m index 09e8ccf..6656ff8 100644 --- a/CashReegister/Currency.m +++ b/CashReegister/Currency.m @@ -2,7 +2,7 @@ // Currency.m // CashReegister // -// Created by Brandon Salazar on 12/6/12. +// Created by David Spivak on 12/6/12. // Copyright (c) 2012 Dealermatch. All rights reserved. // diff --git a/CashReegister/ViewController.m b/CashReegister/ViewController.m index a12af67..1f24791 100644 --- a/CashReegister/ViewController.m +++ b/CashReegister/ViewController.m @@ -14,6 +14,7 @@ @interface ViewController () @implementation ViewController +//set up initial values, construct array of currency - (void)viewDidLoad { self.scrollHeight = 0; @@ -30,7 +31,6 @@ - (void)viewDidLoad [self getCurrencyWithValue:0.10 andIdentify:@"DIME"], [self getCurrencyWithValue:0.05 andIdentify:@"NICKEL"], [self getCurrencyWithValue:0.01 andIdentify:@"PENNY"],nil]; - //if you can see this, initial commit worked [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. @@ -65,13 +65,15 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } +//convert any string to a valid number -(NSString*)convertNumber:(NSString*)input { - NSMutableCharacterSet *decimalAndDot = [NSMutableCharacterSet characterSetWithCharactersInString:@"."]; [decimalAndDot formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]]; NSString *formattedInput = [[input componentsSeparatedByCharactersInSet:[decimalAndDot invertedSet]]componentsJoinedByString:@""]; NSMutableArray *decimalComponents = [NSMutableArray arrayWithArray: [formattedInput componentsSeparatedByString:@"."]]; + + //if there is at least one decimal point if([decimalComponents count] > 1) { NSArray *temp = [NSArray arrayWithObjects:[decimalComponents objectAtIndex:0],[decimalComponents objectAtIndex:1], nil]; @@ -80,25 +82,23 @@ -(NSString*)convertNumber:(NSString*)input formattedInput = [decimalComponents componentsJoinedByString:@""]; } return formattedInput; - - //NSString *formattedPhone =[[input componentsSeparatedByCharactersInSet:[decimalAndDot invertedSet]]componentsJoinedByString:@""]; - - } - +//round it to 2 decimal places -(float)roundStringValue:(NSString*)s { - int temp = [s floatValue] * 100 + .5; - float rounded = temp / 100; + int temp = ([s floatValue] * 100 + .5); + float rounded = temp; + rounded = rounded/100; return rounded; } +//print string to scrollView -(void)displayResults:(NSString*)s { CGSize maximumLabelSize = CGSizeMake(320,9999); CGSize expectedLabelSize = [s sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping]; - UILabel *lblChange = [[UILabel alloc]initWithFrame:CGRectMake(0, self.scrollHeight, self.scrChange.frame.size.width, expectedLabelSize.height)]; + UILabel *lblChange = [[UILabel alloc]initWithFrame:CGRectMake(5, self.scrollHeight, self.scrChange.frame.size.width-10, expectedLabelSize.height)]; [lblChange setFont:[UIFont fontWithName:@"Helvetica" size:16]]; [lblChange setText:s]; [lblChange setNumberOfLines:0]; @@ -106,42 +106,70 @@ -(void)displayResults:(NSString*)s [self.scrChange addSubview:lblChange]; self.scrollHeight = self.scrollHeight + 30 + lblChange.frame.size.height; [self.scrChange setContentSize:CGSizeMake(self.scrChange.frame.size.width, self.scrollHeight)]; + + int position = 0; + + //if the user could scroll, scroll to the bottom + if(self.scrChange.contentSize.height > self.scrChange.frame.size.height) + { + position = self.scrChange.contentSize.height - self.scrChange.frame.size.height; + } + + [self.scrChange setContentOffset:CGPointMake(0, position) animated:YES]; } + +//when the user presses the change button - (IBAction)btnChange:(id)sender { + [self.txtPrice resignFirstResponder]; + [self.txtCash resignFirstResponder]; float price = [self roundStringValue: [self convertNumber:self.txtPrice.text]]; + [self.txtPrice setText:[NSString stringWithFormat:@"%.2f",price]]; float cash = [self roundStringValue: [self convertNumber:self.txtCash.text]]; + [self.txtCash setText:[NSString stringWithFormat:@"%.2f",cash]]; float owed = cash - price; + + + //if they gave more cash than the price if(owed > 0) { NSMutableString *allChange = [[NSMutableString alloc]initWithString:@""]; + + //for each unit of currency in order from greatest to least money for(Currency *c in self.arrCurrency) { BOOL done = NO; + //while this unit is still less than the total owed while(!done) { - - if(owed - [c.value floatValue] > 0) + //if this unit is still less than the total owed + if(owed - [c.value floatValue] >= 0) { - [allChange appendFormat:@", %@",c.name ]; + NSLog(@"thing: %@, amount left: %f",c.name,owed); + [allChange appendFormat:@"%@,",c.name ]; owed = owed - [c.value floatValue]; + } + //or it it's not, move onto the next lowest unit else { done = YES; } } } + [allChange deleteCharactersInRange:NSMakeRange([allChange length]-1, 1)]; [self displayResults:allChange]; } + //or if they gave exact change else if(owed == 0) { - [self displayResults:@"Exact Change"]; + [self displayResults:@"ZERO"]; } + //or if they didn't give enough money else { - [self displayResults:[NSString stringWithFormat:@"You still owe $%f more",-owed]]; + [self displayResults:@"ERROR"]; } } diff --git a/CashReegister/en.lproj/MainStoryboard.storyboard b/CashReegister/en.lproj/MainStoryboard.storyboard index 0c7c600..52e7521 100644 --- a/CashReegister/en.lproj/MainStoryboard.storyboard +++ b/CashReegister/en.lproj/MainStoryboard.storyboard @@ -16,7 +16,7 @@ - + @@ -25,7 +25,7 @@ - + diff --git a/CashReegisterTests/CashReegisterTests.m b/CashReegisterTests/CashReegisterTests.m index d0a39b5..6f7a005 100644 --- a/CashReegisterTests/CashReegisterTests.m +++ b/CashReegisterTests/CashReegisterTests.m @@ -29,4 +29,5 @@ - (void)testExample STFail(@"Unit tests are not implemented yet in CashReegisterTests"); } + @end