1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## after 

def discount_by_item(apple)
  full_quantity = 0
  discount_quantity = 0
  full_price = apple.price
  discount_price = apple.price - apple.price_plan.discount
  if (apple.price_plan.limit.nil?) # if we don't have a limit
    full_quantity=apple.quantity
    if apple.quantity>apple.price_plan.buy_quantity   # we're buying more than needed for the discount
      full_quantity=apple.price_plan.buy_quantity     # then we're paying full price for the buy quantity  
      discount_quantity=apple.quantity-full_quantity  # and discounted price for all th test
    end
  else
    # how many items in each group
    groups_of=apple.price_plan.limit+apple.price_plan.buy_quantity
    groups=apple.quantity/groups_of
    full_quantity=groups*apple.price_plan.buy_quantity
    discount_quantity=groups*(groups_of-full_quantity)
    # if we have some left over items
    if (left_overs=(apple.quantity-full_quantity+discount_quantity))>0
      # TODO: your homework
    end    
  end
  full_quantity, full_price, discount_quantity, discount_price
end


## before


def discount_by_item(apple)
  full_qty_counter = 0
  disc_qty_counter = 0
  full_qty = 0
  discount_qty = 0
  full_price = apple.price
  discount_price = apple.price - apple.price_plan.discount
  if (apple.price_plan.limit > 0)
    for i in 1..apple.quantity
      if (full_qty_counter >= apple.price_plan.buy_qty && disc_qty_counter <= apple.price_plan.limit)
        discount_qty += 1
        disc_qty_counter += 1
      else
        full_qty += 1
        full_qty_counter += 1
      end
      if (disc_qty_counter == apple.price_plan.limit)
        disc_qty_counter = 0
        full_qty_counter = 0
      end
    end
  else
    while (full_qty >= apple.price_plan.buy_qty)
      full_qty -= 1
      discount_qty += 1
    end
  end
  return full_qty, full_price, discount_qty, discount_price
end