-
Notifications
You must be signed in to change notification settings - Fork 193
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
challengeA/okamoto #314
Open
mochikichi
wants to merge
12
commits into
enechange:master
Choose a base branch
from
mochikichi:challengeA/okamoto
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
challengeA/okamoto #314
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
97c6af5
first commit
mochikichi 9389a72
simulatorの実装
mochikichi d410aff
specの重複削除
mochikichi e7e777e
plans読み込みをJSONファイルからできるように
mochikichi 85f8838
入力値を標準入力から受け取るように修正
mochikichi 7b78da7
README追加
mochikichi ebb77b1
planクラスを別ファイルへ分離
mochikichi c94a9b9
参考文献をREADMEに追加
mochikichi 1cafae6
契約アンペア対象がない場合のテストを追加
mochikichi e2f9b7f
PlanクラスをPlansとPlanへ分離
mochikichi 2efa019
specファイル名を変更
mochikichi 3d2a545
mainのtopレベルでのreturnはやめる
mochikichi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/spec/examples.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source "https://rubygems.org" | ||
|
||
gem 'rspec' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
diff-lcs (1.4.4) | ||
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 | ||
x64-mingw32 | ||
|
||
DEPENDENCIES | ||
rspec | ||
|
||
BUNDLED WITH | ||
2.2.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# CHALLENGE A | ||
|
||
## 概要 | ||
|
||
契約アンペア(A)と月の電力使用量(kWh)の入力に応じて、各プラン名と料金のシミュレーションを行うスクリプトです。 | ||
|
||
## シミュレーション実行方法 | ||
|
||
```sh | ||
ruby main.rb | ||
``` | ||
|
||
## データの編集 | ||
|
||
JSON形式で`/data`のディレクトリに格納しています。プラン追加や編集の際はJSONデータを修正することで対応できるように実装しています。 | ||
|
||
## テスト | ||
|
||
```sh | ||
bundle exec rspec | ||
``` | ||
|
||
## 補足事項 | ||
|
||
- 合計電気料金は小数点以下切り捨て | ||
|
||
## 参考文献 | ||
|
||
- オブジェクト指向設計実践ガイド | ||
- [使えるRSpec入門](https://qiita.com/jnchito/items/42193d066bd61c740612) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
[ | ||
{ | ||
"name": "従量電灯B", | ||
"provider_name": "東京電力エナジーパートナー", | ||
"basic_price": [ | ||
{ | ||
"ampere": 10, | ||
"price": 286.0 | ||
}, | ||
{ | ||
"ampere": 15, | ||
"price": 429.0 | ||
}, | ||
{ | ||
"ampere": 20, | ||
"price": 572.0 | ||
}, | ||
{ | ||
"ampere": 30, | ||
"price": 858.0 | ||
}, | ||
{ | ||
"ampere": 40, | ||
"price": 1144.0 | ||
}, | ||
{ | ||
"ampere": 50, | ||
"price": 1430.0 | ||
}, | ||
{ | ||
"ampere": 60, | ||
"price": 1716.0 | ||
} | ||
], | ||
"pay_per_use_price": [ | ||
{ | ||
"price_per_kwh": 19.88, | ||
"min_kwh": 0 | ||
}, | ||
{ | ||
"price_per_kwh": 26.48, | ||
"min_kwh": 120 | ||
}, | ||
{ | ||
"price_per_kwh": 30.57, | ||
"min_kwh": 300 | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "おうちプラン", | ||
"provider_name": "Looopでんき", | ||
"basic_price": [ | ||
{ | ||
"ampere": 10, | ||
"price": 0 | ||
}, | ||
{ | ||
"ampere": 15, | ||
"price": 0 | ||
}, | ||
{ | ||
"ampere": 20, | ||
"price": 0 | ||
}, | ||
{ | ||
"ampere": 30, | ||
"price": 0 | ||
}, | ||
{ | ||
"ampere": 40, | ||
"price": 0 | ||
}, | ||
{ | ||
"ampere": 50, | ||
"price": 0 | ||
}, | ||
{ | ||
"ampere": 60, | ||
"price": 0 | ||
} | ||
], | ||
"pay_per_use_price": [ | ||
{ | ||
"price_per_kwh": 26.40, | ||
"min_kwh": 0 | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "ずっとも電気1", | ||
"provider_name": "東京ガス", | ||
"basic_price": [ | ||
{ | ||
"ampere": 30, | ||
"price": 858.0 | ||
}, | ||
{ | ||
"ampere": 40, | ||
"price": 1144.0 | ||
}, | ||
{ | ||
"ampere": 50, | ||
"price": 1430.0 | ||
}, | ||
{ | ||
"ampere": 60, | ||
"price": 1716.0 | ||
} | ||
], | ||
"pay_per_use_price": [ | ||
{ | ||
"price_per_kwh": 23.67, | ||
"min_kwh": 0 | ||
}, | ||
{ | ||
"price_per_kwh": 23.88, | ||
"min_kwh": 140 | ||
}, | ||
{ | ||
"price_per_kwh": 26.41, | ||
"min_kwh": 350 | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "従量電灯Bたっぷりプラン", | ||
"provider_name": "JXTGでんき", | ||
"basic_price": [ | ||
{ | ||
"ampere": 30, | ||
"price": 858.0 | ||
}, | ||
{ | ||
"ampere": 40, | ||
"price": 1144.0 | ||
}, | ||
{ | ||
"ampere": 50, | ||
"price": 1430.0 | ||
}, | ||
{ | ||
"ampere": 60, | ||
"price": 1716.8 | ||
} | ||
], | ||
"pay_per_use_price": [ | ||
{ | ||
"price_per_kwh": 19.88, | ||
"min_kwh": 0 | ||
}, | ||
{ | ||
"price_per_kwh": 26.48, | ||
"min_kwh": 120 | ||
}, | ||
{ | ||
"price_per_kwh": 25.08, | ||
"min_kwh": 300 | ||
}, | ||
{ | ||
"price_per_kwh": 26.15, | ||
"min_kwh": 600 | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'json' | ||
JSON_FILE_PATH = '../data/plans.json' | ||
|
||
class Plan | ||
attr_reader :contract_amp, :power_usage, :plans | ||
|
||
def initialize(contract_amp, power_usage) | ||
@plans = json_load | ||
@contract_amp = contract_amp | ||
@power_usage = power_usage | ||
end | ||
|
||
def show_plans | ||
plans.map { |plan| available?(plan)? plan_with_price(plan) : nil }.compact | ||
end | ||
|
||
private | ||
|
||
def json_load | ||
json_file_path = File.expand_path(JSON_FILE_PATH, __dir__) | ||
JSON.load(File.open(json_file_path)) | ||
end | ||
|
||
def available?(plan) | ||
plan['basic_price'].find { |price| price['ampere'] == contract_amp } | ||
end | ||
|
||
def plan_with_price(plan) | ||
{ | ||
provider_name: plan['provider_name'], | ||
plan_name: plan['name'], | ||
price: sum_price(plan) | ||
} | ||
end | ||
|
||
def sum_price(plan) | ||
(basic_price(plan) + pay_per_use_price(plan)).floor | ||
end | ||
|
||
def basic_price(plan) | ||
plan['basic_price'].find { |price| price['ampere'] == contract_amp }['price'] | ||
end | ||
|
||
def pay_per_use_price(plan) | ||
sum = 0 | ||
power_usage_before_stage = 0 | ||
sorted_pay_per_use_price_lists(plan).each do |price_list| | ||
power_usage_current_stage = power_usage - price_list['min_kwh'] - power_usage_before_stage | ||
#丸め誤差が生じるため小数点第四位を四捨五入とする | ||
sum += (price_list['price_per_kwh'] * power_usage_current_stage).round(3) | ||
power_usage_before_stage += power_usage_current_stage | ||
end | ||
sum | ||
end | ||
|
||
def sorted_pay_per_use_price_lists(plan) | ||
pay_per_use_price_lists(plan).sort_by {|list| list['min_kwh']}.reverse | ||
end | ||
|
||
def pay_per_use_price_lists(plan) | ||
plan['pay_per_use_price'].select { |price| price['min_kwh'] <= power_usage } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require './lib/plan' | ||
|
||
class Simulator | ||
attr_reader :contract_amp, :power_usage, :plans | ||
|
||
def initialize(contract_amp, power_usage) | ||
@contract_amp = contract_amp | ||
@power_usage = power_usage | ||
@plans = Plan.new(contract_amp, power_usage) | ||
end | ||
|
||
def simulate | ||
plans.show_plans | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require './lib/simulator' | ||
|
||
AVAILABLE_AMP = [10, 15, 20, 30, 40, 50, 60] | ||
|
||
puts '契約アンペア数を数字のみで入力してください。' | ||
puts "#{AVAILABLE_AMP}の中から選択してください。" | ||
contract_amp = gets.to_i | ||
|
||
unless AVAILABLE_AMP.include?(contract_amp) | ||
puts '契約アンペア数の入力が不正です。' | ||
exit | ||
end | ||
|
||
puts '電力使用量(kWh)を整数のみで入力してください' | ||
power_usage = gets.to_i | ||
|
||
if power_usage.negative? | ||
puts '電力使用量(kWh)は0以上を入力してください。' | ||
exit | ||
end | ||
|
||
simulator = Simulator.new(contract_amp, power_usage) | ||
plan_lists = simulator.simulate | ||
|
||
return puts '条件に一致するプランはありません。' if plan_lists.empty? | ||
|
||
puts 'ご入力いただいた条件での見積もり結果は以下の通りです。' | ||
puts "契約アンペア:#{contract_amp}(A), 電力使用量:#{power_usage}(kwh)" | ||
puts '================================================' | ||
plan_lists.each do |plan| | ||
puts "プロバイダー名:#{plan[:provider_name]}" | ||
puts "プラン名:#{plan[:plan_name]}" | ||
puts "月額料金:#{plan[:price]}円" | ||
puts '================================================' | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
この行以下のメソッドですが、planを引数に持つメソッドとなっています。
この辺りから、より良いクラス設計が考えられるのではないかと感じました。もしお時間があれば考えてみてください・・!
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.
@yuyasat
コメントありがとうございます。
おっしゃるとおり、すべてにplanを引数に持つということはplanのインスタンスを作る設計とした方が良さそうだと気付きました。
PlansクラスとPlanクラスへ分離してみましたので再度ご確認いただけると幸いです。
Plansクラス
Planクラス