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

ChanrengeA/tokiwa_takumi #317

Open
wants to merge 6 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
1 change: 1 addition & 0 deletions challengeA/tokiwa_takumi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/spec/examples.txt
1 change: 1 addition & 0 deletions challengeA/tokiwa_takumi/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
8 changes: 8 additions & 0 deletions challengeA/tokiwa_takumi/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"

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

gem "rspec"

26 changes: 26 additions & 0 deletions challengeA/tokiwa_takumi/Gemfile.lock
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
ruby

DEPENDENCIES
rspec

BUNDLED WITH
2.1.2
5 changes: 5 additions & 0 deletions challengeA/tokiwa_takumi/csv/jxtg/basic_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ampere,basic_charge
30,858.00
40,1144.00
50,1430.00
60,1716.00
2 changes: 2 additions & 0 deletions challengeA/tokiwa_takumi/csv/jxtg/info.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
provider_name,plan_name
JXTGでんき,従量電灯Bたっぷりプラン
5 changes: 5 additions & 0 deletions challengeA/tokiwa_takumi/csv/jxtg/usage_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
greater_than,less_than,unit_charge
0,120,19.88
120,300,26.48
300,600,25.08
600,,26.15
7 changes: 7 additions & 0 deletions challengeA/tokiwa_takumi/csv/looop/basic_charge.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions challengeA/tokiwa_takumi/csv/looop/info.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
provider_name,plan_name
Looopでんき,おうちプラン
2 changes: 2 additions & 0 deletions challengeA/tokiwa_takumi/csv/looop/usage_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
greater_than,less_than,unit_charge
0,,26.40
8 changes: 8 additions & 0 deletions challengeA/tokiwa_takumi/csv/tepco/basic_charge.csv
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions challengeA/tokiwa_takumi/csv/tepco/info.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
provider_name,plan_name
東京電力エナジーパートナー,従量電灯B
4 changes: 4 additions & 0 deletions challengeA/tokiwa_takumi/csv/tepco/usage_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
greater_than,less_than,unit_charge
0,120,19.88
120,300,26.48
300,,30.57
5 changes: 5 additions & 0 deletions challengeA/tokiwa_takumi/csv/tokyo_gas/basic_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ampere,basic_charge
30,858.00
40,1144.00
50,1430.00
60,1716.00
2 changes: 2 additions & 0 deletions challengeA/tokiwa_takumi/csv/tokyo_gas/info.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
provider_name,plan_name
東京ガス,ずっともでんき1
4 changes: 4 additions & 0 deletions challengeA/tokiwa_takumi/csv/tokyo_gas/usage_charge.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
greater_than,less_than,unit_charge
0,140,23.67
140,350,23.88
350,,26.41
38 changes: 38 additions & 0 deletions challengeA/tokiwa_takumi/lib/csv_plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'csv'
require_relative 'plan'

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

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/*', base: __dir__)
plans_dir.each do |plan|
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", __dir__), headers: true).map(&:to_hash),
CSV.read(File.expand_path("./#{plan}/usage_charge.csv", __dir__), headers: true).map(&:to_hash)
)
end
plans
end
end
end
32 changes: 32 additions & 0 deletions challengeA/tokiwa_takumi/lib/plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 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
34 changes: 34 additions & 0 deletions challengeA/tokiwa_takumi/lib/simulator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'csv_plan'

class Simulator

def initialize(ampere, monthly_amount_kwh)
@ampere = ampere
@monthly_amount_kwh = monthly_amount_kwh
suggest_plan
end

def simulate
@suggest_plans.flatten
end

private

def suggest_plan
@suggest_plans = []
plan_list = CSVPlan.create_list_from_csv.map(&:convert_to_plan)
@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
119 changes: 119 additions & 0 deletions challengeA/tokiwa_takumi/spec/plan_spec.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions challengeA/tokiwa_takumi/spec/simulator_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading