Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
class ApplicationController < ActionController::Base
def blank_square_form
render({ :template => "calculation_templates/square_form.html.erb" })
end

def calculate_square

# params = {"elephant" => 42}

@num = params.fetch("elephant").to_f
@square_of_num = @num ** 2

render({ :template => "calculation_templates/square_results.html.erb" })
end

def calculate_random
@lower = params.fetch("user_min").to_f
@upper = params.fetch("user_max").to_f
@result = rand(@lower...@upper)

render({ :template => "calculation_templates/rand_results.html.erb" })
end

def blank_random_form
render({ :template => "calculation_templates/random_form.html.erb" })
end

def square_root_form
render({ :template => "calculation_templates/square_root_form.html.erb" })
end

def square_root_results
@nums = params.fetch("squareroot_number").to_f
@squareroot_of_num = @nums ** (0.5)

render({ :template => "calculation_templates/square_root_results.html.erb" })
end

def payment_form
render({ :template => "calculation_templates/payment_form.html.erb" })
end

def payment_results
@num_apr = params.fetch("apr_number").to_f
@apr_per_period = (@num_apr / 100) / 12
@num_years = params.fetch("years_number").to_f.round(3)
@num_principal = params.fetch("principal_number").to_f

@monthly_payment = ((@apr_per_period * @num_principal) / (1 - (1 + @apr_per_period) ** (-12 * @num_years)))

@num_apr = @num_apr.to_s(:percentage, precision: 4)
render({ :template => "calculation_templates/payment_results.html.erb" })
end
end
24 changes: 24 additions & 0 deletions app/views/calculation_templates/payment_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<body>
<div>
<form action="/payment/results">

<div>
<label for="apr_payment_id">APR </label>
<input id="apr_payment_id" type="text" placeholder="e.g 5.42" size="50px" name="apr_number">
</div>

<div>
<label for="years_payment_id">Number of years</label>
<input id="years_payment_id" type="text" placeholder="How many years to repay?" size="50px" name="years_number">
</div>

<div>
<label for="principal_id">Principal</label>
<input id="principal_id" type="text" placeholder="How much principal?" size="50px" name="principal_number">
</div>

<div>
<button>Calculate monthly payment</button>
</form>
</div>
</body>
21 changes: 21 additions & 0 deletions app/views/calculation_templates/payment_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<body>
<div class="result_banner">
<h1>Payment Results </h1>
</div>

<div class="result_buttons">
<div class="result_wrapper">
<dl>
<dt> APR: </dt>
<dd> <%= @num_apr %> </dd>
<dt> Number of years: </dt>
<dd> <%= @num_years %> </dd>
<dt> Principal: </dt>
<dd> <%= @num_principal.to_s(:currency) %> </dd>
<dt>Monthly Payment: </dt>
<dd> <%= @monthly_payment.to_s(:currency) %> </dd>
</dl>
<a href="/payment/new">Calculate another payment</a>
</div>
</div>
</body>
23 changes: 23 additions & 0 deletions app/views/calculation_templates/rand_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="result_banner">
<h1>Random Results </h1>
</div>

<div class="result_buttons">
<div class="result_wrapper">


<dl>
<dt> Minimum </dt>
<dd> <%= @lower%> </dd>

<dt> Maximum</dt>
<dd> <%= @upper %> </dd>

<dt> Random Number</dt>
<dd> <%= @result%> </dd>

<a href="/random/new">Pick another random number

</dl>
</div>
</div>
15 changes: 15 additions & 0 deletions app/views/calculation_templates/random_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<body>
<form action="/random/results">
<div>
<label for="minimum_id">Minimum</label>
<input id="minimum_id" type="text" placeholder="e.g 1.5" size="50px" name="user_min">
</div>
<div>
<label for="maximum_id">Maximum</label>
<input id="maximum_id" type="text" placeholder="e.g 4.5" size="50px" name="user_max">
</div>
<button>Pick random number</button>
</form>
</body>
</html>
22 changes: 22 additions & 0 deletions app/views/calculation_templates/square_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@



<body>
<div class="form_items">
<form action="/square/results">

<div class="label_items">
<label for="elephant">Enter a number</label>
</div>

<div class="label_items">
<input id="elephant" type="text" placeholder="What number do we want to square?" size="50px" name="elephant">
</div>

<div class="button_items">
<button>Calculate Square</button>
</div>

</form>
</div>
</body>
16 changes: 16 additions & 0 deletions app/views/calculation_templates/square_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<div class="result_banner">
<h1>Square Results </h1>
</div>


<dl>
<dt>Number</dt>
<dd><%= @num %></dd>


<dt>Square</dt>
<dd><%= @square_of_num %></dd>
</dl>

<a href= "/square/new">Calculate another square
19 changes: 19 additions & 0 deletions app/views/calculation_templates/square_root_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

<body>
<div class=form_items>
<form action="/square_root/results">

<div class="label_items">
<label for="squareroot_id">Enter a number</label>
</div>

<div class="label_items">
<input id="squareroot_id" type="text" placeholder="What do you want to get the square root of?" size="50px" name="squareroot_number">
</div>

<div class="button_items">
<button> Calculate Square Root</button>
</div>
</form>
</div>
</body>
15 changes: 15 additions & 0 deletions app/views/calculation_templates/square_root_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class= "result_banner">
<h1> Square Root Results </h1>
</div>

<div class ="result_buttons">
<div class= "result_wrapper">
<dl>

<dt> Number: </dt>
<dd> <%= @nums %> </dd>
<dt> Square Root </dt>
<dd> <%= @squareroot_of_num.round(3) %> </dd>
</dl>
</div>
</div>
16 changes: 16 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
Rails.application.routes.draw do
get("/square/new", { :controller => "application", :action => "blank_square_form" })

get("/square/results", { :controller => "application", :action => "calculate_square" })

get("/random/results", { :controller => "application", :action => "calculate_random" })

get("/random/new", { :controller => "application", :action => "blank_random_form" })

get("square_root/new", { :controller => "application", :action => "square_root_form" })

get("square_root/results", { :controller => "application", :action => "square_root_results"})

get("payment/new", { :controller => "application", :action => "payment_form" })

get("payment/results", { :controller => "application", :action => "payment_results"})


end
Binary file added db/development.sqlite3
Binary file not shown.
Binary file added db/test.sqlite3
Binary file not shown.