Is it possible to see an updated balance when calling my print_statement
? In my Transaction
class, I am using an @amount
and @timestamp
instance variable to store a transaction into my @transaction
instance variable in my Account
class. I am then calling my print_statement
method which only prints out the @timestamp
and @amount
for each transaction.
In my desired outcome, I need to also print out an updated Balance
with each transaction. I am new to coding, so any advice would be much appreciated!
My desired outcome:
date || credit || debit || balance
14/01/2012 || || 500.00 || 2500.00
13/01/2012 || 2000.00 || || 3000.00
10/01/2012 || 1000.00 || || 1000.00
My current outcome:
2.7.0 :001 > require './lib/account'
=> true
2.7.0 :002 > require './lib/transaction'
=> false
2.7.0 :003 > account = Account.new
2.7.0 :004 > account.deposit(100)
=> [#<Transaction:0x00007f9eff8941a8 @amount=100, @timestamp="09/01/2021">]
2.7.0 :005 > account.withdraw(50)
=> [#<Transaction:0x00007f9eff8941a8 @amount=100, @timestamp="09/01/2021">, #<Transaction:0x00007f9efd9673c0 @amount=-50, @timestamp="09/01/2021">]
2.7.0 :006 > account.balance
=> 50
2.7.0 :007 > account.print_statement
date || credit || debit || balance ||
-------------------------------------------
09/01/2021||100|| || Balance ||
09/01/2021|| ||-50|| Balance ||
=> [nil, nil]
Account class
require_relative 'transaction'
class Account
attr_reader :transactions
def initialize
@transactions = []
end
def balance
@transactions.map(&:amount).sum
end
def deposit(amount)
@transactions << Transaction.new(amount)
end
def withdraw(amount)
@transactions << Transaction.new(-amount)
end
def header
puts ' date || credit || debit || balance || '
puts '-------------------------------------------'
end
def print_statement
header
@transactions.map(&:formate)
end
end
Transaction class
class Transaction
attr_reader :amount, :timestamp
def initialize(amount)
@amount = amount
@timestamp = Time.now.strftime("%d/%m/%Y")
end
def formate
if @amount > 0
puts "#{@timestamp}||#{@amount}|| || Balance ||"
elsif
puts "#{@timestamp}|| ||#{@amount}|| Balance ||"
end
end
end
question from:
https://stackoverflow.com/questions/65645441/is-there-a-way-i-can-call-a-method-from-another-class-in-ruby 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…