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

Item quantity control is wrong

You wrote the control like this:

guard item.quantity >= 0 else { throw VendingMachineError.OutOfStock }

But i think we should have controled the quantity of item with quantity that wanted.

For example customer may want 3 soda and program doesn't know that there is 3 Soda in the machine. We just sure that there is more than 0. It would be 1 or 2.

This is my opinion. I just want to be sure which method is right.

Exactly. The same question I have. It is no point to check if it is greater than zero.

1 Answer

Kent Lou
PLUS
Kent Lou
Courses Plus Student 2,185 Points

Hi Soner and Taraj, you are absolutely right. What he wrote assumed customer is only purchasing one of the item selected, the proper check should be

guard item.quantity >= quantity else {
    throw VendingMachineError.OutOfStock 
}

where item.quantity is the available stock and quantity is the input amount as required by customer