Class: BuyerAPI

Inherits:
Object
  • Object
show all
Extended by:
UserModule
Defined in:
lib/BuyerAPI.rb

Constant Summary collapse

@@name =
"Raj Negi"
@@money =
5000
@@cc_details =
"icic"

Class Method Summary collapse

Methods included from UserModule

get_number, get_string, list_products, search_product

Class Method Details

.buyObject



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
# File 'lib/BuyerAPI.rb', line 14

def self.buy
  puts "\n\tEnter the details of product you want to buy ('q' to quit): "
  product_id = get_number("ID")
  product = ProductAPI.search_id(product_id)

  if !product
    puts "Product doesn't exist"
  elsif @@money < product[:price]
    puts "Insufficient funds to make purchase!"
  elsif product[:quantity].to_i < 1
    puts "Product out of stock!"
  else
    ProductAPI.update(product[:id], :quantity, product[:quantity] - 1)
    @@money -= product[:price]
    transcation = {
      product_name: product[:name],
      buyer_name: @@name,
      buyer_cc_details: @@cc_details,
      timestamp: Time.now.to_s,
      amount_paid: product[:price]
    }
    TranscationAPI.record(transcation)
    puts "\n\tPurchase successful, current amount left: #{@@money}"
  end
end

.set_cc_details(cc) ⇒ Object



48
49
50
# File 'lib/BuyerAPI.rb', line 48

def self.set_cc_details(cc)
  @@cc_details = cc
end

.set_money(m) ⇒ Object



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

def self.set_money(m)
  @@money = m
end

.set_name(n) ⇒ Object



40
41
42
# File 'lib/BuyerAPI.rb', line 40

def self.set_name(n)
  @@name = n
end