Last active
September 8, 2017 20:17
-
-
Save jmoon90/7539012 to your computer and use it in GitHub Desktop.
Add feature to original cashier gist.
This file contains hidden or 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
| load "more_feature_cashier.rb" | |
| puts "Welcome to James\' coffee emporium!" | |
| product_list | |
| product_price | |
| choices_for_selection | |
| make_selection | |
| while_loop | |
| sale_complete | |
| amount_tendered | |
| daily_report |
This file contains hidden or 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 'csv' | |
| require 'table_print' | |
| require 'json' | |
| require 'pry' | |
| #The owner at the cash register can | |
| #Enter purchases (S), (M), and (L) | |
| #Without manually entering prices. | |
| #+What type of purchase they want Light, Medium, Bold, or Complete | |
| #+Number of bags to purchase | |
| #+Accept user input | |
| #+Subtotal for each type | |
| #+Total type purchase + quantity | |
| @product_lists = {} | |
| def product_list | |
| i = 1 | |
| CSV.foreach("products.csv", headers: true) do |row| | |
| @product_lists[i] = row | |
| puts "#{i} Add item - #{row["name"]} - #{row["retail_price"]}" | |
| i += 1 | |
| end | |
| end | |
| @coffee_prices = [] | |
| def product_price | |
| @product_lists.each do |list| | |
| @coffee_prices << { list[1]["name"] => list[1]["retail_price"].to_f, "quantity" => 0, "whole_sale" => list[1]["purchasing_price"].to_f } | |
| end | |
| end | |
| def time_format | |
| time = Time.now | |
| "#{time.month}/#{time.day}/#{time.year} #{time.strftime("%I")}:#{time.strftime("%M")}" | |
| end | |
| def thankyou_message | |
| puts "==Thank You!===" | |
| puts "The total change due is $#{@amount_tendered - @total_price }\n" | |
| puts time_format | |
| puts "===============" | |
| end | |
| def choices_for_selection | |
| @product_lists | |
| end | |
| def make_selection | |
| puts "Make a selection: " | |
| print ">" | |
| end | |
| def purchase_order_method | |
| user_input_method | |
| puts "How many bags?" | |
| total_bags_purchased | |
| @coffee_prices[@user_input-1]["quantity"] = @total_bags | |
| end | |
| def while_loop | |
| purchase_order_method | |
| @total_price = @coffee_prices[@user_input-1].values[0..1].inject(:*) | |
| puts "Next line is the total_price" | |
| puts @total_price | |
| while @user_input != "done" || @user_input.to_i != 0 | |
| puts "Subtotal: $#{@total_price}" | |
| puts "What item is being purchased?" | |
| print ">" | |
| @user_input = gets.chomp | |
| break if @user_input.to_i == 0 || @user_input == "done" | |
| puts "How many bags?" | |
| @total_bags = gets.to_i | |
| purchase = @coffee_prices[@user_input.to_i - 1] | |
| @coffee_prices[@user_input.to_i - 1]["quantity"] += @total_bags | |
| @total_price += (purchase.values[0] * @total_bags) | |
| end | |
| end | |
| def sale_complete | |
| puts "===Sale Complete===" | |
| @purchase = [] | |
| @quantity = [] | |
| @retail_price = [] | |
| @purchasing_price = [] | |
| @coffee_prices.each do |value| | |
| puts | |
| print "$#{ value.values[0..1].inject(:*) } -- #{ value["quantity"] } #{ value.keys[0] }" unless value == 0 || value =="done" | |
| current_purchase = value.keys[0] | |
| quantity_purchase = value["quantity"] | |
| selling_price = value[value.keys[0]] | |
| wholesale_price = value["whole_sale"] | |
| if value["quantity"] != 0 | |
| @quantity << quantity_purchase | |
| @purchase << current_purchase | |
| @retail_price << selling_price | |
| @purchasing_price << wholesale_price | |
| end | |
| end | |
| File.open("output.json", 'a+') do |file| | |
| data = { time_format: time_format, purchase: @purchase, quantity: @quantity, retail_price: @retail_price, purchasing_price: @purchasing_price } | |
| file.puts data.to_json | |
| end | |
| end | |
| def amount_tendered | |
| puts "\nTotal: $#{@total_price}" | |
| puts "What is the amount tendered?" | |
| print "> " | |
| @amount_tendered = gets.chomp.to_i | |
| while @total_price > @amount_tendered | |
| puts "Remaining #{@total_price - @amount_tendered}" | |
| @total_price = @total_price - @amount_tendered | |
| puts "What is the amount tendered?" | |
| print "> " | |
| @amount_tendered = gets.chomp.to_i | |
| thankyou_message if @amount_tendered > @total_price | |
| end | |
| end | |
| def daily_report | |
| puts "Would you like to view today\'s sales?- \'y\' or \'n\'" | |
| answer = gets.chomp | |
| if answer == 'y' | |
| report = [] | |
| puts "What date would you like to see? - (m/d/year)" | |
| @review_date = gets.chomp | |
| puts | |
| transaction = File.open('output.json', 'a+').each do |line| | |
| @transaction = JSON.parse(line) | |
| if @transaction["time_format"].include?(@review_date) | |
| report << @transaction | |
| elsif DateTime.strptime(@review_data, "%m/%d/%Y").to_time > Time.now | |
| puts "Future date can not be found. Error. Error." | |
| else | |
| puts "Wrong input. Error" | |
| break | |
| end | |
| end | |
| tp report | |
| i = 0 | |
| @gross_sales = 0 | |
| @net_profit = 0 | |
| report.each do |transaction| | |
| keys = [] | |
| i+=1 | |
| @trans_total = 0 | |
| @net_total = 0 | |
| transaction["purchase"].each do |item_purchase| | |
| keys << item_purchase | |
| end | |
| i = 0 | |
| keys.each_with_index do |item_name, index| | |
| @trans_total += (transaction["retail_price"][index] * transaction["quantity"][index]) | |
| @net_total += (transaction["purchasing_price"][index] * transaction["quantity"][index] ) | |
| i += 1 | |
| end | |
| @gross_sales += @trans_total | |
| @net_profit += @net_total | |
| end | |
| puts "Gross_sales #{@gross_sales}" | |
| puts "Net profit #{@gross_sales - @net_profit}" | |
| end | |
| end | |
| def user_input_method | |
| @user_input = gets.chomp.to_i | |
| end | |
| def total_bags_purchased | |
| @total_bags = gets.to_i | |
| end |
This file contains hidden or 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
| {"time_format":"11/23/2013 08:07","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:08","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:08","purchase":["light","medium","bold","javachip"],"quantity":[1,1,1,1],"retail_price":[5.0,7.5,9.75,13.0],"purchasing_price":[3.0,4.0,5.0,8.0]} | |
| {"time_format":"11/23/2013 08:12","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:13","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:18","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:31","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:32","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:33","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:34","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:41","purchase":["light"],"quantity":[2],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:46","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} | |
| {"time_format":"11/23/2013 08:47","purchase":["light"],"quantity":[1],"retail_price":[5.0],"purchasing_price":[3.0]} |
This file contains hidden or 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
| name | sku | retail_price | purchasing_price | |
|---|---|---|---|---|
| light | 0001 | 5.00 | 3.00 | |
| medium | 0002 | 7.50 | 4.00 | |
| bold | 0003 | 9.75 | 5.00 | |
| javachip | 0004 | 13.00 | 8.00 |
This file contains hidden or 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
| # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment