-
Notifications
You must be signed in to change notification settings - Fork 8
/
swisscalc.calc.tipCalculator.js
42 lines (35 loc) · 2.19 KB
/
swisscalc.calc.tipCalculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// Eric Morgan
// Copyright (c) 2014.
//
// Class for implementing a tip calculator
var swisscalc = swisscalc || {};
swisscalc.calc = swisscalc.calc || {};
swisscalc.calc.tipCalculator = function() {
this._subtotal = new swisscalc.display.fixedPointDisplay(2); // Use fixed point display to handle keypad inputs
this._tipPercent = 0.15; // Store the actual value
};
// Getters...
swisscalc.calc.tipCalculator.prototype.getSubtotalValue = function() { return this._subtotal.getDisplayValue(); };
swisscalc.calc.tipCalculator.prototype.getTipValueDecimal = function() { return this._tipPercent; };
swisscalc.calc.tipCalculator.prototype.getTipValuePercentage = function() { return this._tipPercent * 100.0; };
// Setters...
swisscalc.calc.tipCalculator.prototype.setSubtotalValue = function(value) { this._subtotal.setDisplayValue(value); };
swisscalc.calc.tipCalculator.prototype.setTipValueDecimal = function(decimal) { this._tipPercent = decimal; };
swisscalc.calc.tipCalculator.prototype.setTipValuePercentage = function(perc) { this._tipPercent = perc / 100.0; };
// Display functions...
swisscalc.calc.tipCalculator.prototype.getSubtotalDisplay = function() { return swisscalc.lib.format.asUSCurrency(this._subtotal.getDisplayValue()); };
swisscalc.calc.tipCalculator.prototype.getTipPercentDisplay = function() { return (this._tipPercent * 100.0).toFixed(1) + "%"; };
swisscalc.calc.tipCalculator.prototype.getTipAmountDisplay = function() { return swisscalc.lib.format.asUSCurrency(this.getTipAmount()); };
swisscalc.calc.tipCalculator.prototype.getTipCombinedDisplay = function() { return this.getTipPercentDisplay() + " " + this.getTipAmountDisplay(); };
swisscalc.calc.tipCalculator.prototype.getTotalDisplay = function() { return swisscalc.lib.format.asUSCurrency(this.getTotal()); };
// Returns the tip amount (in dollars)
swisscalc.calc.tipCalculator.prototype.getTipAmount = function() {
var subtotal = this.getSubtotalValue();
return subtotal * this._tipPercent;
};
// Returns the bill total including tip (in dollars)
swisscalc.calc.tipCalculator.prototype.getTotal = function() {
var tipAmount = this.getTipAmount();
return this.getSubtotalValue() + tipAmount;
};