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

電気料金シュミレーション機能の追加 #313

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
42 changes: 42 additions & 0 deletions challengeA/katayama/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'json'
require './simulator'
require './plan'

# 契約アンペアを入力
puts <<~TEXT
契約しているアンペア数を入力して下さい。
(例:30Aの場合は"30"と入力してください)
TEXT

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

# 電気使用量を入力
puts <<~TEXT
次に1ヶ月の電気使用量(kWh)を入力してください。
(例:300kWhの場合は"300"と入力してください)
TEXT


print "電気使用量を入力 > "
kwh = gets.to_i


# プランごとの設定値をJSONファイルから取得
plan_objs = []
File.open("plan_data.json") do |file|
json_data = JSON.load(file, create_additions: true)

# プランごとにインスタンスを生成する
json_data["plan"].map do |plan_obj|
plan_objs << Plan.new(plan_obj)
end
end

# 見積もりシュミレーションを実行する
simulator = Simulator.new(amp,kwh)
simulator.simulate(plan_objs)
102 changes: 102 additions & 0 deletions challengeA/katayama/plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
class Plan
attr_accessor :name, :provider_name, :supported_amp, :base, :as_pay, :is_supported, :min_charge

# 電気料金の計算ロジックにおいて今回適用外としているもの
# ・燃料費調整額及び再生可能エネルギー賦課金
# ・「東京ガス ずっとも電気1」の「ガス・電気セット割(定額A)」- [基本料金から275円(税込み)割引]

# 従量料金表において電気使用量の上限が存在しない場合の値
AS_PAY_CHARGE_RANGE_MAX = 9999

def initialize(args)
# プラン名
@name = args["name"]
# 会社名
@provider_name = args["provider_name"]
# 対応アンペア一覧
@supported_amp = args["supported_amp"]
# 基本料金
@base = args["base"]
# 従量料金
@as_pay = args["as_pay"]
# アンペア対応可否
@is_supported = true
# 月額料金最低額
@min_charge = args["min_charge"]
end

# 電気料金を計算する
def calc_total_charge(amp, kwh)

# 基本料金の計算
base_charge = calc_base_charge(amp, kwh)

# 従量料金の計算
as_pay_charge = calc_as_pay_charge(kwh)

# 電気料金 = 基本料金 + 従量料金(少数切り捨て)
total_charge = (base_charge + as_pay_charge).floor

# 電気料金が月額最低額以下の場合、月額最低額で電気料金を計上する
# (現時点で東京電力エナジーパートナー 従量電灯Bのみ確認)
if !self.min_charge.nil? && self.min_charge > total_charge
total_charge = self.min_charge.floor
end

return total_charge
end

private

# 基本料金の算出を実行する
def calc_base_charge(input_amp, kwh)
base_charge = 0

# 入力されたアンペアがプラン対象かどうか判定
if self.supported_amp.include?(input_amp)

# 入力されたアンペアが対応可の場合、入力された契約アンペア数に対応する基本料金を算出する
target_amp = self.base.find {|b| b["amp"] == input_amp}
base_charge = target_amp["charge"]

# 電気使用量が0kWhの場合、電気料金のうち基本料金が半額になる
if kwh == 0
base_charge = base_charge/2
end

return base_charge
else

# 入力されたアンペアが対応外の場合
self.is_supported = false
return base_charge
end
end

# 従量料金の算出を実行する
# 従量料金 = 従量料金の単価 × 電気使用量(kWh)
def calc_as_pay_charge(input_amount)
range_array = self.as_pay
as_pay_sum = 0

# 電気使用量に対応した従量料金の料金帯を取得
max_range = range_array.find {|a| is_range?(input_amount, a)}
index = range_array.index(max_range)
range_array.each_with_index do |range, idx|

# 電気使用量が従量料金の料金帯における端数の場合
if idx == index && is_range?(input_amount, range)
as_pay_sum += max_range["per_charge"]* (input_amount - max_range["from"])
break
end
as_pay_sum += range["per_charge"]*(range["to"] - range["from"])
end
return as_pay_sum
end

