Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Created November 22, 2013 21:15
Show Gist options
  • Save vanmichael/7607018 to your computer and use it in GitHub Desktop.
Save vanmichael/7607018 to your computer and use it in GitHub Desktop.
Challenge 4
require './register.rb'
require 'pry'
cashier = Register.new
#Load Inventory List from CSV
cashier.load_products("products.csv")
#Display Menu
#cashier.display_menu
#Make Selection
#cashier.get_selection
#If start new transaction
while cashier.get_item != 'done' do
cashier.get_item_qty
cashier.calculate_subtotal
cashier.add_to_invoice
end
cashier.get_date_time
cashier.add_to_csv
require 'csv'
require 'pry'
require 'Date'
class Register
attr_reader :products, :item, :invoice
def initialize
@products = {}
@invoice = {}
@subtotal = 0
end
def load_products(file)
CSV.foreach(file, headers: true) do |line|
@products[line["SKU"]] = {name: line["Name"], Retail_Price: line["Retail_Price"], Purchasing_Price: line["Purchasing_Price"]}
end
end
def display_menu
puts "Please make a selection"
puts "Enter 1 to start a new transaction!"
puts "Enter 2 to retrieve a daily report!"
end
def get_selection
puts 'Make Your Selection!'
@menu_selection = gets.chomp
end
def get_item_qty
puts 'Enter how many!'
@qty = gets.chomp
end
def get_item
puts 'Enter an Item!'
@item = gets.chomp
end
def calculate_subtotal
@subtotal += (@products[@item][:Retail_Price].to_f * @qty.to_f)
end
def add_to_invoice
@invoice[@item] = {} if @invoice[@item].nil?
@invoice[@item][:type] = @products[@item][:name]
@invoice[@item][:qty] = 0 if @invoice[@item][:qty].nil?
@invoice[@item][:qty] += @qty.to_i
end
def display_invoice
puts @invoice
end
def get_date_time
@date = Time.new
@date = "#{@date.year}#{@date.month}#{@date.day}"
#@time = "#{@date.hour}#{@date.min}"
end
def add_to_csv
CSV.open("invoices.csv", "a+") do |csv|
@invoice.each do |key,value|
csv << ["#{@date}", "#{key}", "#{}"
end
end
@invoice = {}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment