From 31aa714cf818552cb08e95c0e32512acb184bec6 Mon Sep 17 00:00:00 2001 From: takutoki <34600200+takutoki@users.noreply.github.com> Date: Thu, 7 Oct 2021 16:25:13 +0900 Subject: [PATCH 1/6] =?UTF-8?q?csv=E3=81=8B=E3=82=89=E3=83=9E=E3=82=B9?= =?UTF-8?q?=E3=82=BF=E3=83=87=E3=83=BC=E3=82=BF=E3=82=92=E5=8F=96=E5=BE=97?= =?UTF-8?q?=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tokiwa_takumi/csv/jxtg/basic_charge.csv | 5 ++++ challengeA/tokiwa_takumi/csv/jxtg/info.csv | 2 ++ .../tokiwa_takumi/csv/jxtg/usage_charge.csv | 5 ++++ .../tokiwa_takumi/csv/looop/basic_charge.csv | 7 +++++ challengeA/tokiwa_takumi/csv/looop/info.csv | 2 ++ .../tokiwa_takumi/csv/looop/usage_charge.csv | 2 ++ .../tokiwa_takumi/csv/tepco/basic_charge.csv | 8 +++++ challengeA/tokiwa_takumi/csv/tepco/info.csv | 2 ++ .../tokiwa_takumi/csv/tepco/usage_charge.csv | 4 +++ .../csv/tokyo_gas/basic_charge.csv | 5 ++++ .../tokiwa_takumi/csv/tokyo_gas/info.csv | 2 ++ .../csv/tokyo_gas/usage_charge.csv | 4 +++ challengeA/tokiwa_takumi/lib/csv_plan.rb | 30 +++++++++++++++++++ challengeA/tokiwa_takumi/lib/plan.rb | 0 challengeA/tokiwa_takumi/lib/simulator.rb | 25 ++++++++++++++++ 15 files changed, 103 insertions(+) create mode 100644 challengeA/tokiwa_takumi/csv/jxtg/basic_charge.csv create mode 100644 challengeA/tokiwa_takumi/csv/jxtg/info.csv create mode 100644 challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv create mode 100644 challengeA/tokiwa_takumi/csv/looop/basic_charge.csv create mode 100644 challengeA/tokiwa_takumi/csv/looop/info.csv create mode 100644 challengeA/tokiwa_takumi/csv/looop/usage_charge.csv create mode 100644 challengeA/tokiwa_takumi/csv/tepco/basic_charge.csv create mode 100644 challengeA/tokiwa_takumi/csv/tepco/info.csv create mode 100644 challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv create mode 100644 challengeA/tokiwa_takumi/csv/tokyo_gas/basic_charge.csv create mode 100644 challengeA/tokiwa_takumi/csv/tokyo_gas/info.csv create mode 100644 challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv create mode 100644 challengeA/tokiwa_takumi/lib/csv_plan.rb create mode 100644 challengeA/tokiwa_takumi/lib/plan.rb create mode 100644 challengeA/tokiwa_takumi/lib/simulator.rb diff --git a/challengeA/tokiwa_takumi/csv/jxtg/basic_charge.csv b/challengeA/tokiwa_takumi/csv/jxtg/basic_charge.csv new file mode 100644 index 000000000..17285d199 --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/jxtg/basic_charge.csv @@ -0,0 +1,5 @@ +ampere,basic_charge +30,858.00 +40,1144.00 +50,1430.00 +60,1716.00 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/jxtg/info.csv b/challengeA/tokiwa_takumi/csv/jxtg/info.csv new file mode 100644 index 000000000..14f58d9ae --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/jxtg/info.csv @@ -0,0 +1,2 @@ +provider_name,plan_name +JXTGでんき,従量電灯Bたっぷりプラン \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv b/challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv new file mode 100644 index 000000000..247f0142d --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv @@ -0,0 +1,5 @@ +greater_than,less_than,unit_charge +0,120,19.88 +121,300,26.48 +301,600,25.08 +601,,26.15 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/looop/basic_charge.csv b/challengeA/tokiwa_takumi/csv/looop/basic_charge.csv new file mode 100644 index 000000000..5a58ea5a9 --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/looop/basic_charge.csv @@ -0,0 +1,7 @@ +ampere,basic_charge +10,0.00 +20,0.00 +30,0.00 +40,0.00 +50,0.00 +60,0.00 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/looop/info.csv b/challengeA/tokiwa_takumi/csv/looop/info.csv new file mode 100644 index 000000000..e5c2b1a54 --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/looop/info.csv @@ -0,0 +1,2 @@ +provider_name,plan_name +Looopでんき,おうちプラン \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/looop/usage_charge.csv b/challengeA/tokiwa_takumi/csv/looop/usage_charge.csv new file mode 100644 index 000000000..77eb6f09e --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/looop/usage_charge.csv @@ -0,0 +1,2 @@ +greater_than,less_than,unit_charge +0,,26.40 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/tepco/basic_charge.csv b/challengeA/tokiwa_takumi/csv/tepco/basic_charge.csv new file mode 100644 index 000000000..93eb363fa --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/tepco/basic_charge.csv @@ -0,0 +1,8 @@ +ampere,basic_charge +10,286.00 +15,419.00 +20,572.00 +30,858.00 +40,1144.00 +50,1430.00 +60,1716.00 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/tepco/info.csv b/challengeA/tokiwa_takumi/csv/tepco/info.csv new file mode 100644 index 000000000..dc1a3549f --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/tepco/info.csv @@ -0,0 +1,2 @@ +provider_name,plan_name +東京電力エナジーパートナー,従量電灯B \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv b/challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv new file mode 100644 index 000000000..a67f8a197 --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv @@ -0,0 +1,4 @@ +greater_than,less_than,unit_charge +0,120,19.88 +121,300,26.48 +301,,30.57 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/tokyo_gas/basic_charge.csv b/challengeA/tokiwa_takumi/csv/tokyo_gas/basic_charge.csv new file mode 100644 index 000000000..17285d199 --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/tokyo_gas/basic_charge.csv @@ -0,0 +1,5 @@ +ampere,basic_charge +30,858.00 +40,1144.00 +50,1430.00 +60,1716.00 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/tokyo_gas/info.csv b/challengeA/tokiwa_takumi/csv/tokyo_gas/info.csv new file mode 100644 index 000000000..9457d9b77 --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/tokyo_gas/info.csv @@ -0,0 +1,2 @@ +provider_name,plan_name +東京ガス,ずっともでんき1 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv b/challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv new file mode 100644 index 000000000..acbc51572 --- /dev/null +++ b/challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv @@ -0,0 +1,4 @@ +greater_than,less_than,unit_charge +0,140,23.67 +141,350,23.88 +351,,26.41 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/lib/csv_plan.rb b/challengeA/tokiwa_takumi/lib/csv_plan.rb new file mode 100644 index 000000000..e15682519 --- /dev/null +++ b/challengeA/tokiwa_takumi/lib/csv_plan.rb @@ -0,0 +1,30 @@ +require 'csv' + +class CSVPlan + attr_reader :provider_name, :plan_name, :basic_charge_list, :usage_charge_list + + def initialize(provider_name, plan_name, basic_charge_list, usage_charge_list) + @provider_name = provider_name + @plan_name = plan_name + @basic_charge_list = basic_charge_list + @usage_charge_list = usage_charge_list + end + + class << self + def create_list_from_csv + plans = [] + + plans_dir = Dir.glob('../csv/*') + + plans_dir.each do |plan| + info = CSV.read(File.expand_path("./#{plan}/info.csv"), headers: true) + plans << CSVPlan.new(info[:provider_name][0], + info[:plan_name][0], + CSV.read(File.expand_path("./#{plan}/basic_charge.csv"), headers: true).map(&:to_hash), + CSV.read(File.expand_path("./#{plan}/usage_charge.csv"), headers: true).map(&:to_hash) + ) + end + plans + end + end +end diff --git a/challengeA/tokiwa_takumi/lib/plan.rb b/challengeA/tokiwa_takumi/lib/plan.rb new file mode 100644 index 000000000..e69de29bb diff --git a/challengeA/tokiwa_takumi/lib/simulator.rb b/challengeA/tokiwa_takumi/lib/simulator.rb new file mode 100644 index 000000000..eae08c3ac --- /dev/null +++ b/challengeA/tokiwa_takumi/lib/simulator.rb @@ -0,0 +1,25 @@ +## simulator = Simulator.new(40, 280) +## simulator.simulate +#=> [{ provider_name: ‘Looopでんき’, plan_name: ‘おうちプラン’, price: ‘1234’ }, …] + +# require 'json' + +class Simulator + + def initialize(ampere, monthly_amount_kwh) + @ampere = ampere + @monthly_amount_kwh = monthly_amount_kwh + suggest_plan + end + + def simulate + ## @plansをそのまま出力 + end + + private + + def suggest_plan + @plans = [] + + end +end \ No newline at end of file From dd1e4519cb174289f0abc18b2f86d349e680335e Mon Sep 17 00:00:00 2001 From: takutoki <34600200+takutoki@users.noreply.github.com> Date: Fri, 8 Oct 2021 02:17:50 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=E9=9B=BB=E5=8A=9B=E6=96=99=E9=87=91?= =?UTF-8?q?=E3=81=AE=E3=82=B7=E3=83=9F=E3=83=A5=E3=83=AC=E3=83=BC=E3=83=88?= =?UTF-8?q?=E8=A8=88=E7=AE=97=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challengeA/tokiwa_takumi/lib/csv_plan.rb | 14 +++++-- challengeA/tokiwa_takumi/lib/plan.rb | 46 +++++++++++++++++++++++ challengeA/tokiwa_takumi/lib/simulator.rb | 18 ++++----- 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/challengeA/tokiwa_takumi/lib/csv_plan.rb b/challengeA/tokiwa_takumi/lib/csv_plan.rb index e15682519..d76eee1d1 100644 --- a/challengeA/tokiwa_takumi/lib/csv_plan.rb +++ b/challengeA/tokiwa_takumi/lib/csv_plan.rb @@ -1,4 +1,5 @@ require 'csv' +require_relative 'plan' class CSVPlan attr_reader :provider_name, :plan_name, :basic_charge_list, :usage_charge_list @@ -10,14 +11,21 @@ def initialize(provider_name, plan_name, basic_charge_list, usage_charge_list) @usage_charge_list = usage_charge_list end + def convert_to_plan + Plan.new( + provider_name, + plan_name, + basic_charge_list, + usage_charge_list + ) + end + class << self def create_list_from_csv plans = [] - plans_dir = Dir.glob('../csv/*') - plans_dir.each do |plan| - info = CSV.read(File.expand_path("./#{plan}/info.csv"), headers: true) + info = CSV.table(File.expand_path("./#{plan}/info.csv")) plans << CSVPlan.new(info[:provider_name][0], info[:plan_name][0], CSV.read(File.expand_path("./#{plan}/basic_charge.csv"), headers: true).map(&:to_hash), diff --git a/challengeA/tokiwa_takumi/lib/plan.rb b/challengeA/tokiwa_takumi/lib/plan.rb index e69de29bb..aaad3fdbd 100644 --- a/challengeA/tokiwa_takumi/lib/plan.rb +++ b/challengeA/tokiwa_takumi/lib/plan.rb @@ -0,0 +1,46 @@ +class Plan + attr_reader :provider_name, :plan_name, :basic_charge_list, :usage_charge_list + + def initialize(provider_name, plan_name, basic_charge_list, usage_charge_list) + @provider_name = provider_name + @plan_name = plan_name + @basic_charge_list = basic_charge_list + @usage_charge_list = usage_charge_list + end + + def simulate_charge(ampere, monthly_amount_kwh) + basic_charge = basic_charge(ampere) + return if basic_charge.nil? ## 対応するアンペアなし + + price = basic_charge + usage_charge(monthly_amount_kwh) + { + "provider_name": @provider_name, + "plan_name": @plan_name, + "price": price + } + end + + private + + def basic_charge(ampere) + basic_charge_plan = basic_charge_list.find{ |c| c["ampere"].to_i == ampere } + basic_charge_plan["basic_charge"].to_i unless basic_charge_plan.nil? + end + + def usage_charge(monthly_amount_kwh) + charge = 0 + usage_charge_list.each do |usage_charge| + gt = usage_charge["greater_than"].to_i + lt = usage_charge["less_than"].to_i + unit_charge = usage_charge["unit_charge"].to_f + if lt.zero? && gt < monthly_amount_kwh # 最大区分 + charge += (monthly_amount_kwh - gt) * unit_charge + elsif lt <= monthly_amount_kwh && !lt.zero? # 超過する区分 + charge += (lt - gt) * unit_charge + elsif gt < monthly_amount_kwh && monthly_amount_kwh < lt # 超過しない区分 + charge += (monthly_amount_kwh - gt) * unit_charge + end + end + charge + end +end diff --git a/challengeA/tokiwa_takumi/lib/simulator.rb b/challengeA/tokiwa_takumi/lib/simulator.rb index eae08c3ac..6fd77f299 100644 --- a/challengeA/tokiwa_takumi/lib/simulator.rb +++ b/challengeA/tokiwa_takumi/lib/simulator.rb @@ -1,8 +1,4 @@ -## simulator = Simulator.new(40, 280) -## simulator.simulate -#=> [{ provider_name: ‘Looopでんき’, plan_name: ‘おうちプラン’, price: ‘1234’ }, …] - -# require 'json' +require_relative 'csv_plan' class Simulator @@ -13,13 +9,17 @@ def initialize(ampere, monthly_amount_kwh) end def simulate - ## @plansをそのまま出力 + @suggest_plans.flatten end private def suggest_plan - @plans = [] - + @suggest_plans = [] + plan_list = CSVPlan.create_list_from_csv.map(&:convert_to_plan) + @suggest_plans = plan_list.map{ |plan| plan.simulate_charge(@ampere, @monthly_amount_kwh) } end -end \ No newline at end of file +end + +a = Simulator.new(40, 300) +p a.simulate \ No newline at end of file From 83ec4670b7ae39896ac0cc361d89de8f62144f25 Mon Sep 17 00:00:00 2001 From: takutoki <34600200+takutoki@users.noreply.github.com> Date: Fri, 8 Oct 2021 02:18:45 +0900 Subject: [PATCH 3/6] =?UTF-8?q?csv=E3=83=9E=E3=82=B9=E3=82=BF=E3=83=87?= =?UTF-8?q?=E3=83=BC=E3=82=BF=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv | 6 +++--- challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv | 4 ++-- challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv b/challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv index 247f0142d..5a87cb863 100644 --- a/challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv +++ b/challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv @@ -1,5 +1,5 @@ greater_than,less_than,unit_charge 0,120,19.88 -121,300,26.48 -301,600,25.08 -601,,26.15 \ No newline at end of file +120,300,26.48 +300,600,25.08 +600,,26.15 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv b/challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv index a67f8a197..40f568915 100644 --- a/challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv +++ b/challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv @@ -1,4 +1,4 @@ greater_than,less_than,unit_charge 0,120,19.88 -121,300,26.48 -301,,30.57 \ No newline at end of file +120,300,26.48 +300,,30.57 \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv b/challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv index acbc51572..98c5cd088 100644 --- a/challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv +++ b/challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv @@ -1,4 +1,4 @@ greater_than,less_than,unit_charge 0,140,23.67 -141,350,23.88 -351,,26.41 \ No newline at end of file +140,350,23.88 +350,,26.41 \ No newline at end of file From 9072d7d2ea4f4a01cf8b39a3c19d6f165dd5957b Mon Sep 17 00:00:00 2001 From: takutoki <34600200+takutoki@users.noreply.github.com> Date: Fri, 8 Oct 2021 02:37:30 +0900 Subject: [PATCH 4/6] =?UTF-8?q?=E5=87=BA=E5=8A=9B=E7=94=A8=E3=81=AB?= =?UTF-8?q?=E6=95=B4=E5=BD=A2=E3=81=99=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=82=92=E3=83=A2=E3=83=87=E3=83=AB=E3=81=8B=E3=82=89?= =?UTF-8?q?=E3=82=B5=E3=83=BC=E3=83=93=E3=82=B9=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=E3=81=AB=E7=A7=BB=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challengeA/tokiwa_takumi/lib/plan.rb | 14 -------------- challengeA/tokiwa_takumi/lib/simulator.rb | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/challengeA/tokiwa_takumi/lib/plan.rb b/challengeA/tokiwa_takumi/lib/plan.rb index aaad3fdbd..b79537e85 100644 --- a/challengeA/tokiwa_takumi/lib/plan.rb +++ b/challengeA/tokiwa_takumi/lib/plan.rb @@ -8,20 +8,6 @@ def initialize(provider_name, plan_name, basic_charge_list, usage_charge_list) @usage_charge_list = usage_charge_list end - def simulate_charge(ampere, monthly_amount_kwh) - basic_charge = basic_charge(ampere) - return if basic_charge.nil? ## 対応するアンペアなし - - price = basic_charge + usage_charge(monthly_amount_kwh) - { - "provider_name": @provider_name, - "plan_name": @plan_name, - "price": price - } - end - - private - def basic_charge(ampere) basic_charge_plan = basic_charge_list.find{ |c| c["ampere"].to_i == ampere } basic_charge_plan["basic_charge"].to_i unless basic_charge_plan.nil? diff --git a/challengeA/tokiwa_takumi/lib/simulator.rb b/challengeA/tokiwa_takumi/lib/simulator.rb index 6fd77f299..6e95e9c39 100644 --- a/challengeA/tokiwa_takumi/lib/simulator.rb +++ b/challengeA/tokiwa_takumi/lib/simulator.rb @@ -17,9 +17,22 @@ def simulate def suggest_plan @suggest_plans = [] plan_list = CSVPlan.create_list_from_csv.map(&:convert_to_plan) - @suggest_plans = plan_list.map{ |plan| plan.simulate_charge(@ampere, @monthly_amount_kwh) } + @suggest_plans = plan_list.map{ |plan| simulate_charge(plan) } + end + + def simulate_charge(plan) + basic_charge = plan.basic_charge(@ampere) + return if basic_charge.nil? ## 対応するアンペアなし + + price = basic_charge + plan.usage_charge(@monthly_amount_kwh) + { + "provider_name": plan.provider_name, + "plan_name": plan.plan_name, + "price": price + } end end -a = Simulator.new(40, 300) +## 動作確認用 +a = Simulator.new(40, 400) p a.simulate \ No newline at end of file From 8b6513d2506c89d72715d8df7448bfc8942c28ea Mon Sep 17 00:00:00 2001 From: takutoki <34600200+takutoki@users.noreply.github.com> Date: Fri, 8 Oct 2021 14:52:50 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=83=87?= =?UTF-8?q?=E3=83=BC=E3=82=BF=E3=81=AE=E3=83=87=E3=82=A3=E3=83=AC=E3=82=AF?= =?UTF-8?q?=E3=83=88=E3=83=AA=E6=8E=A2=E7=B4=A2=E3=82=92=E5=AE=9F=E8=A1=8C?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=8C=E3=81=82=E3=82=8B?= =?UTF-8?q?=E3=83=91=E3=82=B9=E3=81=8B=E3=82=89=E3=81=AE=E7=9B=B8=E5=AF=BE?= =?UTF-8?q?=E3=83=91=E3=82=B9=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challengeA/tokiwa_takumi/lib/csv_plan.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challengeA/tokiwa_takumi/lib/csv_plan.rb b/challengeA/tokiwa_takumi/lib/csv_plan.rb index d76eee1d1..a3a782847 100644 --- a/challengeA/tokiwa_takumi/lib/csv_plan.rb +++ b/challengeA/tokiwa_takumi/lib/csv_plan.rb @@ -23,13 +23,13 @@ def convert_to_plan class << self def create_list_from_csv plans = [] - plans_dir = Dir.glob('../csv/*') + plans_dir = Dir.glob('../csv/*', base: __dir__) plans_dir.each do |plan| - info = CSV.table(File.expand_path("./#{plan}/info.csv")) + info = CSV.table(File.expand_path("./#{plan}/info.csv", __dir__)) plans << CSVPlan.new(info[:provider_name][0], info[:plan_name][0], - CSV.read(File.expand_path("./#{plan}/basic_charge.csv"), headers: true).map(&:to_hash), - CSV.read(File.expand_path("./#{plan}/usage_charge.csv"), headers: true).map(&:to_hash) + CSV.read(File.expand_path("./#{plan}/basic_charge.csv", __dir__), headers: true).map(&:to_hash), + CSV.read(File.expand_path("./#{plan}/usage_charge.csv", __dir__), headers: true).map(&:to_hash) ) end plans From 90350540b8b18feabcc371baeecff98e62cebbc6 Mon Sep 17 00:00:00 2001 From: takutoki <34600200+takutoki@users.noreply.github.com> Date: Fri, 8 Oct 2021 15:38:07 +0900 Subject: [PATCH 6/6] =?UTF-8?q?rspec=E3=82=92=E7=94=A8=E3=81=84=E3=81=A6?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challengeA/tokiwa_takumi/.gitignore | 1 + challengeA/tokiwa_takumi/.rspec | 1 + challengeA/tokiwa_takumi/Gemfile | 8 ++ challengeA/tokiwa_takumi/Gemfile.lock | 26 ++++ challengeA/tokiwa_takumi/lib/simulator.rb | 4 - challengeA/tokiwa_takumi/spec/plan_spec.rb | 119 ++++++++++++++++++ .../tokiwa_takumi/spec/simulator_spec.rb | 33 +++++ challengeA/tokiwa_takumi/spec/spec_helper.rb | 100 +++++++++++++++ 8 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 challengeA/tokiwa_takumi/.gitignore create mode 100644 challengeA/tokiwa_takumi/.rspec create mode 100644 challengeA/tokiwa_takumi/Gemfile create mode 100644 challengeA/tokiwa_takumi/Gemfile.lock create mode 100644 challengeA/tokiwa_takumi/spec/plan_spec.rb create mode 100644 challengeA/tokiwa_takumi/spec/simulator_spec.rb create mode 100644 challengeA/tokiwa_takumi/spec/spec_helper.rb diff --git a/challengeA/tokiwa_takumi/.gitignore b/challengeA/tokiwa_takumi/.gitignore new file mode 100644 index 000000000..f05cf8c8d --- /dev/null +++ b/challengeA/tokiwa_takumi/.gitignore @@ -0,0 +1 @@ +/spec/examples.txt diff --git a/challengeA/tokiwa_takumi/.rspec b/challengeA/tokiwa_takumi/.rspec new file mode 100644 index 000000000..c99d2e739 --- /dev/null +++ b/challengeA/tokiwa_takumi/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/challengeA/tokiwa_takumi/Gemfile b/challengeA/tokiwa_takumi/Gemfile new file mode 100644 index 000000000..c8b0363b5 --- /dev/null +++ b/challengeA/tokiwa_takumi/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + + source "https://rubygems.org" + + git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } + + gem "rspec" + \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/Gemfile.lock b/challengeA/tokiwa_takumi/Gemfile.lock new file mode 100644 index 000000000..be655ec0c --- /dev/null +++ b/challengeA/tokiwa_takumi/Gemfile.lock @@ -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 + ruby + +DEPENDENCIES + rspec + +BUNDLED WITH + 2.1.2 diff --git a/challengeA/tokiwa_takumi/lib/simulator.rb b/challengeA/tokiwa_takumi/lib/simulator.rb index 6e95e9c39..dfe778406 100644 --- a/challengeA/tokiwa_takumi/lib/simulator.rb +++ b/challengeA/tokiwa_takumi/lib/simulator.rb @@ -32,7 +32,3 @@ def simulate_charge(plan) } end end - -## 動作確認用 -a = Simulator.new(40, 400) -p a.simulate \ No newline at end of file diff --git a/challengeA/tokiwa_takumi/spec/plan_spec.rb b/challengeA/tokiwa_takumi/spec/plan_spec.rb new file mode 100644 index 000000000..c106ce630 --- /dev/null +++ b/challengeA/tokiwa_takumi/spec/plan_spec.rb @@ -0,0 +1,119 @@ +require "spec_helper" + +RSpec.describe Plan do + let!(:plan) { CSVPlan.create_list_from_csv.map(&:convert_to_plan) } + + describe "東京ガスずっともでんき1" do + let!(:tokyo_gas) { plan.find{ |plan| plan.provider_name == "東京ガス" }} + + describe "基本料金の算出が正しいこと" do + it "対応アンペアが存在する" do + expect(tokyo_gas.basic_charge(30)).to eq 858 + end + + it "対応アンペアが存在しない" do + expect(tokyo_gas.basic_charge(100)).to eq nil + end + end + + describe "従量課金料金の算出が正しいこと" do + it "1つの区分で収まる使用量で算出する" do + expect(tokyo_gas.usage_charge(100)).to eq (23.67 * 100) + end + + it "2つ以上の区分にまたがる使用量で算出する" do + expect_value = (23.67 * 140) + (23.88 * 160) + expect(tokyo_gas.usage_charge(300)).to eq expect_value + end + + it "一番上の区分まで使用した算出をする" do + expect_value = (23.67 * 140) + (23.88 * 210) + (26.41 * 50) + expect(tokyo_gas.usage_charge(400)).to eq expect_value + end + end + end + + describe "東京エナジーパートナー従量電灯B" do + let!(:tepco) { plan.find{ |plan| plan.provider_name == "東京電力エナジーパートナー" }} + + describe "基本料金の算出が正しいこと" do + it "対応アンペアが存在する" do + expect(tepco.basic_charge(40)).to eq 1144 + end + + it "対応アンペアが存在しない" do + expect(tepco.basic_charge(100)).to eq nil + end + end + + describe "従量課金料金の算出が正しいこと" do + it "1つの区分で収まる使用量で算出する" do + expect(tepco.usage_charge(100)).to eq (19.88 * 100) + end + + it "2つ以上の区分にまたがる使用量で算出する" do + expect_value = (19.88 * 120) + (26.48 * 180) + expect(tepco.usage_charge(300)).to eq expect_value + end + + it "一番上の区分まで使用した算出をする" do + expect_value = (19.88 * 120) + (26.48 * 180) + (30.57 * 100) + expect(tepco.usage_charge(400)).to eq expect_value + end + end + end + + describe "Loop電気おうちプラン" do + let!(:loop) { plan.find{ |plan| plan.provider_name == "Looopでんき" }} + + describe "基本料金の算出が正しいこと" do + it "対応アンペアが存在する" do + expect(loop.basic_charge(40)).to eq 0 + end + + it "対応アンペアが存在しない" do + expect(loop.basic_charge(100)).to eq nil + end + end + + it "基本料金の算出が正しいこと" do + expect(loop.basic_charge(40)).to eq 0 + end + + describe "従量課金料金の算出が正しいこと(区分が1つのみ)" do + it "1つの区分だけで算出されること" do + expect(loop.usage_charge(500)).to eq (26.40 * 500) + end + end + end + + describe "JXTGでんき従量電灯Bたっぷりプラン" do + let!(:jxtg) { plan.find{ |plan| plan.provider_name == "JXTGでんき" }} + + describe "基本料金の算出が正しいこと" do + it "対応アンペアが存在する" do + expect(jxtg.basic_charge(50)).to eq 1430 + end + + it "対応アンペアが存在しない" do + expect(jxtg.basic_charge(100)).to eq nil + end + end + + describe "従量課金料金の算出が正しいこと" do + it "1つの区分で収まる使用量で算出する" do + expect(jxtg.usage_charge(100)).to eq (19.88 * 100) + end + + it "2つ以上の区分にまたがる使用量で算出する" do + expect_value = (19.88 * 120) + (26.48 * 180) + expect(jxtg.usage_charge(300)).to eq expect_value + end + + it "一番上の区分まで使用した算出をする" do + expect_value = (19.88 * 120) + (26.48 * 180) + (25.08 * 300) + (26.15 * 50) + expect(jxtg.usage_charge(650)).to eq expect_value + end + end + end +end diff --git a/challengeA/tokiwa_takumi/spec/simulator_spec.rb b/challengeA/tokiwa_takumi/spec/simulator_spec.rb new file mode 100644 index 000000000..33b9be3a6 --- /dev/null +++ b/challengeA/tokiwa_takumi/spec/simulator_spec.rb @@ -0,0 +1,33 @@ +require "spec_helper" + +RSpec.describe Simulator do + describe "simulatorの結果をが期待どおりであること" do + let!(:simulator) { Simulator.new(40, 300) } + + it "simulator" do + expect(simulator.simulate.count).to eq 4 + expect(simulator.simulate).to match_array [ + { + "provider_name": "東京ガス", + "plan_name": "ずっともでんき1", + "price": 1144 + (23.67 * 140) + (23.88 * 160) + }, + { + "provider_name": "JXTGでんき", + "plan_name": "従量電灯Bたっぷりプラン", + "price": 1144 + (19.88 * 120) + (26.48 * 180) + }, + { + "provider_name": "Looopでんき", + "plan_name": "おうちプラン", + "price": (26.40 * 300) + }, + { + "provider_name": "東京電力エナジーパートナー", + "plan_name": "従量電灯B", + "price": 1144 + (19.88 * 120) + (26.48 * 180) + } + ] + end + end +end diff --git a/challengeA/tokiwa_takumi/spec/spec_helper.rb b/challengeA/tokiwa_takumi/spec/spec_helper.rb new file mode 100644 index 000000000..d592cedc7 --- /dev/null +++ b/challengeA/tokiwa_takumi/spec/spec_helper.rb @@ -0,0 +1,100 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed + + Dir[File.join(File.dirname(__FILE__), '../lib/*.rb')].each { |f| require f } +end