diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2014-11-14 09:45:23 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2014-11-14 09:55:38 +0100 |
commit | 20e52cca19d06d84ca6f3e4294549022dc7518a7 (patch) | |
tree | 94f8fc38d177ca0ae0e5523fd591f07b43814221 | |
parent | 6ed672e4439cf6be54781dd87abcc815d09cd5d1 (diff) | |
download | imusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.tar.gz imusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.tar.bz2 imusician-reports-20e52cca19d06d84ca6f3e4294549022dc7518a7.zip |
Report sales by country.
-rw-r--r-- | lib/reports/sales_by_country.rb | 36 | ||||
-rw-r--r-- | lib/sale.rb | 5 | ||||
-rw-r--r-- | parse_report.rb | 4 |
3 files changed, 42 insertions, 3 deletions
diff --git a/lib/reports/sales_by_country.rb b/lib/reports/sales_by_country.rb new file mode 100644 index 0000000..58f1b15 --- /dev/null +++ b/lib/reports/sales_by_country.rb @@ -0,0 +1,36 @@ +module SalesReporter + module Reports + class SalesByCountry + def self.render(sales) + sales_by_country = {} + + sales.each do |s| + c = s.country + if sales_by_country[c] + sales_by_country[c][:count] += s.quantity + sales_by_country[c][:total] += s.amount + else + sales_by_country[c] = {:count => s.quantity, :total => s.amount} + end + end + + sorted = sales_by_country.sort { |a,b| b[1][:count] <=> a[1][:count] } + + puts "Country: Streams: Revenue:" + puts "------------------------------------------" + + total_rev = 0 + total_streams = 0 + + sorted.each do |key, row| + puts "#{key}: #{row[:count].to_s.rjust(7)} #{row[:total].round(5).to_s.rjust(10)} EUR" + total_rev += row[:total] + total_streams += row[:count] + end + + puts "------------------------------------------" + puts "Total: #{total_streams.to_s.rjust(7)} #{total_rev.round(5).to_s.rjust(10)} EUR" + end + end + end +end diff --git a/lib/sale.rb b/lib/sale.rb index f02a471..057927c 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,12 +1,13 @@ module SalesReporter class Sale - attr_reader :date, :amount, :quantity + attr_reader :date, :amount, :quantity, :country - def initialize(date_, amount, quantity) + def initialize(date_, amount, quantity, country) @date = sanitize_date(date_) raise TypeError('sanitize failed') unless @date.is_a? Date @amount = amount @quantity = quantity + @country = country end private diff --git a/parse_report.rb b/parse_report.rb index 40df679..5410e05 100644 --- a/parse_report.rb +++ b/parse_report.rb @@ -1,5 +1,6 @@ require 'sale' require 'reports/sales_by_date' +require 'reports/sales_by_country' require 'csv' csv_opts = { @@ -13,8 +14,9 @@ sales = [] CSV.foreach(ARGV[0], csv_opts) do |row| if row.count > 0 - sales << SalesReporter::Sale.new(row["Date"], row["revenue EUR"], row["quantity"]) + sales << SalesReporter::Sale.new(row["Date"], row["revenue EUR"], row["quantity"], row["Country"]) end end SalesReporter::Reports::SalesByDate.render(sales) +SalesReporter::Reports::SalesByCountry.render(sales) |