Class: Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/fifo_lifo/utils.rb

Instance Method Summary collapse

Constructor Details

#initialize(bitcoins, prices) ⇒ Utilities

Returns a new instance of Utilities.



2
3
4
5
# File 'lib/fifo_lifo/utils.rb', line 2

def initialize(bitcoins, prices)
    @bitcoins = bitcoins
    @prices = prices
end

Instance Method Details

#calculate_profit_or_loss(purchases, purchases_prices) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fifo_lifo/utils.rb', line 58

def calculate_profit_or_loss(purchases, purchases_prices)
    total_cost = 0
    cost_of_goods_sold = 0
    total_income = 0
    temp_remaining_invetory = 0

    sells = get_sell_and_prices[0]
    sells_prices = get_sell_and_prices[1]
    
    purchases.each_with_index do |curr_purchase, purchase_index|
        invetory = curr_purchase + temp_remaining_invetory  # initializing the invetory
        temp_remaining_invetory = 0
        total_amt_spent = invetory * purchases_prices[purchase_index] # setting the amout spent purchasing a given good
        temp_income = 0 # initializing a temporary income variable to zero
        sells.each_with_index do |curr_sell, index|
            break if sells.sum().zero? # breaking the loop if there is no more sells remaining on the list
            next if curr_sell.zero?
            if invetory.zero? || ((invetory + curr_sell) < 0) # whenever the current invetory is less than the sell
                temp_remaining_invetory = invetory
                cost_of_goods_sold += total_amt_spent
                total_income += temp_income
                temp_income = 0 # resetting the temporary income variable to zero
                total_amt_spent = 0
                break
            end
            temp_income += (-1 * curr_sell) * sells_prices[index] # calculating the income at every selling and save it in a temporary variable
            invetory += curr_sell # modifying the invetory whenever we sell
            sells[index] = 0 # setting the sell to zero so that it won't count for the next time
        end
        remaining_invetory_cost = total_amt_spent - (invetory * purchases_prices[purchase_index])
        total_income += temp_income
        cost_of_goods_sold += remaining_invetory_cost
        break if sells.sum().zero?
    end
    total_profit = total_income - cost_of_goods_sold # calculating the final total profit
    result = if total_profit.positive?
        "The total profit is: #{total_profit}"
    else
        "The loss is: #{total_profit}"
    end
    [result, cost_of_goods_sold, total_income]
end

#get_purchase_and_pricesObject

getting all purchases and their prices



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

def get_purchase_and_prices
    purchases = []
    mprices = []
    @bitcoins.each_with_index do |curr_bit, index|
        if curr_bit.positive?
            purchases << curr_bit
            mprices << @prices[index]
        end
    end
    [purchases, mprices]
end

#get_sell_and_pricesObject

getting all sells and prices



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fifo_lifo/utils.rb', line 20

def get_sell_and_prices
    sells = []
    mprices = []
    @bitcoins.each_with_index do |curr_bit, index|
        if curr_bit.negative?
            sells << curr_bit
            mprices << @prices[index]
        end
    end
    [sells, mprices]
end

#get_total_costObject

get the total cost of all purchased goods



33
34
35
36
37
38
39
40
41
# File 'lib/fifo_lifo/utils.rb', line 33

def get_total_cost
    purchases = get_purchase_and_prices[0]
    purchases_prices = get_purchase_and_prices[1]
    total_cost = 0
    purchases.each_with_index do |curr_purchase, index|
        total_cost += (curr_purchase * purchases_prices[index])
    end
    total_cost
end

#get_unit_purchasedObject

get the number of all unit purchased



49
50
51
# File 'lib/fifo_lifo/utils.rb', line 49

def get_unit_purchased
    get_purchase_and_prices[0].sum()
end

#get_unit_soldObject

get the number of all unit sold



44
45
46
# File 'lib/fifo_lifo/utils.rb', line 44

def get_unit_sold
    get_sell_and_prices[0].sum()
end

#reverse_array(arr) ⇒ Object

reverse the array



54
55
56
# File 'lib/fifo_lifo/utils.rb', line 54

def reverse_array(arr)
    arr.reverse()
end