Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Vending Machine App in Swift 2.0 Using the Vending Machine Modeling the Vending Machine

Mahdi Pedramrazi
Mahdi Pedramrazi
2,505 Points

Item quantity update is misplaced.

shouldn't we decrease the item quantity after we are sure that the user has enough money to purchase ? this line of code can go at the end.

item.quantity -= quantity

3 Answers

Yes absolutely, I think so, too. Both lines should be placed after the guard statements at the end of the function:

item.quantity -= quantity
inventory.updateValue(item, forKey: selection)
Drew Carver
Drew Carver
5,189 Points

I agree. I think these two lines:

item.quantity -= quantity
inventory.updateValue(item, forKey: selection)

should go in the if block, right after this statement:

amountDeposited -= totalPrice