-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.rb
More file actions
61 lines (53 loc) · 1.52 KB
/
report.rb
File metadata and controls
61 lines (53 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'fileutils'
# Class that generates reports for the processed payments
# Processes each card details
# Returns a report of the processed payments
class Report
attr_accessor :payments, :amount_by_card_type
FILENAME = "#{Dir.getwd}/inputs.csv"
def initialize(payments)
@payments = payments
@amount_by_card_type = {}
end
# Prints report of the processed payments
def print
rename_report_file
{
total_payments: total_payments,
total_dollar_amount_processed: total_dollar_amount_processed,
amount_by_card_type: get_amount_by_card_type
}
end
private
# Returns the total payments processed
def total_payments
@payments.count
end
# Returns the total dollar amount payments processed
def total_dollar_amount_processed
@payments.inject(0.0) { |total, arr| total + cent_to_dollar(arr[5]) }
end
# Returns the total dollar amount payments processed per card type
def get_amount_by_card_type
@payments.each do |card|
amount = cent_to_dollar(card[5])
if @amount_by_card_type.key?(card[6])
@amount_by_card_type[card[6]] += amount
else
@amount_by_card_type[card[6]] =
amount
end
end
@amount_by_card_type
end
# Renames the input.csv file that was processed
def rename_report_file
if File.exists?(FILENAME)
new_filename = "#{Dir.getwd}/#{Time.now.strftime("%Y-%m-%d %H:%M")}.csv"
FileUtils.mv(FILENAME, new_filename)
end
end
def cent_to_dollar(amount)
amount.to_f / 100
end
end