Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created September 9, 2010 10:46
Show Gist options
  • Select an option

  • Save JoshCheek/571709 to your computer and use it in GitHub Desktop.

Select an option

Save JoshCheek/571709 to your computer and use it in GitHub Desktop.
in class program done in a group to practice software engineering
# http://gist.github.com/571709
# Okay, after class, I cleaned it up just a small amount, and added bigdecimal
# Other than that, this was written in class by team Riskies, if you can't tell, our team was pretty much the best.
require 'rubygems'
require 'sinatra'
require 'bigdecimal' # use big decimals, because IEEE floats are imprecise, and we are working with money
class String
def to_bd
BigDecimal.new self # make bigdecimal less clunky to work with
end
end
def salestax
params[:state].to_bd
end
def price
params[:price].to_bd
end
def quantity
params[:quantity].to_bd
end
def subtotal
price * quantity
end
def discount
return '0'.to_bd if subtotal < 1_000
return '0.03'.to_bd if subtotal < 5_000
return '0.06'.to_bd if subtotal < 10_000
return '0.10'.to_bd
end
def discounted_subtotal
subtotal * ( 1 - discount )
end
def tax
discounted_subtotal * salestax
end
def total
discounted_subtotal + tax
end
def percent(value)
sprintf '%3.0f%' , value * 100
end
def money(quantity)
sprintf '$%.02f' , quantity
end
get '/' do
<<HTML
<form action="/total" method="post" accept-charset="utf-8">
<table border="0" cellspacing="5">
<tr>
<td><label for="price">Price</label></td>
<td><input type="text" name="price" value="" id="price"> </td>
</tr>
<tr>
<td><label for="quantity">Quantity</label></td>
<td><input type="text" name="quantity" value="" id="quantity"></td>
</tr>
<tr>
<td><label for="state">State</label></td>
<td>
<select name='state'>
<option value="0.0825" > CA </option>
<option value="0.047" > UT </option>
<option value="0.0685" > NV </option>
<option value="0.06" > FL </option>
</select>
</td>
</tr>
</table>
<p><input type="submit" value="Submit &rarr;"></p>
</form>
HTML
end
post '/total' do
<<HTML
<table border="0" cellspacing="5" >
<tr><td>Discount: </td><td>#{ percent discount }</td></tr>
<tr><td>Subtotal: </td><td>#{ money subtotal }</td></tr>
<tr><td>Discounted Subtotal: </td><td>#{ money discounted_subtotal }</td></tr>
<tr><td>Sales Tax: </td><td>#{ money tax }</td></tr>
<tr><td>Total: </td><td>#{ money total }</td></tr>
</table>
HTML
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment