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..a197835 --- /dev/null +++ b/CashReegister/Currency.h @@ -0,0 +1,15 @@ +// +// Currency.h +// CashReegister +// +// Created by David Spivak 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..6656ff8 --- /dev/null +++ b/CashReegister/Currency.m @@ -0,0 +1,15 @@ +// +// Currency.m +// CashReegister +// +// Created by David Spivak 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 274f7b0..1f24791 100644 --- a/CashReegister/ViewController.m +++ b/CashReegister/ViewController.m @@ -14,14 +14,48 @@ @interface ViewController () @implementation ViewController +//set up initial values, construct array of currency - (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]; + [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. } @@ -31,4 +65,112 @@ - (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]; + [decimalComponents removeObjectAtIndex:0]; + [decimalComponents replaceObjectAtIndex:0 withObject:[temp componentsJoinedByString:@"."]]; + formattedInput = [decimalComponents componentsJoinedByString:@""]; + } + return formattedInput; +} + +//round it to 2 decimal places +-(float)roundStringValue:(NSString*)s +{ + 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(5, self.scrollHeight, self.scrChange.frame.size.width-10, 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)]; + + 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 this unit is still less than the total owed + if(owed - [c.value floatValue] >= 0) + { + 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:@"ZERO"]; + } + //or if they didn't give enough money + else + { + [self displayResults:@"ERROR"]; + } + +} @end diff --git a/CashReegister/en.lproj/MainStoryboard.storyboard b/CashReegister/en.lproj/MainStoryboard.storyboard index e8d72ab..52e7521 100644 --- a/CashReegister/en.lproj/MainStoryboard.storyboard +++ b/CashReegister/en.lproj/MainStoryboard.storyboard @@ -1,21 +1,64 @@ - + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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