Skip to content
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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Binary file not shown.
Binary file not shown.
66 changes: 66 additions & 0 deletions Shishkov_Ivan/0/Project.rb
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;
}
153 changes: 153 additions & 0 deletions Shishkov_Ivan/0/WorkWithData.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
class FilesParse

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]

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Style/BlockDelimiters: Avoid using {...} for multi-line blocks.
Naming/UncommunicativeBlockParamName: Only use lowercase characters for block parameter.
Naming/VariableName: Use snake_case for variable names.

def formatValue(val, year)
if val

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SelfAssignment: Use self-assignment shorthand /=.
Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.


return result.round(2);
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SelfAssignment: Use self-assignment shorthand /=.
Style/NumericLiterals: Use underscores(_) as decimal mark and separate every 3 digits with them.

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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.

"price" => productMonthData[monthKey]["Minsk"],
"year" => yearKey,

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.

end

def getMaxPrice(hash)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/MethodLength: Method has too many lines. [18/10]
Naming/MethodName: Use snake_case for method names.

result = Hash.new;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/EmptyLiteral: Use hash literal {} instead of Hash.new.
Style/Semicolon: Do not use semicolons to terminate expressions.

maxYearPrice = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.
Style/Semicolon: Do not use semicolons to terminate expressions.


hash.each{|year, yearHash|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Style/BlockDelimiters: Avoid using {...} for multi-line blocks.
Naming/UncommunicativeBlockParamName: Only use lowercase characters for block parameter.
Naming/VariableName: Use snake_case for variable names.

maxMonthPrice = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.
Style/Semicolon: Do not use semicolons to terminate expressions.

yearHash.each{|month, monthHash|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.
Style/BlockDelimiters: Avoid using {...} for multi-line blocks.
Naming/UncommunicativeBlockParamName: Only use lowercase characters for block parameter.
Naming/VariableName: Use snake_case for variable names.

price = monthHash['Minsk']

if(price > maxMonthPrice)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundKeyword: Space after keyword if is missing.

maxMonthPrice = price;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.
Style/Semicolon: Do not use semicolons to terminate expressions.

result['month'] = month;

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAroundKeyword: Space after keyword if is missing.
Style/Next: Use next to skip iteration.

maxYearPrice = maxMonthPrice;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.
Style/Semicolon: Do not use semicolons to terminate expressions.

result['year'] = year;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Semicolon: Do not use semicolons to terminate expressions.

result['price'] = maxYearPrice;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/Semicolon: Do not use semicolons to terminate expressions.

end
}
return result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantReturn: Redundant return detected.

end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/TrailingBlankLines: Final newline missing.