Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions challengeA/yoshifumi_sawada/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
9 changes: 9 additions & 0 deletions challengeA/yoshifumi_sawada/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# gem "rails"
gem "rspec", ">= 3.0.0"
gem 'pry', '~> 0.13.1'
32 changes: 32 additions & 0 deletions challengeA/yoshifumi_sawada/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.3)
diff-lcs (1.4.4)
method_source (1.0.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)

PLATFORMS
ruby

DEPENDENCIES
pry (~> 0.13.1)
rspec (>= 3.0.0)

BUNDLED WITH
2.1.4
5 changes: 5 additions & 0 deletions challengeA/yoshifumi_sawada/csv/jxtg/basic_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
amp,basic_charge
30,858
40,1144
50,1430
60,1716
5 changes: 5 additions & 0 deletions challengeA/yoshifumi_sawada/csv/jxtg/usage_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kwh_from,kwh_to,unit_price
0,120,19.88
120,300,26.48
300,600,25.08
600,Float::INFINITY,26.15
8 changes: 8 additions & 0 deletions challengeA/yoshifumi_sawada/csv/loop/basic_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
amp,basic_charge
10,0
15,0
20,0
30,0
40,0
50,0
60,0
2 changes: 2 additions & 0 deletions challengeA/yoshifumi_sawada/csv/loop/usage_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
kwh,unit_price
0,26.4
5 changes: 5 additions & 0 deletions challengeA/yoshifumi_sawada/csv/plans.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
provider_name,plan_name
東京電力エナジーパートナー,従量電灯B
Looopでんき,おうちプラン
東京ガス,ずっとも電気1
JXTGでんき,従量電灯B たっぷりプラン
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
amp,basic_charge
10,286
15,429
20,572
30,858
40,1144
50,1430
60,1716
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kwh_from,kwh_to,unit_price
0,120,19.88
120,300,26.48
300,Float::INFINITY,30.57
5 changes: 5 additions & 0 deletions challengeA/yoshifumi_sawada/csv/tokyogas/basic_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
amp,basic_charge
30,858
40,1144
50,1430
60,1716
4 changes: 4 additions & 0 deletions challengeA/yoshifumi_sawada/csv/tokyogas/usage_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kwh_from,kwh_to,unit_price
0,140,23.67
140,350,23.88
350,Float::INFINITY,26.41
39 changes: 39 additions & 0 deletions challengeA/yoshifumi_sawada/lib/basic_charge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "csv"
require "pry"

class Basic_charge
def self.import(path)
if path[:path].include?("tokyo_energy_partner")
CSV.read("csv/tokyo_energy_partner/basic_charge.csv", headers: true).map do |row|
{
amp: row["amp"].to_i,
basic_charge: row["basic_charge"].to_i
}
end

elsif path[:path].include?("loop")
CSV.read("csv/loop/basic_charge.csv", headers: true).map do |row|
{
amp: row["amp"].to_i,
basic_charge: row["basic_charge"].to_i
}
end

elsif path[:path].include?("tokyogas")
CSV.read("csv/tokyogas/basic_charge.csv", headers: true).map do |row|
{
amp: row["amp"].to_i,
basic_charge: row["basic_charge"].to_i
}
end

elsif path[:path].include?("jxtg")
CSV.read("csv/jxtg/basic_charge.csv", headers: true).map do |row|
{
amp: row["amp"].to_i,
basic_charge: row["basic_charge"].to_i
}
end
end
end
end
14 changes: 14 additions & 0 deletions challengeA/yoshifumi_sawada/lib/plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "csv"
require "pry"

class Plan
def self.import(path)
# 全てを読込、配列を作成
CSV.read("csv/plans.csv", headers: true).map do |row|
{
provider_name: row["provider_name"],
plan_name: row["plan_name"]
}
end
end
end
109 changes: 109 additions & 0 deletions challengeA/yoshifumi_sawada/lib/simulator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require "pry"
require_relative "plan"
require_relative "basic_charge"
require_relative "usage_charge"

class Simulator

def initialize(amp,usage_per_month)
@amp = amp
@usage_per_month = usage_per_month
end

def simulate
# 東京電力エナジーパートナー従量電灯Bの電気量料金のCSVデータをインポート
usage_datas = Usage_charge.import(path: "../csv/tokyo_energy_partner/usage_charge.csv")
# 東京電力エナジーパートナーの従量電灯Bの電気量料金
usage_charge1 = calculate(usage_datas)
# 東京ガスのずっとも電気1の電気量料金の電気量料金テーブルのCSVデータをインポート
usage_datas3 = Usage_charge.import(path: "../csv/tokyogas/usage_charge.csv")
# 東京ガスのずっとも電気1の電気量料金
usage_charge3 = calculate(usage_datas3)

# JXTGでんき 従量電灯B たっぷりプランの電気量料金の電気量料金テーブルのCSVデータをインポート
usage_datas4 = Usage_charge.import(path: "../csv/jxtg/usage_charge.csv")
# 東京ガスのずっとも電気1の電気量料金
usage_charge4 = calculate(usage_datas4)

