Class: InvestedCrypto
- Inherits:
-
Object
- Object
- InvestedCrypto
- Defined in:
- lib/app/invested_crypto.rb
Constant Summary collapse
- @@all =
[]
- @@pastel =
Pastel.new
- @@prompt =
TTY::Prompt.new
Instance Attribute Summary collapse
-
#amount_invested ⇒ Object
Returns the value of attribute amount_invested.
-
#entry_rate ⇒ Object
Returns the value of attribute entry_rate.
-
#exit_rate ⇒ Object
Returns the value of attribute exit_rate.
-
#name ⇒ Object
Returns the value of attribute name.
-
#number_owned ⇒ Object
Returns the value of attribute number_owned.
-
#total_amount ⇒ Object
Returns the value of attribute total_amount.
Class Method Summary collapse
- .add_to_player ⇒ Object
- .all ⇒ Object
- .ask_for_amount ⇒ Object
- .ask_for_number ⇒ Object
- .clear ⇒ Object
- .if_balance_negative? ⇒ Boolean
- .select_crypto_to_invest ⇒ Object
- .set_and_show_total_amount ⇒ Object
- .set_exit_rate ⇒ Object
- .set_invested_crypto_details(invested_crypto, name, rate, amount) ⇒ Object
- .show_investments ⇒ Object
- .total ⇒ Object
Instance Method Summary collapse
-
#initialize(name = "", entry_rate = 0, exit_rate = 0, amount_invested = 0, number_owned = 0, total_amount = 0) ⇒ InvestedCrypto
constructor
A new instance of InvestedCrypto.
Constructor Details
#initialize(name = "", entry_rate = 0, exit_rate = 0, amount_invested = 0, number_owned = 0, total_amount = 0) ⇒ InvestedCrypto
Returns a new instance of InvestedCrypto.
11 12 13 14 15 16 17 18 19 |
# File 'lib/app/invested_crypto.rb', line 11 def initialize name="", entry_rate=0, exit_rate=0, amount_invested=0, number_owned=0, total_amount=0 @name = name @entry_rate = entry_rate @exit_rate = exit_rate @amount_invested = amount_invested @number_owned = number_owned @total_amount = total_amount @@all << self end |
Instance Attribute Details
#amount_invested ⇒ Object
Returns the value of attribute amount_invested.
6 7 8 |
# File 'lib/app/invested_crypto.rb', line 6 def amount_invested @amount_invested end |
#entry_rate ⇒ Object
Returns the value of attribute entry_rate.
6 7 8 |
# File 'lib/app/invested_crypto.rb', line 6 def entry_rate @entry_rate end |
#exit_rate ⇒ Object
Returns the value of attribute exit_rate.
6 7 8 |
# File 'lib/app/invested_crypto.rb', line 6 def exit_rate @exit_rate end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/app/invested_crypto.rb', line 6 def name @name end |
#number_owned ⇒ Object
Returns the value of attribute number_owned.
6 7 8 |
# File 'lib/app/invested_crypto.rb', line 6 def number_owned @number_owned end |
#total_amount ⇒ Object
Returns the value of attribute total_amount.
6 7 8 |
# File 'lib/app/invested_crypto.rb', line 6 def total_amount @total_amount end |
Class Method Details
.add_to_player ⇒ Object
29 30 31 |
# File 'lib/app/invested_crypto.rb', line 29 def self.add_to_player Player.all[0].crypto_invested = @@all end |
.all ⇒ Object
21 22 23 |
# File 'lib/app/invested_crypto.rb', line 21 def self.all @@all end |
.ask_for_amount ⇒ Object
40 41 42 43 |
# File 'lib/app/invested_crypto.rb', line 40 def self.ask_for_amount @@prompt.ask(" Please type the amount you want to invest:") end |
.ask_for_number ⇒ Object
33 34 35 36 37 38 |
# File 'lib/app/invested_crypto.rb', line 33 def self.ask_for_number @@prompt.ask("Please select the number for coin you want to invest:") do |q| q.validate(/^([1-9]|1[012345])$/, "Invalid Input") end end |
.clear ⇒ Object
25 26 27 |
# File 'lib/app/invested_crypto.rb', line 25 def self.clear @@all.clear end |
.if_balance_negative? ⇒ Boolean
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/app/invested_crypto.rb', line 78 def self.if_balance_negative? if Player.all[0].balance.negative? @@all.last.amount_invested -= Player.all[0].balance.abs sleep(2) puts @@pastel.cyan.bold(" Due to insufficient balance, you have only invested #{@@all.last.amount_invested} in #{@@all.last.name.upcase}") Player.all[0].balance = 0 end end |
.select_crypto_to_invest ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/app/invested_crypto.rb', line 53 def self.select_crypto_to_invest player_new = Player.all[0] until player_new.balance <= 0 do invested_crypto = InvestedCrypto.new selection = ask_for_number amount_invested = ask_for_amount selection_name = player_new.past_crypto_list.each_with_index.select{|crypto, index| selection.to_i == (index+1)} player_new.balance = player_new.balance - amount_invested.to_i puts @@pastel.cyan.bold(" You have invested #{amount_invested} AUD in #{selection_name[0][0][0].upcase}. You have #{player_new.balance} AUD left ") set_invested_crypto_details(invested_crypto, selection_name[0][0][0], selection_name[0][0][2], amount_invested ) if_balance_negative? end end |
.set_and_show_total_amount ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/app/invested_crypto.rb', line 118 def self.set_and_show_total_amount puts @@pastel.cyan.bold(" Below is your investments' CALCULATIONS...... Coin-Name Owned Invested(AUD) TotalAmount(AUD) -------------- ----------- ----------------- ----------------- ") @@all.each do |i| i.total_amount = (i.number_owned * i.exit_rate).to_f.round(3) puts @@pastel.green.bold(" #{i.name.upcase} #{i.number_owned} #{i.amount_invested} #{i.total_amount} ") end total end |
.set_exit_rate ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/app/invested_crypto.rb', line 108 def self.set_exit_rate Player.all[0].exited_crypto_data.each do |exited_data| @@all.each do |investment| if exited_data[0] == investment.name investment.exit_rate = exited_data[2] end end end end |
.set_invested_crypto_details(invested_crypto, name, rate, amount) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/app/invested_crypto.rb', line 45 def self.set_invested_crypto_details(invested_crypto, name, rate, amount) invested_crypto.name = name invested_crypto.entry_rate = rate invested_crypto.amount_invested = amount.to_i invested_crypto.number_owned = (amount.to_i / rate).to_f.round(3) end |
.show_investments ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/app/invested_crypto.rb', line 90 def self.show_investments puts @@pastel.cyan.bold(" Below is your INVESTMENTS...... Coin-Name Owned Invested(AUD) -------------- ----------- ----------------- ") @@all.each do |i| puts @@pastel.green.bold(" #{i.name.upcase} #{i.number_owned} #{i.amount_invested} ") end end |