Created
July 4, 2011 21:36
-
-
Save caike/1063979 to your computer and use it in GitHub Desktop.
Vending Machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VendingMachine | |
constructor: -> | |
@products = { | |
'coke': 5, | |
'iced tea': 5 | |
} | |
@balance = 0 | |
getCount: (productName)-> | |
@products[productName] | |
addMoney: (currency)-> | |
@balance += currency if currency > 0 | |
getBalance: -> | |
@balance | |
buy: (product) -> | |
unless @balance > 0 && @products[product] | |
return null | |
@balance-- | |
@products[product]-- | |
return product | |
demandChange: -> | |
change = @balance | |
@balance = 0 | |
change |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var VendingMachine; | |
VendingMachine = (function() { | |
function VendingMachine() { | |
this.products = { | |
'coke': 5, | |
'iced tea': 5 | |
}; | |
this.balance = 0; | |
} | |
VendingMachine.prototype.getCount = function(productName) { | |
return this.products[productName]; | |
}; | |
VendingMachine.prototype.addMoney = function(currency) { | |
if (currency > 0) { | |
return this.balance += currency; | |
} | |
}; | |
VendingMachine.prototype.getBalance = function() { | |
return this.balance; | |
}; | |
VendingMachine.prototype.buy = function(product) { | |
if (!(this.balance > 0 && this.products[product])) { | |
return null; | |
} | |
this.balance--; | |
this.products[product]--; | |
return product; | |
}; | |
VendingMachine.prototype.demandChange = function() { | |
var change; | |
change = this.balance; | |
this.balance = 0; | |
return change; | |
}; | |
return VendingMachine; | |
})(); | |
}).call(this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'A vending machine', -> | |
beforeEach -> | |
@m = new VendingMachine | |
it 'returns product count', -> | |
expect(@m.getCount('coke')).toEqual(5) | |
it 'returns iced tea count', -> | |
expect(@m.getCount('iced tea')).toEqual(5) | |
it 'accepts money', -> | |
@m.addMoney(100) | |
expect(@m.getBalance()).toEqual 100 | |
@m.addMoney(-10) | |
expect(@m.getBalance()).toEqual 100 | |
it 'should sell us stuff', -> | |
expect(@m.buy('coke')).toBe(null) | |
@m.addMoney 2 | |
expect(@m.buy('coke')).toEqual 'coke' | |
expect(@m.buy('iced tea')).toEqual 'iced tea' | |
expect(@m.buy('coke')).toBe(null) | |
it 'should not sell crack', -> | |
@m.addMoney(1000) | |
expect(@m.getBalance()).toEqual 1000 | |
expect(@m.buy('crack')).toBe(null) | |
it 'should decrement the product count after buying', -> | |
@m.addMoney(5) | |
previous_count = @m.getCount('coke') | |
@m.buy('coke') | |
new_count = previous_count - 1 | |
expect(@m.getCount('coke')).toBe(new_count) | |
it 'should give change', -> | |
@m.addMoney(2) | |
@m.buy('coke') | |
expect(@m.demandChange()).toEqual 1 | |
expect(@m.getBalance()).toEqual 0 | |
it 'should just work', -> | |
@m.addMoney(10) | |
@m.buy('coke') | |
@m.buy('iced tea') | |
expect(@m.demandChange()).toEqual 8 | |
expect(@m.getBalance()).toEqual 0 | |
expect(@m.buy('coke')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment