Skip to content

Instantly share code, notes, and snippets.

@ciprianna
Created June 11, 2015 19:45
Show Gist options
  • Save ciprianna/3d4b9b88b9ba575c19c0 to your computer and use it in GitHub Desktop.
Save ciprianna/3d4b9b88b9ba575c19c0 to your computer and use it in GitHub Desktop.
CheckSplitter with SQL
# Check Splitter Info
# Check Splitter
# Creates new class called CheckSplitter
class CheckSplitter
# Shows which attributes are accessible
attr_accessor :total_cost, :tip_percentage, :people, :args
# Initializes the attributes as optional; sets default values in the options
# hash and merges the hash with inputs (if given). Uses those defaults as
# initialized attributes. Calls methods below with args as an argument.
def initialize(args = {})
options = {total_cost: 20, tip_percentage: 0.15, people: 2}
@args = options.merge(args)
@total_cost = args[:total_cost]
tip_percent(@args)
people_count(@args)
neg_tip(@args)
end
# Makes the default value a decimal, so the user can input either integers or
# floats.
#
# args - Hash that we will pull the :tip_percentage value out. This value is
# a Float.
#
# Returns an instance variable called tip_percentage, containing a Float.
def tip_percent(args)
tip_percent_temp = args[:tip_percentage].to_f
if tip_percent_temp >= 1
@tip_percentage = (tip_percent_temp / 100.0)
else
@tip_percentage = tip_percent_temp
end
end
# Checks if the tip input was negative; if so, sets it to 0.15
def neg_tip(args)
tip_percent_temp = args[:tip_percentage].to_f
if tip_percent_temp < 0
@tip_percentage = 0.15
end
end
# Evaluates the people input; if the input is less than 1, it sets it to 1
def people_count(args)
people_temp = args[:people]
if people_temp < 1
@people = 1
else
@people = people_temp
end
end
# Total cost of the meal with tip
def total_cost_with_tip
total_cost + (total_cost * tip_percentage)
end
# Adds the total cost of the meal per person and the tip per person
def final_per_person
total_cost_with_tip / people
end
end
# Check Splitter as a Database
require "sqlite3"
require_relative "checksplitter_code.rb"
# Creates the database connection
DATABASE = SQLite3::Database.new("bills.db")
# Creates the table
DATABASE.execute("CREATE TABLE IF NOT EXISTS split_checks (id INTEGER PRIMARY KEY, total_cost INTEGER, tip_percentage INTEGER, people INTEGER, total_cost_with_tip INTEGER, final_per_person INTEGER);")
# Returns the results as a Hash
DATABASE.results_as_hash = true
# Show everything in the table
def show_table
DATABASE.execute("SELECT * FROM split_checks;")
end
# Show total_cost_with_tip (Float)
def show_total_cost_with_tip
DATABASE.execute("SELECT total_cost_with_tip FROM split_checks;")
end
# Show final_per_person (Float)
def show_final_per_person
DATABASE.execute("SELECT final_per_person FROM split_checks;")
end
# Show total_cost (Float)
def show_total_cost
DATABASE.execute("SELECT total_cost FROM split_checks;")
end
# Show tip_percentage (Float)
def show_tip_percentage
DATABASE.execute("SELECT tip_percentage FROM split_checks;")
end
# Show people (Integer)
def show_number_of_people
DATABASE.execute("SELECT people FROM split_checks;")
end
# Show row of information for a given check by id (Integer)
def check_costs(id)
DATABASE.execute("SELECT * FROM split_checks WHERE id = #{id}")
end
# Update tip_percentage (Float), and also updates the corresponding total_cost
# (Float) and final_per_person (Float) values.
#
# id - Integer
# new_tip - Float
#
# Returns nothing.
def update_tip(id, new_tip)
total_cost = DATABASE.execute("SELECT total_cost FROM split_checks WHERE id = #{id};")
total_cost = total_cost.first["total_cost"]
people = DATABASE.execute("SELECT people FROM split_checks WHERE id = #{id};")
people = people.first["people"]
new_check = CheckSplitter.new(total_cost: total_cost, tip_percentage: new_tip, people: people)
DATABASE.execute("UPDATE split_checks SET tip_percentage = #{new_tip}, total_cost_with_tip = #{new_check.total_cost_with_tip}, final_per_person = #{new_check.final_per_person} WHERE id = #{id};")
end
# Update total_cost (Float); also updates the corresponding total_cost_with_tip
# (Float), and final_per_person (Float) values.
#
# id - Integer
# new_cost - Integer
#
# Returns nothing
def update_total_cost(id, new_cost)
tip_percent = DATABASE.execute("SELECT tip_percentage FROM split_checks WHERE id = #{id};")
tip_percent = tip_percent.first["tip_percentage"]
people = DATABASE.execute("SELECT people FROM split_checks WHERE id = #{id};")
people = people.first["people"]
new_check = CheckSplitter.new(total_cost: new_cost, tip_percentage: tip_percent, people: people)
DATABASE.execute("UPDATE split_checks SET total_cost = #{new_cost}, total_cost_with_tip= #{new_check.total_cost_with_tip}, final_per_person = #{new_check.final_per_person} WHERE id = #{id};")
end
# Update the number of people (Integer); updates the corresponding value for
# final_per_person (Float)
#
# id - Integer
# new_number_of_people - Integer
#
# Returns nothing
def update_number_of_people(id, new_number_of_people)
tip_percent = DATABASE.execute("SELECT tip_percentage FROM split_checks WHERE id = #{id};")
tip_percent = tip_percent.first["tip_percentage"]
total_cost = DATABASE.execute("SELECT total_cost FROM split_checks WHERE id = #{id};")
total_cost = total_cost.first["total_cost"]
new_check = CheckSplitter.new(total_cost: total_cost, tip_percentage: tip_percent, people: new_number_of_people)
DATABASE.execute("UPDATE split_checks SET people = #{new_number_of_people}, final_per_person = #{new_check.final_per_person} WHERE id = #{id};")
end
# Add a new Check with CheckSplitter object and adds to database
#
# total_cost - Integer
# tip_percentage - Float
# people - Integer
#
# Returns nothing
def add_check(total_cost, tip_percentage, people)
new_check = CheckSplitter.new(total_cost: total_cost, tip_percentage: tip_percentage, people: people)
new_entry(total_cost, tip_percentage, people, new_check.total_cost_with_tip, new_check.final_per_person)
end
# Utility method - Adds a new entry to the split_checks table
#
# total_cost - Integer
# tip_percentage - Float
# people - Integer
# total_cost_with_tip - Float; calculated from CheckSplitter
# final_per_person - Float; calculated from CheckSplitter
#
# Returns nothing.
def new_entry (total_cost, tip_percentage, people, total_cost_with_tip, final_per_person)
DATABASE.execute("INSERT INTO split_checks (total_cost, tip_percentage, people, total_cost_with_tip, final_per_person) VALUES (#{total_cost}, #{tip_percentage}, #{people}, #{total_cost_with_tip}, #{final_per_person});")
end
# Remove a split_checks entry
#
# id - Integer
#
# Returns nothing
def remove_check(id)
DATABASE.execute("DELETE FROM split_checks WHERE id = #{id};")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment