Build a Tip Calculator in Ruby that Can Accept Multiple Data Types as Input

1
1193

In this coding exercise we’ll build out an extensive tip calculator that can accept integers, string based numbers and even words to dynamically generate a tip.

Summary

Build a Ruby module that can accurately process tip amounts based on input with multiple data type options.

Exercise File

Code File

Exercise Description

The key to this exercise is understanding how to properly manage multiple data type inputs and then to process the tip calculations.

Example Behavior

Tippy::Builder.new(total: 100, gratuity: '23.5').generate # 123.5
Tippy::Builder.new(total: 100, gratuity: 'high').generate # 125.0
Tippy::Builder.new(total: 100, gratuity: 'LOW').generate # 115.0
Tippy::Builder.new(total: 100, gratuity: 'standard').generate # 118.0
Tippy::Builder.new(total: 100, gratuity: '18').generate # 118.0
Tippy::Builder.new(total: 100, gratuity: 20).generate # 120.0
Tippy::Builder.new(total: 100, gratuity: 0).generate # 100.0

Please note that you need to handle:

  • Parsing string based numbers such as '20'
  • Traditional integers
  • Words, such as high, standard, and low. You can look at the tests to figure out what percentages those words should represent.

Real World Usage

This is going to be a challenging exercise that will test a number of coding techniques, such as:

  • Working with modules and classes
  • Performing metaprogramming
  • Control flow for varying data types
  • Regular expressions

Each of these components are utilized in day to day development.

Solution

Can be found on the solutions branch on github.

1 COMMENT

  1. Not everyone would agree to the hard coded values you have for the string types. Please consider some kind of configuration options that the user could set. Thanks again Jordan!

LEAVE A REPLY

Please enter your comment!
Please enter your name here