0

I am creating a simple CC class that can create and update a credit card. To do this, I have created cc_bal{} as an instance object so it can update respect credit cards. The hash is to save and update a person and the amount on their cc. I end up getting an output of just the original amount that was created and not the updated amount

Heres the code:

class CC

    def initialize(cc_name, cc_bal = {}, open_cc = false)
        @cc_name = cc_name
        @cc_bal = cc_bal
        @open_cc = open_cc
    end

    def create(initAmount, person)
        if initAmount > 0
            @open_cc = true
            @cc_bal[:person]=initAmount
            puts "congrats #{person} on opening your new #{@cc_name} CC! with $#{@cc_bal[:person]}" 
        else
            puts "sorry not enough funds"
        end
    end

    def update(amount, person)
        if @open_cc == true
            @cc_bal[:person] + amount
        else
            puts "sorry no account created, #{person}"
        end
        puts "#{person} has CC balance of #{@cc_bal[:person]}"
    end
end


#chase = Bank.new("JP Morgan Chase")
#wells_fargo = Bank.new("Wells Fargo")
me = Person.new("Shehzan", 500)
friend1 = Person.new("John", 1000)
#chase.open_account(me)
#chase.open_account(friend1)
#wells_fargo.open_account(me)
#wells_fargo.open_account(friend1)
#chase.deposit(me, 200)
#chase.deposit(friend1, 300)
#chase.withdraw(me, 50)
#chase.transfer(me, wells_fargo, 100)
#chase.deposit(me, 5000)
#chase.withdraw(me, 5000)
#puts chase.total_cash_in_bank
#puts wells_fargo.total_cash_in_bank
credit_card = CC.new("Visa")
credit_card.create(10,me)
credit_card.update(50,me)
credit_card.create(20,friend1)
credit_card.update(40,friend1)

Please disregard the function calls that are commented out.

Any idea why the CC's are not updatiing?

rahul2001
  • 1,577
  • 2
  • 17
  • 32

1 Answers1

1
if @open_cc == true
  @cc_bal[:person] + amount
else

You increase the amount, but you don't set the new value anywhere. It should be

if @open_cc == true
  @cc_bal[:person] += amount
else

Note. The code needs some serious refactoring and cleanup.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • Serious is an understatement. If this is handling real money I'd be extremely worried. – tadman Oct 30 '14 at 21:43
  • I also guess, it's `@cc_bal[person]`, not `@cc_bal[:person]` based on the set of parameters passed to create and update. – moonfly Oct 30 '14 at 22:18
  • Sorry, a bit new to programming -- what exactly would you look for when refactoring and cleaning this code up? All commenting and superficial things aside, is there something wrong with the logic?? – rahul2001 Oct 30 '14 at 23:49