Created
April 21, 2020 13:24
-
-
Save overloadedargs/cd6b4ffc3b0c9b2c81f044438c431a06 to your computer and use it in GitHub Desktop.
Another checkout
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
require_relative 'product' | |
class Checkout | |
attr_reader :checkout_items, :products | |
def initialize | |
@products = Hash.new | |
@checkout_items = [] | |
load_products | |
end | |
def scan(product_code) | |
@checkout_items << product_code | |
end | |
def total | |
total = 0.0 | |
@checkout_items.each do |product_code| | |
product = @products[product_code] | |
total += product.price | |
end | |
total | |
end | |
private | |
def load_products | |
checkout_data.each do |product| | |
@products[product[0]] = Product.new(product[0], product[1], product[2]) | |
end | |
end | |
def checkout_data | |
[ | |
["FR1", "Fruit tea", 3.11], | |
["SR1", "Strawberries", 5.00] | |
] | |
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
require_relative '../lib/checkout' | |
describe Checkout do | |
let(:checkout) { Checkout.new } | |
it "should be a checkout" do | |
expect(checkout).to be_a Checkout | |
end | |
context "with one item" do | |
before do | |
checkout.scan("FR1") | |
end | |
it "should be able to sum a total" do | |
expect(checkout.total).to eq 3.11 | |
end | |
end | |
context "with one item" do | |
before do | |
checkout.scan("SR1") | |
checkout.scan("FR1") | |
end | |
it "should be able to sum a total" do | |
expect(checkout.total).to eq 8.11 | |
end | |
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
class Product | |
attr_reader :code, :name, :price | |
def initialize(code, name, price) | |
@code = code | |
@name = name | |
@price = price | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment