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

Feature/challenge a shogo_takasaki #316

Open
wants to merge 23 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
69 changes: 69 additions & 0 deletions challengeA/shogo_takasaki/data/plans.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"suppliers": [
{
"provider_name": "東京電力エナジーパートナー",
"plan_name": "従量電灯B",
"basic_price": {
"10": 286.00,
"15": 429.00,
"20": 572.00,
"30": 858.00,
"40": 1144.00,
"50": 1430.00,
"60": 1716.00
},
"usage_price": {
"0": 19.88,
"301": 30.57,
"121": 26.48
}
},
{
"provider_name": "Looopでんき",
"plan_name": "おうちプラン",
"basic_price": {
"10": 0.00,
"15": 0.00,
"20": 0.00,
"30": 0.00,
"40": 0.00,
"50": 0.00,
"60": 0.00
},
"usage_price": {
"0": 26.40
}
},
{
"provider_name": "東京ガス",
"plan_name": "ずっとも電気1",
"basic_price": {
"30": 858.00,
"40": 1144.00,
"50": 1430.00,
"60": 1716.00
},
"usage_price": {
"0": 23.67,
"141": 23.88,
"351": 26.41
}
},
{
"provider_name": "JXTGでんき",
"plan_name": "従量電灯B たっぷりプラン",
"basic_price": {
"30": 858.00,
"40": 1144.00,
"50": 1430.00,
"60": 1716.80
},
"usage_price": {
"0": 19.88,
"121": 26.48,
"301": 25.08,
"601": 26.15
}
}
]
}
52 changes: 52 additions & 0 deletions challengeA/shogo_takasaki/src/plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# display plan info
class Plan
attr_reader :provider_name, :plan_name, :basic_price_list, :usage_price_list

module Message
NO_BASIC_PRICE = '基本料金設定無し'
NO_UNIT_PRICE = '従量料金設定無し'
end

def initialize(plan)
@provider_name = plan['provider_name']
@plan_name = plan['plan_name']
@basic_price_list = plan['basic_price']
@usage_price_list = plan['usage_price']
end

def basic_price(ampere, usage)
raise Message::NO_BASIC_PRICE if @basic_price_list[ampere.to_s].nil?
return @basic_price_list[ampere.to_s] / 2 if usage.zero?
cassy1003 marked this conversation as resolved.
Show resolved Hide resolved

@basic_price_list[ampere.to_s]
end

def unit_price(usage)
result = @usage_price_list.select { |k, v| k.to_i <= usage }.max.last
return result unless result.nil?

raise Message::NO_UNIT_PRICE
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hashからpriceを引き出すところはスッキリして良くなったと思います。
ただそもそも従量料金の計算の仕方が誤っています。どう間違っているかまではお伝えしないので、もう一度お調べできますか?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません、今日面談だったことを気にせず申し上げてしまいました。ご無理のない範囲でお時間があれば見てもらえるとありがたいです!


def usage_price(usage)
unit_price(usage) * usage
end

def price(ampere, usage)
basic_price(ampere, usage) + usage_price(usage)
end

def price_with_tax(ampere, usage)
(price(ampere, usage) + (price(ampere, usage) * Simulator::TAX_RATE)).to_i
end

def display(ampere, usage)
{
provider_name: provider_name,
plan_name: plan_name,
price: price_with_tax(ampere, usage)
}
end
end
24 changes: 24 additions & 0 deletions challengeA/shogo_takasaki/src/simulator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'json'

# Simulator for electric usage
class Simulator
attr_reader :ampere, :usage, :plans

TAX_RATE = 0.1

def initialize(ampere, usage)
@ampere = ampere
@usage = usage
File.open('../data/plans.json') do |f|
@plans = JSON.load(f)
end
end

def simulate
@plans['suppliers'].map do |plan|
Plan.new(plan).display(@ampere, @usage)
end
end
end
176 changes: 176 additions & 0 deletions challengeA/shogo_takasaki/test/plan_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# frozen_string_literal: true

require 'minitest/autorun'
require '../src/plan'
require '../src/simulator'
require 'json'

# display plan info test
class PlanTest < Minitest::Test
def setup
File.open("../data/plans.json") do |f|
@plans = JSON.load(f)
end
@tokyo_energy = Plan.new(@plans['suppliers'][0])
@looop = Plan.new(@plans['suppliers'][1])
@tokyo_gas = Plan.new(@plans['suppliers'][2])
@jxtg = Plan.new(@plans['suppliers'][3])
end

def test_tokyo_energy_basic_price
assert_equal 143, @tokyo_energy.basic_price(10, 0)
assert_equal 286, @tokyo_energy.basic_price(10, 100)
assert_equal 429, @tokyo_energy.basic_price(15, 100)
assert_equal 572, @tokyo_energy.basic_price(20, 100)
assert_equal 858, @tokyo_energy.basic_price(30, 100)
assert_equal 1144, @tokyo_energy.basic_price(40, 100)
assert_equal 1430, @tokyo_energy.basic_price(50, 100)
assert_equal 1716, @tokyo_energy.basic_price(60, 100)
e = assert_raises RuntimeError do @tokyo_energy.basic_price(61, 100) end
assert_equal Plan::Message::NO_BASIC_PRICE, e.message
e = assert_raises RuntimeError do @tokyo_energy.basic_price(61, 0) end
assert_equal Plan::Message::NO_BASIC_PRICE, e.message
end

def test_loops_basic_price
assert_equal 0, @looop.basic_price(10, 100)
assert_equal 0, @looop.basic_price(20, 100)
assert_equal 0, @looop.basic_price(30, 100)
assert_equal 0, @looop.basic_price(40, 100)
assert_equal 0, @looop.basic_price(50, 100)
assert_equal 0, @looop.basic_price(60, 100)
e = assert_raises RuntimeError do @looop.basic_price(61, 100) end
assert_equal Plan::Message::NO_BASIC_PRICE, e.message
end

def test_tokyo_gas_basic_price
assert_equal 429, @tokyo_gas.basic_price(30, 0)
assert_equal 858, @tokyo_gas.basic_price(30, 100)
assert_equal 1144, @tokyo_gas.basic_price(40, 100)
assert_equal 1430, @tokyo_gas.basic_price(50, 100)
assert_equal 1716, @tokyo_gas.basic_price(60, 100)
e = assert_raises RuntimeError do @tokyo_gas.basic_price(20, 100) end
assert_equal Plan::Message::NO_BASIC_PRICE, e.message
end

def test_jxtg_basic_price
assert_equal 429, @jxtg.basic_price(30, 0)
assert_equal 858, @jxtg.basic_price(30, 100)
assert_equal 1144, @jxtg.basic_price(40, 100)
assert_equal 1430, @jxtg.basic_price(50, 100)
assert_equal 1716.80, @jxtg.basic_price(60, 100)
e = assert_raises RuntimeError do @jxtg.basic_price(20, 100) end
assert_equal Plan::Message::NO_BASIC_PRICE, e.message
end

def test_tokyo_energy_unit_price
assert_equal 19.88, @tokyo_energy.unit_price(0)
assert_equal 19.88, @tokyo_energy.unit_price(119)
assert_equal 19.88, @tokyo_energy.unit_price(120)
assert_equal 26.48, @tokyo_energy.unit_price(121)
assert_equal 26.48, @tokyo_energy.unit_price(300)
assert_equal 30.57, @tokyo_energy.unit_price(301)
assert_equal 30.57, @tokyo_energy.unit_price(400)
end

def test_loops_unit_price
assert_equal 26.40, @looop.unit_price(0)
assert_equal 26.40, @looop.unit_price(119)
assert_equal 26.40, @looop.unit_price(120)
assert_equal 26.40, @looop.unit_price(121)
assert_equal 26.40, @looop.unit_price(300)
assert_equal 26.40, @looop.unit_price(301)
assert_equal 26.40, @looop.unit_price(400)
end

def test_tokyo_gas_unit_price
assert_equal 23.67, @tokyo_gas.unit_price(0)
assert_equal 23.67, @tokyo_gas.unit_price(139)
assert_equal 23.67, @tokyo_gas.unit_price(140)
assert_equal 23.88, @tokyo_gas.unit_price(141)
assert_equal 23.88, @tokyo_gas.unit_price(349)
assert_equal 23.88, @tokyo_gas.unit_price(350)
assert_equal 26.41, @tokyo_gas.unit_price(351)
end

def test_jxtg_unit_price
assert_equal 19.88, @jxtg.unit_price(0)
assert_equal 19.88, @jxtg.unit_price(120)
assert_equal 26.48, @jxtg.unit_price(121)
assert_equal 26.48, @jxtg.unit_price(300)
assert_equal 25.08, @jxtg.unit_price(301)
assert_equal 25.08, @jxtg.unit_price(600)
assert_equal 26.15, @jxtg.unit_price(601)
end

def test_additional_price
assert_equal @tokyo_energy.unit_price(0) * 0, @tokyo_energy.usage_price(0)
assert_equal @tokyo_energy.unit_price(119) * 119, @tokyo_energy.usage_price(119)
assert_equal @tokyo_energy.unit_price(120) * 120, @tokyo_energy.usage_price(120)
assert_equal @tokyo_energy.unit_price(121) * 121, @tokyo_energy.usage_price(121)
assert_equal @tokyo_energy.unit_price(300) * 300, @tokyo_energy.usage_price(300)
assert_equal @tokyo_energy.unit_price(301) * 301, @tokyo_energy.usage_price(301)
assert_equal @tokyo_energy.unit_price(400) * 400, @tokyo_energy.usage_price(400)

assert_equal @jxtg.unit_price(0) * 0, @jxtg.usage_price(0)
assert_equal @jxtg.unit_price(120) * 120, @jxtg.usage_price(120)
assert_equal @jxtg.unit_price(121) * 121, @jxtg.usage_price(121)
assert_equal @jxtg.unit_price(300) * 300, @jxtg.usage_price(300)
assert_equal @jxtg.unit_price(301) * 301, @jxtg.usage_price(301)
assert_equal @jxtg.unit_price(600) * 600, @jxtg.usage_price(600)
assert_equal @jxtg.unit_price(601) * 601, @jxtg.usage_price(601)
end

def test_price
assert_equal @tokyo_energy.basic_price(10, 0) + @tokyo_energy.usage_price(0), @tokyo_energy.price(10, 0)
assert_equal @tokyo_energy.basic_price(15, 119) + @tokyo_energy.usage_price(119), @tokyo_energy.price(15, 119)
assert_equal @tokyo_energy.basic_price(20, 120) + @tokyo_energy.usage_price(120), @tokyo_energy.price(20, 120)
assert_equal @tokyo_energy.basic_price(30, 121) + @tokyo_energy.usage_price(121), @tokyo_energy.price(30, 121)
assert_equal @tokyo_energy.basic_price(40, 300) + @tokyo_energy.usage_price(300), @tokyo_energy.price(40, 300)
assert_equal @tokyo_energy.basic_price(50, 301) + @tokyo_energy.usage_price(301), @tokyo_energy.price(50, 301)
assert_equal @tokyo_energy.basic_price(60, 400) + @tokyo_energy.usage_price(400), @tokyo_energy.price(60, 400)

assert_equal @jxtg.basic_price(30, 0) + @jxtg.usage_price(0), @jxtg.price(30, 0)
assert_equal @jxtg.basic_price(30, 120) + @jxtg.usage_price(120), @jxtg.price(30, 120)
assert_equal @jxtg.basic_price(40, 121) + @jxtg.usage_price(121), @jxtg.price(40, 121)
assert_equal @jxtg.basic_price(40, 300) + @jxtg.usage_price(300), @jxtg.price(40, 300)
assert_equal @jxtg.basic_price(50, 301) + @jxtg.usage_price(301), @jxtg.price(50, 301)
assert_equal @jxtg.basic_price(60, 600) + @jxtg.usage_price(600), @jxtg.price(60, 600)
assert_equal @jxtg.basic_price(60, 601) + @jxtg.usage_price(601), @jxtg.price(60, 601)
end

def test_price_with_tax
assert_equal (@tokyo_energy.price(60, 400) + (@tokyo_energy.price(60, 400) * Simulator::TAX_RATE)).to_i, @tokyo_energy.price_with_tax(60, 400)
assert_equal (@jxtg.price(30, 0) + (@jxtg.price(30, 0) * Simulator::TAX_RATE)).to_i, @jxtg.price_with_tax(30, 0)
end

def test_display
result_tokyo_energy = {
provider_name: '東京電力エナジーパートナー',
plan_name: '従量電灯B',
price: @tokyo_energy.price_with_tax(60, 400)
}
assert_equal result_tokyo_energy, @tokyo_energy.display(60, 400)

result_looops = {
provider_name: 'Looopでんき',
plan_name: 'おうちプラン',
price: @looop.price_with_tax(60, 400)
}
assert_equal result_looops, @looop.display(60, 400)

result_tokyo_gas = {
provider_name: '東京ガス',
plan_name: 'ずっとも電気1',
price: @tokyo_gas.price_with_tax(60, 400)
}
assert_equal result_tokyo_gas, @tokyo_gas.display(60, 400)

result_jxtg = {
provider_name: 'JXTGでんき',
plan_name: '従量電灯B たっぷりプラン',
price: @jxtg.price_with_tax(60, 400)
}
assert_equal result_jxtg, @jxtg.display(60, 400)
end
end
40 changes: 40 additions & 0 deletions challengeA/shogo_takasaki/test/simulator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'minitest/autorun'
require '../src/plan'
require '../src/simulator'
require 'json'

# Simulator for electric usage test
class SimulatorTest < Minitest::Test

def setup
@simulator = Simulator.new(60, 400)
end

def test_simulate
result = [
{
provider_name: "東京電力エナジーパートナー",
plan_name: "従量電灯B",
price: 15338
},
{
provider_name: "Looopでんき",
plan_name: "おうちプラン", price: 11616
},
{
provider_name: "東京ガス",
plan_name: "ずっとも電気1",
price: 13508
},
{
provider_name: "JXTGでんき",
plan_name: "従量電灯B たっぷりプラン",
price: 12923
}
]
assert_equal result, @simulator.simulate
end

end