Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CashReegister.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -52,6 +53,8 @@
4EA04E8815E5212500021A01 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
4EA04E8A15E5212500021A01 /* CashReegisterTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CashReegisterTests.h; sourceTree = "<group>"; };
4EA04E8B15E5212500021A01 /* CashReegisterTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CashReegisterTests.m; sourceTree = "<group>"; };
5811C1E91670F43F00BF93DB /* Currency.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Currency.h; sourceTree = "<group>"; };
5811C1EA1670F43F00BF93DB /* Currency.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Currency.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -116,6 +119,8 @@
4EA04E7215E5212500021A01 /* MainStoryboard.storyboard */,
4EA04E7515E5212500021A01 /* ViewController.h */,
4EA04E7615E5212500021A01 /* ViewController.m */,
5811C1E91670F43F00BF93DB /* Currency.h */,
5811C1EA1670F43F00BF93DB /* Currency.m */,
4EA04E6715E5212500021A01 /* Supporting Files */,
);
path = CashReegister;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -434,6 +440,7 @@
4EA04E9115E5212500021A01 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
4EA04E9215E5212500021A01 /* Build configuration list for PBXNativeTarget "CashReegisterTests" */ = {
isa = XCConfigurationList;
Expand All @@ -442,6 +449,7 @@
4EA04E9415E5212500021A01 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
15 changes: 15 additions & 0 deletions CashReegister/Currency.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Currency.h
// CashReegister
//
// Created by David Spivak on 12/6/12.
// Copyright (c) 2012 Dealermatch. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Currency : NSObject
@property(strong,nonatomic)NSNumber *value;
@property(strong,nonatomic)NSString *name;

@end
15 changes: 15 additions & 0 deletions CashReegister/Currency.m
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion CashReegister/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
//

#import <UIKit/UIKit.h>
#import "Currency.h"

@interface ViewController : UIViewController
@interface ViewController : UIViewController<UITextFieldDelegate>
@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
142 changes: 142 additions & 0 deletions CashReegister/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand All @@ -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
53 changes: 48 additions & 5 deletions CashReegister/en.lproj/MainStoryboard.storyboard
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="1.0" toolsVersion="1906" systemVersion="11A511" targetRuntime="iOS.CocoaTouch" nextObjectID="6" propertyAccessControl="none" initialViewController="2">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="2840" systemVersion="11G56" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="2">
<dependencies>
<development defaultVersion="4200" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="902"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="1926"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="5">
<objects>
<placeholder placeholderIdentifier="IBFirstResponder" id="4" sceneMemberID="firstResponder"/>
<viewController id="2" customClass="ViewController" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="3">
<rect key="frame" x="0.0" y="20" width="320" height="460"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" id="ADL-SO-Ngg">
<rect key="frame" x="0.0" y="204" width="320" height="256"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</scrollView>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Price" textAlignment="center" minimumFontSize="17" id="eW0-Dp-NOx">
<rect key="frame" x="75" y="29" width="171" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="2" id="AyD-hR-ZaS"/>
</connections>
</textField>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Cash Given" textAlignment="center" minimumFontSize="17" id="R2I-zb-srT">
<rect key="frame" x="75" y="79" width="171" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="2" id="lhF-hb-MAg"/>
</connections>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="Kgf-p1-JnF">
<rect key="frame" x="105" y="132" width="111" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<state key="normal" title="Get Change">
<color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<state key="highlighted">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="btnChange:" destination="2" eventType="touchUpInside" id="DXN-Nj-sXF"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
<connections>
<outlet property="scrChange" destination="ADL-SO-Ngg" id="EDQ-hV-ED6"/>
<outlet property="txtCash" destination="R2I-zb-srT" id="NgA-3X-CbS"/>
<outlet property="txtPrice" destination="eW0-Dp-NOx" id="Dla-t7-gkE"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="4" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
Expand Down
1 change: 1 addition & 0 deletions CashReegisterTests/CashReegisterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ - (void)testExample
STFail(@"Unit tests are not implemented yet in CashReegisterTests");
}


@end