Class: Checkout

Inherits:
Object
  • Object
show all
Defined in:
lib/unit4/checkout/checkout.rb

Overview

the main class that is used for scaning the items and applying the discounts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(promotional_rules = {}) ⇒ Checkout

Returns a new instance of Checkout.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/unit4/checkout/checkout.rb', line 7

def initialize(promotional_rules = {})
  # custom exceptions for incorrect Checkout.new({...}) parameters
  raise PromotionalRulesTypeError, promotional_rules.class.name unless promotional_rules.is_a? Hash
  raise PromotionalRulesForbiddenKeys unless correct_keys?(promotional_rules)

  @promotional_rules = promotional_rules
  @total = 0
  @basket = Basket.new
  @saved_prices = {}
  create_db_connection
end

Instance Attribute Details

#totalObject (readonly)

Returns the value of attribute total.



5
6
7
# File 'lib/unit4/checkout/checkout.rb', line 5

def total
  @total
end

Instance Method Details

#scan(item) ⇒ Object

no structure for the item variable item given, assumed id from sample data input



20
21
22
23
24
# File 'lib/unit4/checkout/checkout.rb', line 20

def scan(item)
  add_to_basket(item)
  calculate_total(item)
  puts "Item with ID '#{item}' has been added to the basket successfully!"
end