Skip to content

Commit

Permalink
Merge pull request #3 from yousamaaa/dev
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
Muhammad Usama Yousaf authored Sep 3, 2023
2 parents 1323444 + 3159649 commit fb6943e
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
47 changes: 47 additions & 0 deletions file_handling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require 'date'
require 'csv'
require_relative 'stats'

include Stats

# This module will handle files it will command line argumnets and save them in an array format
module FileHandling
# This class saves all the data of files
class Collection
def initialize
@collection = []
end

def self.extract_file_names(choice, year_month, path)
case choice
when 'e'
Collection.new.filter_files(Dir["#{path}/**/*.txt"], year_month)
when 'a'
Collection.new.filter_files([] << "#{path}/#{Collection.new.appending_filename(year_month, path)}.txt", year_month)
when 'c'
Collection.new.filter_files([] << "#{path}/#{Collection.new.appending_filename(year_month, path)}.txt", year_month)
when 'b'
Collection.new.filter_files([] << "#{path}/#{Collection.new.appending_filename(year_month, path)}.txt", year_month)
end
end

def filter_files(files, year_month)
files.delete_if { |f| !f.include?(year_month) } if files.length != 1
extract_data(files)
end

def extract_data(files)
files.each do |f|
@collection << CSV.parse(File.read(f), headers: true)
end
@collection
end

def appending_filename(year_month, path)
appended = path.split('/')[-1] << '_' << year_month.split('/')[0] << '_'
appended << Date::ABBR_MONTHNAMES[year_month.split('/')[-1].to_i]
end
end
end
47 changes: 47 additions & 0 deletions printing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require_relative 'stats'
require 'date'
require 'colorize'

include Stats

# This module will output all the stats to the screen
module Printing
# This class outputs the results on screen
class Output
def self.case_e(collection)
puts "Highest: #{Result.highest_temperature(collection)[0]}C on #{Date::MONTHNAMES[Result.highest_temperature(collection)[1].split('-')[1].to_i]} #{Result.highest_temperature(collection)[1].split('-')[2]}"
puts "Lowest: #{Result.lowest_temperature(collection)[0]}C on #{Date::MONTHNAMES[Result.lowest_temperature(collection)[1].split('-')[1].to_i]} #{Result.lowest_temperature(collection)[1].split('-')[2]}"
puts "Humid: #{Result.most_humid(collection)[0]}% on #{Date::MONTHNAMES[Result.most_humid(collection)[1].split('-')[1].to_i]} #{Result.most_humid(collection)[1].split('-')[2]}"
end

def self.case_a(collection)
puts "Highest Average: #{Result.highest_average(collection)}C"
puts "Lowest Average: #{Result.lowest_average(collection)}C"
puts "Average Humidity: #{Result.humidity_average(collection)}%"
end

def self.case_c(collection, year_month)
max_min = Result.month_high_low(collection)
puts "#{Date::MONTHNAMES[year_month.split('/')[-1].to_i]} #{year_month.split('/')[0]}"
max_min[0].length.times do |n|
max_min.length.times do |m|
max_min[m][n].times { print '+'.red } if m.zero?
max_min[m][n].times { print '+'.blue } unless m.zero?
puts "#{max_min[m][n]}C"
end
end
end

def self.case_b(collection, year_month)
max_min = Result.month_high_low(collection)
puts "#{Date::MONTHNAMES[year_month.split('/')[-1].to_i]} #{year_month.split('/')[0]}"
max_min[0].length.times do |n|
max_min[1][n].times { print '+'.blue }
max_min[0][n].times { print '+'.red }
puts "#{max_min[1][n]}C - #{max_min[0][n]}C"
end
end
end
end
80 changes: 80 additions & 0 deletions stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

# This module will calculate all the stats
module Stats
# This class calculates and send all the results to Output class
class Result
def self.highest_temperature(collection)
local_max = []
max_dates = []
collection.each do |file|
local_max << file['Max TemperatureC'].compact.map(&:to_i).max
max_dates << file.by_col[0][Result.new.max_index(file['Max TemperatureC'])]
end
[local_max.max, max_dates[Result.new.max_index(local_max)]]
end

def self.lowest_temperature(collection)
local_min = []
min_dates = []
collection.each do |file|
local_min << file['Min TemperatureC'].compact.map(&:to_i).min
min_dates << file.by_col[0][Result.new.min_index(file['Min TemperatureC'])]
end
[local_min.min, min_dates[Result.new.min_index(local_min)]]
end

def self.most_humid(collection)
local_max = []
max_dates = []
collection.each do |file|
local_max << file['Max Humidity'].compact.map(&:to_i).max
max_dates << file.by_col[0][Result.new.max_index(file['Max Humidity'])]
end
[local_max.max, max_dates[Result.new.max_index(local_max)]]
end

def self.highest_average(collection)
local_max = []
collection.each do |file|
local_max << Result.new.calculate_avg(file['Max TemperatureC'])
end
local_max.max
end

def self.lowest_average(collection)
local_max = []
collection.each do |file|
local_max << Result.new.calculate_avg(file['Min TemperatureC'])
end
local_max.max
end

def self.humidity_average(collection)
local_max = []
collection.each do |file|
local_max << Result.new.calculate_avg(file[' Mean Humidity'])
end
local_max.max
end

def self.month_high_low(collection)
local_max = collection[0]['Max TemperatureC'].compact.map(&:to_i)
local_min = collection[0]['Min TemperatureC'].compact.map(&:to_i)
[local_max, local_min]
end

def max_index(column)
column.map(&:to_i).each_with_index.max[1]
end

def min_index(column)
column.map(&:to_i).each_with_index.min[1]
end

def calculate_avg(column)
array_length = column.compact.length
column.compact.map(&:to_i).inject { |sum, el| sum + el }.to_f / array_length
end
end
end
30 changes: 30 additions & 0 deletions weatherman.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require_relative 'file_handling'
require_relative 'stats'
require_relative 'printing'

include FileHandling
include Stats
include Printing

# This is the main module
module Weatherman
# This is the main class
class Main
choice = ARGV[0][-1]
year_month = ARGV[1]
path = ARGV[2]
collection = Collection.extract_file_names(choice, year_month, path)
case choice
when 'e'
Output.case_e(collection)
when 'a'
Output.case_a(collection)
when 'c'
Output.case_c(collection, year_month)
when 'b'
Output.case_b(collection, year_month)
end
end
end

0 comments on commit fb6943e

Please sign in to comment.