# 従量料金の料金帯に含まれるか否か
def is_range?(value, oppo)
return oppo["from"] <= value && value <= oppo["to"]
end

end
208 changes: 208 additions & 0 deletions challengeA/katayama/plan_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{
"plan": [
{
"name": "従量電灯B",
"provider_name": "東京電力エナジーパートナー",
"supported_amp": [10, 15, 20, 30, 40, 50, 60],
"min_charge": 235.84,
"base": [
{
"amp": 10,
"charge": 286.0
},
{
"amp": 15,
"charge": 429.0
},
{
"amp": 20,
"charge": 572.0
},
{
"amp": 30,
"charge": 858.0
},
{
"amp": 40,
"charge": 1144.0
},
{
"amp": 50,
"charge": 1430.0
},
{
"amp": 60,
"charge": 1716.0
}
],
"as_pay": [
{
"from": 0,
"to": 120,
"per_charge": 19.88
},
{
"from": 120,
"to": 300,
"per_charge": 26.48
},
{
"from": 300,
"to": 9999,
"per_charge": 30.57
}
]
},
{
"name": "おうちプラン",
"supported_amp": [10, 15, 20, 30, 40, 50, 60],
"provider_name": "Loopでんき",
"base": [
{
"amp": 10,
"charge": 0
},
{
"amp": 15,
"charge": 0
},
{
"amp": 20,
"charge": 0
},
{
"amp": 30,
"charge": 0
},
{
"amp": 40,
"charge": 0
},
{
"amp": 50,
"charge": 0
},
{
"amp": 60,
"charge": 0
}
],
"as_pay": [
{
"from": 0,
"to": 9999,
"per_charge": 26.40
}
]
},
{
"name": "ずっとも電気1",
"supported_amp": [30, 40, 50, 60],
"provider_name": "東京ガス",
"base": [
{
"amp": 10,
"charge": 286.0
},
{
"amp": 15,
"charge": 429.0
},
{
"amp": 20,
"charge": 572.0
},
{
"amp": 30,
"charge": 858.0
},
{
"amp": 40,
"charge": 1144.0
},
{
"amp": 50,
"charge": 1430.0
},
{
"amp": 60,
"charge": 1716.0
}
],
"as_pay": [
{
"from": 0,
"to": 140,
"per_charge": 23.67
},
{
"from": 140,
"to": 350,
"per_charge": 23.88
},
{
"from": 350,
"to": 9999,
"per_charge": 26.41
}
]
},
{
"name": "従量電灯Bたっぷりプラン",
"provider_name": "JXTGでんき",
"supported_amp": [30, 40, 50, 60],
"base": [
{
"amp": 10,
"charge": 286.0
},
{
"amp": 15,
"charge": 429.0
},
{
"amp": 20,
"charge": 572.0
},
{
"amp": 30,
"charge": 858.0
},
{
"amp": 40,
"charge": 1144.0
},
{
"amp": 50,
"charge": 1430.0
},
{
"amp": 60,
"charge": 1716.0
}
],
"as_pay": [
{
"from": 0,
"to": 120,
"per_charge": 19.88
},
{
"from": 120,
"to": 300,
"per_charge": 26.48
},
{
"from": 300,
"to": 600,
"per_charge": 25.08
},
{
"from": 600,
"to": 9999,
"per_charge": 26.15
}
]
}
]
}
24 changes: 24 additions & 0 deletions challengeA/katayama/simulator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Simulator
attr_accessor :amp, :kwh

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

# プランごとの見積もりシュミレーションを実行し、結果を表示する
def simulate(plans)
result = []

plans.map do |plan|
total_charge = plan.calc_total_charge(self.amp, self.kwh)
result << {
provider_name: plan.provider_name,
plan_name: plan.name,
price: plan.is_supported ? total_charge : 'アンペア対応外です'
}
end

puts result
end
end