class Utilities
Public Class Methods
new(bitcoins, prices)
click to toggle source
# File lib/fifo_lifo/utils.rb, line 2 def initialize(bitcoins, prices) @bitcoins = bitcoins @prices = prices end
Public Instance Methods
calculate_profit_or_loss(purchases, purchases_prices)
click to toggle source
# 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_prices()
click to toggle source
getting all purchases and their prices
# 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_prices()
click to toggle source
getting all sells and prices
# 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_cost()
click to toggle source
get the total cost of all purchased goods
# 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_purchased()
click to toggle source
get the number of all unit purchased
# File lib/fifo_lifo/utils.rb, line 49 def get_unit_purchased get_purchase_and_prices[0].sum() end
get_unit_sold()
click to toggle source
get the number of all unit sold
# File lib/fifo_lifo/utils.rb, line 44 def get_unit_sold get_sell_and_prices[0].sum() end
reverse_array(arr)
click to toggle source
reverse the array
# File lib/fifo_lifo/utils.rb, line 54 def reverse_array(arr) arr.reverse() end