Skip to content

Instantly share code, notes, and snippets.

@justuseapen
Created November 22, 2013 20:20
Show Gist options
  • Select an option

  • Save justuseapen/7606159 to your computer and use it in GitHub Desktop.

Select an option

Save justuseapen/7606159 to your computer and use it in GitHub Desktop.
require 'pry'
require 'csv'
def prompt(query)
puts query
gets.chomp
end
def get_data
item = prompt "What is the name of the item?"
sku = prompt "What is the SKU"
sell_price = prompt "What do we sell it for?"
buy_price = prompt "What do we buy it for"
write_to_inventory(item,sku,sell_price,buy_price)
puts "Congrats, #{item} was added to the inventory database."
inventory_query
end
def write_to_inventory (item, sku, sell_price, buy_price)
File.open("Inventory.csv", 'a+') {|f| f.puts([item, sku, sell_price, buy_price].join(","))}
end
def list_sales_for(date_range)
if date_range == "today"
date = Date.today
CSV.foreach("transactions.csv", headers: true) do |line|
parsed = Date.parse(line["date"])
if date == parsed
puts "On #{date} You sold #{line["items_sold"]}(s)."
end
end
else
date = Date.parse(date_range)
CSV.foreach("transactions.csv", headers: true) do |line|
parsed = Date.parse(line["date"])
if date == parsed
puts "On #{date} You sold #{line["items_sold"]}(s)."
end
end
end
end
def inspect_sales
date_range = prompt "Please enter the date(s) you'd like to inspect. (YYYY-MM-DD)"
list_sales_for(date_range)
end
def general_inquiry
do_this = prompt "Would you like to conduct a transaction (sell), add to the inventory (add), or inspect sales history (inspect)?"
if do_this == "sell"
select_products
elsif do_this == "add"
get_data
elsif do_this == "inspect"
inspect_sales
end
end
@amount_due = 0.0
def list_inventory
@stock = Hash.new
puts "0 [DONE]"
CSV.foreach("Inventory.csv", headers: true) do |line|
@stock.store(line["item"], line["sell_price"])
end
#List inventory
#select from inventory
#select quantity
#pyotr
@stock.each_with_index do |item, index|
puts "#{index+1} #{item}"
end
end
@basket = []
def total
@total = 0.0
@basket.each do |item|
purchase_price = item[:price].to_f*item[:quantity].to_f
@total += purchase_price
end
transaction(@total)
end
def select_products
list_inventory
selection = ""
puts "What product would you like to buy?"
selection = gets.chomp.to_i
if selection == 0
total
else
choice = selection - 1
@stock.each_with_index do |(name, price), index|
if choice == index
quantity = prompt "How many #{name}'s would the customer like?"
item = { name: name, quantity: quantity, price: price }
@basket << item
end
end
select_products
end
end
def write_to_transaction_history(date, time, items_sold, revenue)
CSV.open("transactions.csv", 'a+') do |f|
f << [date, time, items_sold, revenue]
end
end
def log_transaction(date, time, basket, total)
sold = []
basket.each do |item|
product = item[:name]
quantity_sold = item[:quantity]
sold << "#{item[:quantity]} #{item[:name]}(s) sold"
end
revenue = total
write_to_transaction_history(date, time, sold.join(", "), revenue)
end
def transaction(total)
date = Date.today
time = Time.now.strftime("%I:%M%P")
amount_due = total
puts "The customer owes you $#{"%.2f" % amount_due}."
puts "How much money did the customer give you?"
tendered = gets.chomp.to_f
if tendered >= amount_due
change = tendered-amount_due
change = change.to_f
puts "Please return $#{"%.2f" % change} in change to the customer."
log_transaction(date, time, @basket, amount_due)
else
"The customer did not give you enough cash."
end
end
general_inquiry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment