ATM Problem
by conzett
HTML
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css">
<h1 id="qunit-header">ATM Problem Tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
JavaScript
function atm(a,t) {
if (a > t) {
return t;
}
if (a > 2000) {
throw {
name: 'MaxWithdrawlError',
message: 'Cannot withdraw more than 2000.00 at once'
};
}
if (a < 0 || t < 0) {
throw {
name: 'NegativelError',
message: 'Cannot pass a negative value for the amount or total'
};
}
if ((a % 5) !== 0 ) {
throw {
name: 'MultipleWithdrawlError',
message: 'Withdrawl amount should be a multiple of 5'
};
}
return (t - a - .50);
}
$(document).ready(function() {
test("Throw an exception when the amount + fee is over 2000", function() {
raises(function(){atm(2001,3000);}, "Expect an exception to be thrown");
});
test("Throw an exception if the amount is negative", function() {
raises(function(){atm(-200,100);}, "Expect an exception to be thrown");
});
test("Throw an exception if the amount is not a multiple of 5", function() {
raises(function(){atm(66,100);}, "Expect an exception to be thrown");
});
test("Test withdrawing more than availible", function() {
var value = atm(600, 200);
equals(value, 200, "We expect value to be the total account balance: 200");
});
test("Test withdrawl", function() {
var value = atm(50, 200);
equals(value, 149.50, "We expect value to be 149.5");
});
});