Class: CcManager::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/cc_manager/account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cc_number, limit) ⇒ Account

Creates new account when Add action is mentioned in the file with required arguments.



20
21
22
23
24
25
26
27
28
# File 'lib/cc_manager/account.rb', line 20

def initialize(name, cc_number, limit)
  @errors = []
  @valid_account = false
  @name = name
  @balance = "$0"
  valid_amount?(limit) ? @limit = limit : @errors << "Account cannnot be Added - Invalid Amount specified for limit!!!"
  valid_card?(cc_number) ? @cc_number = cc_number : @errors << "#{name}'s account cannot be added - Credit Card is invalid!!!"
  return valid?
end

Instance Attribute Details

#balanceObject

Balance of the user, initially its vlaue is “$0”.



14
15
16
# File 'lib/cc_manager/account.rb', line 14

def balance
  @balance
end

#cc_numberObject

Credit Card number of the user, only stores if the credit card number is valid against Luhn 10 algorithm.



8
9
10
# File 'lib/cc_manager/account.rb', line 8

def cc_number
  @cc_number
end

#errorsObject

Contains error messages if any.



17
18
19
# File 'lib/cc_manager/account.rb', line 17

def errors
  @errors
end

#limitObject

Charging limit set while creating new account.



11
12
13
# File 'lib/cc_manager/account.rb', line 11

def limit
  @limit
end

#nameObject

Name of the user.



5
6
7
# File 'lib/cc_manager/account.rb', line 5

def name
  @name
end

Instance Method Details

#charge(charged_amount) ⇒ Object

Charge the user according to the given amount to charge but amount should not exceed the limit.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cc_manager/account.rb', line 31

def charge(charged_amount)
  if valid?
    if valid_amount?(charged_amount)
      limit_val = get_amount_in_integer(self.limit)
      charged_amount_val = get_amount_in_integer(charged_amount)
      balance = get_amount_in_integer(self.balance)
      if charged_amount_val+balance <= limit_val
        self.balance = get_amount_in_string(balance + charged_amount_val)
        return true
      else
        return "Cannot Charge - Charged Amount exceeded the limit!!!"
      end
    else
      return "Cannot Charge - Invalid Amount!!!"
    end
  else
    return "Cannot Charge - Invalid Account!!!"
  end
end

#credit(credited_amount) ⇒ Object

Add credit to the user according to the given amount to charge.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cc_manager/account.rb', line 52

def credit(credited_amount)
  if valid?
    if valid_amount?(credited_amount)
      balance = get_amount_in_integer(self.balance)
      credited_amount_val = get_amount_in_integer(credited_amount)
      self.balance = get_amount_in_string(balance - credited_amount_val)
      return true
    else
      return "Cannot Add Credit - Invalid Amount!!!"
    end
  else
    return "Cannot Add Credit - Invalid Account!!!"
  end
end

#valid?Boolean

Validates whether the created account is valid or not.

Returns:

  • (Boolean)


68
69
70
# File 'lib/cc_manager/account.rb', line 68

def valid?
  @errors.empty?
end