# Looopでんきのおうちプランの電気量料金テーブルのCSVデータをインポート
usage_datas2 = Usage_charge.import(path: "../csv/loop/usage_charge.csv")
# Looopでんきのおうちプランの電気量料金の単価
usage_charge_data2 = usage_datas2.find { |data| data['kwh'] == 0.0 }
unit_price2 = usage_charge_data2["unit_price"]


# 基本料金テーブルのCSVデータをインポート
basic_charge_table = Basic_charge.import(path: "../csv/tokyo_energy_partner/basic_charge.csv")
basic_charge_table2 = Basic_charge.import(path: "../csv/loop/basic_charge.csv")
basic_charge_table3 = Basic_charge.import(path: "../csv/tokyogas/basic_charge.csv")
basic_charge_table4 = Basic_charge.import(path: "../csv/jxtg/basic_charge.csv")


# プロバイダと料金プラン名のCSVデータをインポート
plan_datas = Plan.import(path: "csv/plans.csv")

# 該当プランに価格の計算結果のハッシュを追加
plan_list = plan_datas.each do |data|
case
when data[:provider_name] == "東京電力エナジーパートナー"
charge_data = basic_charge_table.find { |data| data[:amp] == @amp }
data[:price]= (charge_data[:basic_charge] + usage_charge1).floor.to_s

when data[:provider_name] == "Looopでんき"
charge_data2 = basic_charge_table2.find { |data| data[:amp] == @amp }
data[:price]= (charge_data2[:basic_charge] + @usage_per_month * unit_price2).floor.to_s

when data[:provider_name] == "東京ガス" && [30,40,50,60].include?(@amp)
charge_data3 = basic_charge_table3.find { |data| data[:amp] == @amp }
data[:price]= (charge_data3[:basic_charge] + usage_charge3).floor.to_s

when data[:provider_name] == "JXTGでんき" && [30,40,50,60].include?(@amp)
charge_data4 = basic_charge_table4.find { |data| data[:amp] == @amp }
data[:price]= (charge_data4[:basic_charge] + usage_charge4).floor.to_s
end
end

# priceキーに値が入ってるプランのみを表示
p output_array = plan_list.select{|plan| plan.has_key?(:price)}
end

private

# 段階別料金プランの計算
def calculate(usage_datas)
usage_charge = 0
usage_datas.each do |usage_data|
if usage_data['kwh_from'] < @usage_per_month && @usage_per_month <= usage_data['kwh_to']
usage_charge = usage_charge + usage_data['unit_price'] * (@usage_per_month - usage_data['kwh_from'])
elsif usage_data['kwh_to'] < @usage_per_month
usage_charge = usage_charge + usage_data['unit_price'] * (usage_data['kwh_to'] - usage_data['kwh_from'])
end
end
return usage_charge
end
end

# #****************実行部分***************************************************************


# 契約アンペアを入力して頂く
while true
print "契約アンペアを入力して下さい > "
amp = gets.to_i
break if [10,15,20,30,40,50,60].include?(amp)
puts "10,15,20,30,40,50,60のいずれかを入力して下さい"
end

# 1ヶ月あたりの使用量を入力して頂く
while true
print "1ヶ月あたりの使用量を入力して下さい > "
usage_per_month = gets.to_i
break if usage_per_month >= 0
puts "0以上の値を入力して下さい"
end

# 入力値を引数にSimulatorクラスのインスタンスを生成
simulator = Simulator.new(amp,usage_per_month)

# インスタンスに対してsimulateメソッド(計算メソッド)を実行して出力結果を表示
simulator.simulate
62 changes: 62 additions & 0 deletions challengeA/yoshifumi_sawada/lib/usage_charge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "csv"
require "pry"
class Usage_charge
def self.import(path)
if path[:path].include?("tokyo_energy_partner")
list = []
CSV.foreach("csv/tokyo_energy_partner/usage_charge.csv", headers: true) do |row|
list << row.to_h.transform_values{|v|
case v
when "Float::INFINITY"
Float::INFINITY
else
v.to_f
end
}
end
return list

elsif path[:path].include?("loop")
list = []
CSV.foreach("csv/loop/usage_charge.csv", headers: true) do |row|
list << row.to_h.transform_values{|v|
case v
when "Float::INFINITY"
Float::INFINITY
else
v.to_f
end
}
end
return list

elsif path[:path].include?("tokyogas")
list = []
CSV.foreach("csv/tokyogas/usage_charge.csv", headers: true) do |row|
list << row.to_h.transform_values{|v|
case v
when "Float::INFINITY"
Float::INFINITY
else
v.to_f
end
}
end
return list

elsif path[:path].include?("jxtg")
list = []
CSV.read("csv/jxtg/usage_charge.csv", headers: true).map do |row|
list << row.to_h.transform_values{|v|
case v
when "Float::INFINITY"
Float::INFINITY
else
v.to_f
end
}
end
return list
end
end
end
Loading