Skip to content

Instantly share code, notes, and snippets.

@RobertKim
Last active October 31, 2016 23:21
Show Gist options
  • Save RobertKim/d54562bf8c809dd302f64ea0073b8a47 to your computer and use it in GitHub Desktop.
Save RobertKim/d54562bf8c809dd302f64ea0073b8a47 to your computer and use it in GitHub Desktop.
Evaluation exercise for ProducePay
#!/usr/bin/ruby
ALL_TRANSACTIONS = []
ALL_CUSTOMERS = []
class Transaction
attr_accessor :id, :customer_id, :description, :amount, :timestamp, :printable_time
def initialize(customer_id, description, amount)
@timestamp = Time.now
@printable_time = @timestamp.strftime("%m/%d/%Y | %H:%M:%S")
@id = Time.now.strftime("%m%d%Y%H%M%S")
@customer_id = customer_id
@description = description
@amount = amount.to_f
ALL_TRANSACTIONS << self
end
end
class Customer
attr_accessor :id, :name, :transactions
def initialize(name)
@id = Time.now.strftime("%m%d%Y%H%M%S")
@name = name
@transactions = self.transactions
ALL_CUSTOMERS << self
end
def transactions
ALL_TRANSACTIONS.select{|x|x.customer_id == @id}
end
def create_associated_transaction(description="Test", amount="500")
Transaction.new(@id,description,amount)
end
def create_ledger
puts ""
puts "SELLER COMPANY\'S LEDGER with #{self.name}"
puts "ACCOUNT => CASH"
puts "DATE | TIME | SELLER | CUSTOMER"
self.transactions.each do |tran|
info_string = "#{tran.printable_time} | #{'%.2f' % [(tran.amount.to_s.to_f * 100).round / 100.0]} DR | #{'%.2f' % [(tran.amount.to_s.to_f * 100).round / 100.0]} CR"
puts info_string
end
amounts_array = self.transactions.map{|x|x.amount.to_f.round(2)}
total_amount = amounts_array.inject(:+)
puts "TOTALS | #{'%.2f' % [(total_amount.to_s.to_f * 100).round / 100.0]} | #{'%.2f' % [(total_amount.to_s.to_f * 100).round / 100.0]}"
end
end
## DRIVER CODE for building Seller Company's Ledger with Customer
#create Customer
begin
puts ""
customer = Customer.new("XYZ Company")
puts "Customer Created | Id => #{customer.id}"
puts ""
10.times do |i|
trans = customer.create_associated_transaction("Test_#{i+1}")
puts "Transaction Created | #{i+1} of 10 | Id => #{trans.id}"
sleep 1
end
rescue => e
puts e
end
puts ""
begin
customer.create_ledger
rescue => error
puts "2nd Error #{error}"
end
puts ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment