-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcasestudy.rb
More file actions
65 lines (47 loc) · 1.49 KB
/
Copy pathcasestudy.rb
File metadata and controls
65 lines (47 loc) · 1.49 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
62
63
64
65
require 'csv'
=begin
dataHash = CSV.read("Customers.csv", { encoding: "UTF-8", headers: true, header_converters: :symbol, converters: :all})
hashed_data = dataHash.map { |d| d.to_hash }
puts hashed_data
dataArray = Array.new
CSV.foreach("Customers.csv", { encoding: "UTF-8", headers: true, header_converters: :symbol, converters: :all}) do |row|
dataArray << row.to_hash
end
puts dataArray
=end
###############################################
custArray = Array.new
custID = ""
innercustID = ""
salesHash = Hash.new
i = 0
CSV.foreach('Customers.csv', { encoding: "UTF-8", headers: true, header_converters: :symbol, converters: :all}) do |row|
amount = 0
i = i + 1
custID = row[10]
if custArray.include?(custID) == false then
amount = amount + row[4].to_f
j = 0
CSV.foreach('Customers.csv', { encoding: "UTF-8", headers: true, header_converters: :symbol, converters: :all}) do |innerrow|
j = j+1
innercustID = innerrow[10]
if innercustID == custID then
if i != j then
amount = amount + innerrow[4].to_f
end
end
end
salesHash[custID] = amount
custArray << custID
end
end
#salesHash.each {|key, value| puts "Amount of #{key} is #{value}"}
CSV.open('output.csv', 'w') do |csv|
csv << ['customerID', 'total sales', 'rank']
newsalesHash = salesHash.sort_by{|key, value| value}.reverse.to_h
k = 1
newsalesHash.each do |key, value|
csv << [key, value, k]
k = k +1
end
end