Skip to content

Instantly share code, notes, and snippets.

@rplugge
Last active August 29, 2015 14:22
Show Gist options
  • Save rplugge/857d2b90930158829910 to your computer and use it in GitHub Desktop.
Save rplugge/857d2b90930158829910 to your computer and use it in GitHub Desktop.
CheckSplitter With Driver app and SQL
require_relative "check_splitter_sql.rb"
puts "Would you like to add an event?"
answer= gets.chomp.downcase
while answer != "no"
puts "\n How much was the meal?"
meal_cost = gets.chomp
puts "\n How much did you tip?"
tip = gets.chomp
puts "\n How many people went?"
number_of_people = gets.chomp
add_event(meal_cost, tip, number_of_people)
puts "\n Would you like to view information about an event?"
answer = gets.chomp.downcase
if answer != "no"
puts "\n Which even would you like to see? (Event 1, 2, 3, etc)"
event_number = gets.chomp.to_i
puts "\n Your cost after tip was #{cost_after_tip(event_number)}"
puts "\n Your cost per person is #{cost_split(event_number)}"
end
puts "\n Would you like to add another event?"
answer = gets.chomp.downcase
end
require "sqlite3"
CHECKSPLITTER = SQLite3::Database.new("checksplitter.db")
CHECKSPLITTER.execute("CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY, total_cost INTEGER, tip INTEGER, number_of_people INTEGER);")
CHECKSPLITTER.results_as_hash = true
#Delete Table
def delete_table
CHECKSPLITTER.execute("DROP TABLE events")
end
#Adds Event
def add_event(total_cost, tip, number_of_people)
CHECKSPLITTER.execute("INSERT INTO events (total_cost, tip, number_of_people) VALUES (#{total_cost}, #{tip}, #{number_of_people});")
end
#Returns Total_Cost of meal (before tip)
def return_total_cost(id)
total_cost = CHECKSPLITTER.execute("SELECT (total_cost) FROM events WHERE id = #{id};")
return total_cost[0]["total_cost"]
end
#Returns Tip amount
def return_tip(id)
tip = CHECKSPLITTER.execute("SELECT (tip) FROM events WHERE id = #{id};")
return tip[0]["tip"]
end
#Returns Number of People
def return_number_of_people(id)
number_of_people = CHECKSPLITTER.execute("SELECT (number_of_people) FROM events WHERE id = #{id};")
return number_of_people[0]["number_of_people"]
end
#Adds Total_Cost of the meal and amount of tip
def cost_after_tip(id)
return_total_cost(id) + return_tip(id)
end
#splits Cost_After_Tip by Number_Of_People
def cost_split(id)
cost_after_tip(id) / return_number_of_people(id)
end
def show_table
CHECKSPLITTER.execute("SELECT * FROM events")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment