Skip to content

Commit ac5e08d

Browse files
author
John Ollier
committed
Add catalogue and product classes. Refactor following code review.
1 parent ca34027 commit ac5e08d

12 files changed

+271
-70
lines changed

.DS_Store

-6 KB
Binary file not shown.

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
.rspec
3+
.DS_Store

Lib/basket.rb

+34-27
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
class Basket
2-
attr_accessor :basket
3-
def initialize
4-
@basket = { "FR1" => 0,
5-
"SR1" => 0,
6-
"CF1" => 0,
7-
}
8-
end
2+
attr_reader :purchases
93

10-
def show_basket
11-
return "#{@basket} costs: £#{basket_total}"
4+
def initialize(catalogue)
5+
# the class is called basket, lets think of a different descriptive name
6+
@purchases = Hash.new
7+
@catalogue = catalogue
8+
@catalogue.products_by_code.each_key { |p_code| @purchases[p_code] = 0 }
129
end
1310

14-
def basket_total
15-
((@basket["FR1"] * 311) + (@basket["SR1"] * 500) + (@basket["CF1"] * 1123)) / 100.00
11+
def show_basket
12+
# #{@basket} will call to_s method of Basket
1613
end
1714

18-
def add(x)
19-
@basket[x] = @basket[x] + 1
15+
def total
16+
running_total = 0
17+
@purchases.each do |code, quantity|
18+
running_total = running_total + quantity * @catalogue.products_by_code[code].price
19+
end
20+
return running_total
2021
end
2122

22-
def remove(x)
23-
@basket[x] = @basket[x] - 1 if @basket[x] > 0
23+
# give our parameter a descriptive name
24+
def add(product_code)
25+
# what if the user passes a non existant product code
26+
if @purchases.key?(product_code)
27+
@purchases[product_code] = @purchases[product_code] + 1
28+
else
29+
# TODO can do better than this
30+
print("Error: no product with code: #{product_code}")
31+
end
2432
end
2533

26-
def complete_purchase
27-
running_price = basket_total
28-
@basket = { "FR1" => 0,
29-
"SR1" => 0,
30-
"CF1" => 0,
31-
}
32-
return "That cost £#{running_price} and your basket is now empty"
33-
34+
def remove(product_code)
35+
if @purchases.key?(product_code)
36+
if purchases[product_code] > 0
37+
purchases[product_code] = purchases[product_code] - 1
38+
else
39+
print("Warning: no product with code #{product_code} in basket")
40+
end
41+
else
42+
# TODO can do better than this
43+
print("Error: no product with code: #{product_code}")
44+
end
3445
end
3546

3647
end
37-
38-
def start_checkout
39-
Basket.new
40-
end

Lib/catalogue.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'product'
2+
3+
class Catalogue
4+
5+
attr_reader :products_by_code
6+
7+
def initialize()
8+
fruit_tea = Product.new('FR1','Fruit Tea', 311)
9+
strawberries = Product.new('SR1','Strawberries', 500)
10+
coffee = Product.new('CF1','Coffee', 1123)
11+
@products_by_code = {'FR1' => fruit_tea, 'SR1' => strawberries, 'CF1' => coffee}
12+
end
13+
14+
end

Lib/item.rb

-13
This file was deleted.

Lib/main.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
puts "Welcome to the GDS supermarket"
2+
3+
finish = false
4+
5+
while !finish
6+
puts "Please enter command"
7+
command = gets
8+
if command == 'checkout':
9+
finish = true
10+
end
11+
12+
end

Lib/product.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Product
2+
3+
attr_reader :code, :name, :price
4+
5+
def initialize(code, name, price)
6+
@code = code
7+
@name = name
8+
@price = price
9+
end
10+
11+
def to_s
12+
"Product: code: #{code}, name: #{name}, price: #{price}"
13+
end
14+
15+
end

Spec/basket_spec.rb

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'basket'
2+
require 'catalogue'
3+
4+
describe Basket do
5+
catalogue = Catalogue.new
6+
describe '#initialize' do
7+
it "is empty initially" do
8+
basket = Basket.new(catalogue)
9+
# when testing methods test the data not a formatted message containing the data
10+
expect(basket.purchases['FR1']).to eq(0)
11+
expect(basket.purchases['SR1']).to eq(0)
12+
expect(basket.purchases['CF1']).to eq(0)
13+
end
14+
end
15+
describe '#add' do
16+
it "can add a valid item" do
17+
basket = Basket.new(catalogue)
18+
basket.add("FR1")
19+
expect(basket.purchases['FR1']).to eq(1)
20+
expect(basket.purchases['SR1']).to eq(0)
21+
expect(basket.purchases['CF1']).to eq(0)
22+
end
23+
it "will not add an invalid item" do
24+
basket = Basket.new(catalogue)
25+
basket.add("ZZZ")
26+
expect(basket.purchases['FR1']).to eq(0)
27+
expect(basket.purchases['SR1']).to eq(0)
28+
expect(basket.purchases['CF1']).to eq(0)
29+
end
30+
end
31+
describe '#remove' do
32+
it "can remove a valid item if present" do
33+
basket = Basket.new(catalogue)
34+
basket.add("FR1")
35+
basket.add("FR1")
36+
basket.remove("FR1")
37+
expect(basket.purchases['FR1']).to eq(1)
38+
expect(basket.purchases['SR1']).to eq(0)
39+
expect(basket.purchases['CF1']).to eq(0)
40+
end
41+
it "will not remove an invalid item" do
42+
basket = Basket.new(catalogue)
43+
basket.remove("ZZZ")
44+
expect(basket.purchases['FR1']).to eq(0)
45+
expect(basket.purchases['SR1']).to eq(0)
46+
expect(basket.purchases['CF1']).to eq(0)
47+
end
48+
end
49+
describe '#total' do
50+
it "will be zero initially" do
51+
basket = Basket.new(catalogue)
52+
expect(basket.total).to eq(0)
53+
end
54+
it "will calculate the running total" do
55+
basket = Basket.new(catalogue)
56+
basket.add("FR1")
57+
basket.add("FR1")
58+
basket.add("CF1")
59+
expect(basket.total).to eq(1745)
60+
end
61+
end
62+
end
63+

Spec/catalogue_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'catalogue'
2+
3+
describe Catalogue do
4+
describe '#initialize' do
5+
it "has all products" do
6+
catalogue = Catalogue.new
7+
# when testing methods test the data not a formatted message containing the data
8+
expect(catalogue.products_by_code['FR1'].code).to eq('FR1')
9+
end
10+
end
11+
end

Spec/product_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'product'
2+
3+
describe Product do
4+
it "has a code" do
5+
fruit_tea = Product.new('FR1','Fruit Tea', 311)
6+
expect(fruit_tea.code).to eq('FR1')
7+
end
8+
it "has a name" do
9+
fruit_tea = Product.new('FR1','Fruit Tea', 311)
10+
expect(fruit_tea.name).to eq('Fruit Tea')
11+
end
12+
it "has a price" do
13+
fruit_tea = Product.new('FR1','Fruit Tea', 311)
14+
expect(fruit_tea.price).to eq(311)
15+
end
16+
end

Spec/spec_helper.rb

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# The generated `.rspec` file contains `--require spec_helper` which will cause
4+
# this file to always be loaded, without a need to explicitly require it in any
5+
# files.
6+
#
7+
# Given that it is always loaded, you are encouraged to keep this file as
8+
# light-weight as possible. Requiring heavyweight dependencies from this file
9+
# will add to the boot time of your test suite on EVERY test run, even for an
10+
# individual file that may not need all of that loaded. Instead, consider making
11+
# a separate helper file that requires the additional dependencies and performs
12+
# the additional setup, and require it from the spec files that actually need
13+
# it.
14+
#
15+
# The `.rspec` file also contains a few flags that are not defaults but that
16+
# users commonly want.
17+
#
18+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19+
RSpec.configure do |config|
20+
# rspec-expectations config goes here. You can use an alternate
21+
# assertion/expectation library such as wrong or the stdlib/minitest
22+
# assertions if you prefer.
23+
config.expect_with :rspec do |expectations|
24+
# This option will default to `true` in RSpec 4. It makes the `description`
25+
# and `failure_message` of custom matchers include text for helper methods
26+
# defined using `chain`, e.g.:
27+
# be_bigger_than(2).and_smaller_than(4).description
28+
# # => "be bigger than 2 and smaller than 4"
29+
# ...rather than:
30+
# # => "be bigger than 2"
31+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32+
end
33+
34+
# rspec-mocks config goes here. You can use an alternate test double
35+
# library (such as bogus or mocha) by changing the `mock_with` option here.
36+
config.mock_with :rspec do |mocks|
37+
# Prevents you from mocking or stubbing a method that does not exist on
38+
# a real object. This is generally recommended, and will default to
39+
# `true` in RSpec 4.
40+
mocks.verify_partial_doubles = true
41+
end
42+
43+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
44+
# have no way to turn it off -- the option exists only for backwards
45+
# compatibility in RSpec 3). It causes shared context metadata to be
46+
# inherited by the metadata hash of host groups and examples, rather than
47+
# triggering implicit auto-inclusion in groups with matching metadata.
48+
config.shared_context_metadata_behavior = :apply_to_host_groups
49+
50+
# The settings below are suggested to provide a good initial experience
51+
# with RSpec, but feel free to customize to your heart's content.
52+
=begin
53+
# This allows you to limit a spec run to individual examples or groups
54+
# you care about by tagging them with `:focus` metadata. When nothing
55+
# is tagged with `:focus`, all examples get run. RSpec also provides
56+
# aliases for `it`, `describe`, and `context` that include `:focus`
57+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
58+
config.filter_run_when_matching :focus
59+
60+
# Allows RSpec to persist some state between runs in order to support
61+
# the `--only-failures` and `--next-failure` CLI options. We recommend
62+
# you configure your source control system to ignore this file.
63+
config.example_status_persistence_file_path = "spec/examples.txt"
64+
65+
# Limits the available syntax to the non-monkey patched syntax that is
66+
# recommended. For more details, see:
67+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
68+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
69+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
70+
config.disable_monkey_patching!
71+
72+
# This setting enables warnings. It's recommended, but in some cases may
73+
# be too noisy due to issues in dependencies.
74+
config.warnings = true
75+
76+
# Many RSpec users commonly either run the entire suite or an individual
77+
# file, and it's useful to allow more verbose output when running an
78+
# individual spec file.
79+
if config.files_to_run.one?
80+
# Use the documentation formatter for detailed output,
81+
# unless a formatter has already been configured
82+
# (e.g. via a command-line flag).
83+
config.default_formatter = 'doc'
84+
end
85+
86+
# Print the 10 slowest examples and example groups at the
87+
# end of the spec run, to help surface which specs are running
88+
# particularly slow.
89+
config.profile_examples = 10
90+
91+
# Run specs in random order to surface order dependencies. If you find an
92+
# order dependency and want to debug it, you can fix the order by providing
93+
# the seed, which is printed after each run.
94+
# --seed 1234
95+
config.order = :random
96+
97+
# Seed global randomization in this process using the `--seed` CLI option.
98+
# Setting this allows you to use `--seed` to deterministically reproduce
99+
# test failures related to randomization by passing the same `--seed` value
100+
# as the one that triggered the failure.
101+
Kernel.srand config.seed
102+
=end
103+
end

Spec/supermarket_spec.rb

-30
This file was deleted.

0 commit comments

Comments
 (0)