Class: InvestedCrypto

Inherits:
Object
  • Object
show all
Defined in:
lib/app/invested_crypto.rb

Constant Summary collapse

@@all =
[]
@@pastel =
Pastel.new
@@prompt =
TTY::Prompt.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_investedObject

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_rateObject

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_rateObject

Returns the value of attribute exit_rate.



6
7
8
# File 'lib/app/invested_crypto.rb', line 6

def exit_rate
  @exit_rate
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/app/invested_crypto.rb', line 6

def name
  @name
end

#number_ownedObject

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_amountObject

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_playerObject



29
30
31
# File 'lib/app/invested_crypto.rb', line 29

def self.add_to_player
    Player.all[0].crypto_invested = @@all
end

.allObject



21
22
23
# File 'lib/app/invested_crypto.rb', line 21

def self.all 
    @@all
end

.ask_for_amountObject



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_numberObject



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

.clearObject



25
26
27
# File 'lib/app/invested_crypto.rb', line 25

def self.clear 
    @@all.clear
end

.if_balance_negative?Boolean

Returns:

  • (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_investObject



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_amountObject



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_rateObject



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_investmentsObject



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

.totalObject



137
138
139
140
141
142
143
# File 'lib/app/invested_crypto.rb', line 137

def self.total
    total_of_totals = 0
    @@all.each do |i|
        total_of_totals += i.total_amount
    end
    Player.all[0].total_balance = total_of_totals.to_f.round(3)
end