-
-
Save ravidsrk/1998626 to your computer and use it in GitHub Desktop.
Cash Register classes in CoffeeScript and Javascript
This file contains 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 Dish | |
constructor: (rawDescription="") -> | |
[all, @title, @price] = @parseRawDescription rawDescription | |
@price = new Money @price | |
parseRawDescription: (rawDescription) -> | |
pattern = /// | |
([^$]+) #Title | |
(\$\d+\.\d+) #Price | |
/// | |
result = rawDescription.match pattern | |
r.trim() for r in result | |
toJSON: -> { @title, price: @price.toString() } | |
class Money | |
constructor: (rawString="") -> | |
@cents = @parseCents rawString | |
parseCents: (rawString) -> | |
[dollars, cents] = (rawString.match(/(\d+)/g) ? [0,0]) | |
+cents + 100*dollars | |
toString: -> | |
"$#{Math.floor(@cents / 100)}.#{@cents % 100}" | |
class Meal | |
constructor: -> | |
@dishes = [] | |
add: (dishes...) -> @dishes.push dishes... | |
totalPrice: -> | |
total = new Money | |
total.cents = total.cents + dish.price.cents for dish in @dishes | |
total | |
toJSON: -> | |
price: @totalPrice().toString() | |
dishes: dish.toJSON() for dish in @dishes | |
@[name] = type for name, type of {Dish, Money, Meal} |
This file contains 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 Dish, Meal, Money, name, type, _ref; | |
var __slice = Array.prototype.slice; | |
Dish = (function() { | |
function Dish(rawDescription) { | |
var all, _ref; | |
if (rawDescription == null) { | |
rawDescription = ""; | |
} | |
_ref = this.parseRawDescription(rawDescription), all = _ref[0], this.title = _ref[1], this.price = _ref[2]; | |
this.price = new Money(this.price); | |
} | |
Dish.prototype.parseRawDescription = function(rawDescription) { | |
var pattern, r, result, _i, _len, _results; | |
pattern = /([^$]+)(\$\d+\.\d+)/; | |
result = rawDescription.match(pattern); | |
_results = []; | |
for (_i = 0, _len = result.length; _i < _len; _i++) { | |
r = result[_i]; | |
_results.push(r.trim()); | |
} | |
return _results; | |
}; | |
Dish.prototype.toJSON = function() { | |
return { | |
title: this.title, | |
price: this.price.toString() | |
}; | |
}; | |
return Dish; | |
})(); | |
Money = (function() { | |
function Money(rawString) { | |
if (rawString == null) { | |
rawString = ""; | |
} | |
this.cents = this.parseCents(rawString); | |
} | |
Money.prototype.parseCents = function(rawString) { | |
var cents, dollars, _ref, _ref2; | |
_ref2 = (_ref = rawString.match(/(\d+)/g)) != null ? _ref : [0, 0], dollars = _ref2[0], cents = _ref2[1]; | |
return +cents + 100 * dollars; | |
}; | |
Money.prototype.toString = function() { | |
return "$" + (Math.floor(this.cents / 100)) + "." + (this.cents % 100); | |
}; | |
return Money; | |
})(); | |
Meal = (function() { | |
function Meal() { | |
this.dishes = []; | |
} | |
Meal.prototype.add = function() { | |
var dishes, _ref; | |
dishes = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | |
return (_ref = this.dishes).push.apply(_ref, dishes); | |
}; | |
Meal.prototype.totalPrice = function() { | |
var dish, total, _i, _len, _ref; | |
total = new Money; | |
_ref = this.dishes; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
dish = _ref[_i]; | |
total.cents = total.cents + dish.price.cents; | |
} | |
return total; | |
}; | |
Meal.prototype.toJSON = function() { | |
var dish; | |
return { | |
price: this.totalPrice().toString(), | |
dishes: (function() { | |
var _i, _len, _ref, _results; | |
_ref = this.dishes; | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
dish = _ref[_i]; | |
_results.push(dish.toJSON()); | |
} | |
return _results; | |
}).call(this) | |
}; | |
}; | |
return Meal; | |
})(); | |
_ref = { | |
Dish: Dish, | |
Money: Money, | |
Meal: Meal | |
}; | |
for (name in _ref) { | |
type = _ref[name]; | |
this[name] = type; | |
} | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment