Created
June 9, 2015 19:41
-
-
Save KBeltz/99cb96848c937c471f24 to your computer and use it in GitHub Desktop.
CheckSplitter Test
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
# Build a CheckSplitter class. | |
class CheckSplitter | |
# total_cost_of_meal - float, cost of meal without tip | |
# tip_percentage - float, tip percentage or decimal | |
# num_people - integer, number of guests | |
def initialize(total_cost_of_meal, tip_percentage, num_people) | |
@total_cost_of_meal = total_cost_of_meal | |
@tip_percentage = tip_percentage | |
@num_people = num_people | |
end | |
#determine how much each person owes | |
#return a decimal value for how much each person owes | |
def even_split | |
meal_plus_tip / @num_people | |
end | |
# calculates meal + tip value in one step | |
def meal_plus_tip | |
@total_cost_of_meal * (tip_as_decimal + 1) | |
end | |
# compensates for the possibility of user entering tip in decimal form | |
# converts tip_percentage to decimal form if necessary | |
def tip_as_decimal | |
if @tip_percentage >= 1 | |
tip_as_decimal = @tip_percentage / 100.0 | |
else | |
tip_as_decimal = @tip_percentage | |
end | |
end | |
end |
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
require "minitest/autorun" | |
require_relative "checksplitter.rb" | |
class CheckSplitterTest < Minitest::Test | |
# One of my specs is that the even_split method should divide the | |
# meal_plus_tip amount evebly among the attendees | |
# | |
# Another spec is that the meal_plus_tip method should calculate the | |
# tip amount based on user input and add that amount to the meal total | |
# | |
# Another spec is that the tip_as_decimal method should calculate the | |
# tip amount in decimal form | |
def test_even_split | |
# meal_plus_tip / @num_people | |
check1 = CheckSplitter.new(100, 20, 2) | |
assert_equal(60, check1.even_split) | |
end | |
def test_meal_plus_tip | |
# @total_cost_of_meal * (tip_as_decimal + 1) | |
check1 = CheckSplitter.new(100, 20, 2) | |
assert_equal(120, check1.meal_plus_tip) | |
end | |
def test_tip_as_decimal | |
#if @tip_percentage >= 1 | |
#tip_as_decimal = @tip_percentage / 100.0 | |
#else | |
#tip_as_decimal = @tip_percentage | |
#end | |
check1 = CheckSplitter.new(100, 20, 2) | |
assert_equal(0.2, check1.tip_as_decimal) | |
check2 = CheckSplitter.new(100, 0.2, 2) | |
assert_equal(0.2, check2.tip_as_decimal) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment