Skip to content

Instantly share code, notes, and snippets.

@eternal44
Created May 4, 2016 20:09
Show Gist options
  • Save eternal44/5225cf24a2c8dd92529b16bc5b192410 to your computer and use it in GitHub Desktop.
Save eternal44/5225cf24a2c8dd92529b16bc5b192410 to your computer and use it in GitHub Desktop.
Designing a vending machine
/*
##########
# PROMPT #
##########
- This vending machine must take user inputs (i.e. A1, A2, B1..etc) and dispenses an item after users input money.
- There are only 9 inputs.
- Assume that there are different kinds of items in the vending machine (Sprite, cheetos, etc).
- The vending machine must store instances of items to be dispensed, the item can't be created at the moment someone requests it.
###############
# ASSUMPTIONS #
###############
- has a glass front - no need to persist quantities or types of the items
- will still process a customer's purchase even if there are no more items in the vending machine
- would usually persist information mechanically
- persisting data (ex: amount a customer paid, vendingSelection prices) would normally be done
mechanically but I'll mock data with objects where applicable
########
# TODO #
########
make change? handle a no-change scenario?
*/
// - this would normally be done mechanically! But for the sake of this
// exercise let's go ahead and process their payment anyway
var vendingSelections = {
A1: {
// price values in cents
price: 150
},
A2: {
price: 100
},
A3: {
price: 100
},
B1: {
price: 150
},
B2: {
price: 100
},
B3: {
price: 100
},
C1: {
price: 150
},
C2: {
price: 100
},
C3: {
price: 100
}
}
var processPayment = function(selection, payment){
// no need to test for valid selections since they are predetermined by the
// buttons the user can push
var balance = vendingSelections[selection][price] - payment
if(!balance){
dispenceItem(selection);
}
// TODO: make change
}
// this function connects to the physical machine and sends a signal to
// mechanically dispence an item.
var dispenseItem = function(selection){
return {
selection: selection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment