-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ivan Shishkov #21
base: master
Are you sure you want to change the base?
Ivan Shishkov #21
Changes from 1 commit
f839f67
2e38fc3
272c815
c8d36df
ca632ed
e807eb3
e97252f
08f2804
f5c6c03
a0fbdbb
f2d625d
08426dd
613e169
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'roo'; | ||
|
||
require 'roo-xls'; | ||
|
||
require './WorkWithData.rb'; | ||
|
||
funcs = FilesParse.new; | ||
months = { | ||
'январь' => 1, | ||
'февраль' => 2, | ||
'март' => 3, | ||
'апрель' => 4, | ||
'май' => 5, | ||
'июнь' => 6, | ||
'июль' => 7, | ||
'авуст' => 8, | ||
'сентябрь' => 9, | ||
'октябрь' => 10, | ||
'ноябрь' => 11, | ||
'декабрь' => 12, | ||
} | ||
|
||
regions = ['Brest', | ||
'Vitebsk', | ||
'Gomel', | ||
'Grodno', | ||
'Minsk', | ||
'Minsk Region', | ||
'Mogilyov'] | ||
products = Hash.new | ||
filePaths = Dir["./data/*"] | ||
|
||
filePaths.each{ |filePath| | ||
fileInstance = funcs.getFile(filePath) | ||
|
||
if fileInstance | ||
funcs.fetchProductsData(fileInstance, products, regions) | ||
end | ||
} | ||
loop { | ||
puts "What price are you looking for?" | ||
|
||
word = gets.chomp.downcase; | ||
|
||
keys = funcs.findKeys(word, products); | ||
|
||
if keys.length == 0 | ||
puts word.capitalize + " can not be found in database" | ||
else | ||
keys.each{|key| | ||
puts "" | ||
recentPriceData = funcs.getRecentPriceData(key, products, months) | ||
puts key.capitalize + " is " + recentPriceData["price"].to_s + " BYN in Minsk these days." | ||
minPrice = funcs.getMinPrice(products[key]) | ||
puts "Lowest was on " + minPrice["year"] + | ||
"/" + months[minPrice["month"]].to_s + | ||
" at price " + minPrice["price"].to_s + " BYN" | ||
maxPrice = funcs.getMaxPrice(products[key]) | ||
puts "Maximum was on " + maxPrice["year"] + | ||
"/" + months[maxPrice["month"]].to_s + | ||
" at price " + maxPrice["price"].to_s + " BYN"; | ||
} | ||
|
||
end | ||
puts; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
class FilesParse | ||
def initialize | ||
puts "work"; | ||
end | ||
def getFile(filePath) | ||
extention = filePath.split(".")[2]; | ||
case extention; | ||
when "xls" | ||
fileInstance = Roo::Excel.new(filePath); | ||
return fileInstance; | ||
when "xlsx" | ||
fileInstance = Roo::Excelx.new(filePath); | ||
return fileInstance; | ||
else | ||
puts "unsupported file type: " + filePath; | ||
return nil; | ||
end | ||
end | ||
|
||
def findKeys(word, products) | ||
result = []; | ||
|
||
products.keys.each{ |key| | ||
if(key.include?(word)) | ||
result.push(key); | ||
end | ||
} | ||
return result; | ||
end | ||
|
||
def fetchProductsData(fileInstance, products, regions) | ||
|
||
for n in 9..fileInstance.last_row | ||
if fileInstance.cell("E", n) == nil | ||
next; | ||
end | ||
|
||
year = fileInstance.cell("A", 3).split(" ")[2]; | ||
month = fileInstance.cell("A", 3).split(" ")[1]; | ||
|
||
key = fileInstance.cell("A", n).strip.downcase; | ||
|
||
if !products[key] | ||
products[key] = Hash.new; | ||
end | ||
if !products[key][year] | ||
products[key][year] = Hash.new; | ||
end | ||
|
||
products[key][year][month] = { | ||
regions[0] => formatValue(fileInstance.cell("G", n), year), | ||
regions[1] => formatValue(fileInstance.cell("I", n), year), | ||
regions[2] => formatValue(fileInstance.cell("K", n), year), | ||
regions[3] => formatValue(fileInstance.cell("M", n), year), | ||
regions[4] => formatValue(fileInstance.cell("O", n), year), | ||
regions[5] => formatValue(fileInstance.cell("Q", n), year), | ||
regions[6] => formatValue(fileInstance.cell("S", n), year) | ||
} | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. |
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceBeforeBlockBraces: Space missing to the left of {. |
||
def formatValue(val, year) | ||
if val | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression. |
||
result = val.to_f; | ||
|
||
if year.to_i < 2017 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||. |
||
result = result / 10000; | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/SelfAssignment: Use self-assignment shorthand /=. |
||
|
||
return result.round(2); | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/SelfAssignment: Use self-assignment shorthand /=. |
||
end | ||
|
||
def getRecentPriceData(key, products, months) | ||
currentYear = Time.now.strftime("%Y").to_s; | ||
|
||
currentMonth = Time.now.strftime("%m"); | ||
|
||
months.each{|month, monthNumber| | ||
if monthNumber == currentMonth | ||
currentMonth == month | ||
end | ||
} | ||
|
||
productYearData = products[key]; | ||
|
||
if productYearData[currentYear] | ||
yearKey = currentYear; | ||
else | ||
yearKey = productYearData.keys.max{|a,b| a.to_i <=> b.to_i} | ||
end | ||
|
||
productMonthData = productYearData[yearKey]; | ||
|
||
if productMonthData[currentMonth] | ||
monthKey = currentMonth; | ||
else | ||
monthKey = productMonthData.keys.max{|a,b| months[a] <=> months[b]} | ||
end | ||
|
||
return { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. |
||
"price" => productMonthData[monthKey]["Minsk"], | ||
"year" => yearKey, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
"month" => monthKey | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. |
||
} | ||
end | ||
|
||
def getMinPrice(hash) | ||
result = Hash.new; | ||
minYearPrice = 9999999999999; | ||
|
||
hash.each{|year, yearHash| | ||
minMonthPrice = 9999999999999; | ||
yearHash.each{|month, monthHash| | ||
price = monthHash['Minsk'] | ||
|
||
if(price < minMonthPrice) | ||
minMonthPrice = price | ||
result['month'] = month; | ||
end | ||
} | ||
if(minMonthPrice < minYearPrice) | ||
minYearPrice = minMonthPrice; | ||
result['year'] = year; | ||
result['price'] = minYearPrice; | ||
end | ||
} | ||
return result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. |
||
end | ||
|
||
def getMaxPrice(hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/MethodLength: Method has too many lines. [18/10] |
||
result = Hash.new; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/EmptyLiteral: Use hash literal {} instead of Hash.new. |
||
maxYearPrice = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. |
||
|
||
hash.each{|year, yearHash| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceBeforeBlockBraces: Space missing to the left of {. |
||
maxMonthPrice = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. |
||
yearHash.each{|month, monthHash| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceBeforeBlockBraces: Space missing to the left of {. |
||
price = monthHash['Minsk'] | ||
|
||
if(price > maxMonthPrice) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceAroundKeyword: Space after keyword if is missing. |
||
maxMonthPrice = price; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. |
||
result['month'] = month; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/Semicolon: Do not use semicolons to terminate expressions. |
||
end | ||
} | ||
if(maxMonthPrice > maxYearPrice) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/SpaceAroundKeyword: Space after keyword if is missing. |
||
maxYearPrice = maxMonthPrice; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming/VariableName: Use snake_case for variable names. |
||
result['year'] = year; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/Semicolon: Do not use semicolons to terminate expressions. |
||
result['price'] = maxYearPrice; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/Semicolon: Do not use semicolons to terminate expressions. |
||
end | ||
} | ||
return result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/RedundantReturn: Redundant return detected. |
||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/TrailingBlankLines: Final newline missing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/EndOfLine: Carriage return character detected.
Metrics/ClassLength: Class has too many lines. [157/100]