Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
46 views
in Technique[技术] by (71.8m points)

Is there a way I can call a method from another class in ruby?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you should have a reference to an account associated with each transaction. You can then use that reference to access the account info you want. Since it also seems like you would just have a list of transactions and not store a fixed balance, I think you will need a method to get the balance after any transaction.

First I added a balance_after method to Account to calculate the balance after any given transaction. It does this by looking up the index of the passed in transaction and then summing up the amounts of all transactions prior to the one passed in.

require_relative 'transaction'
 
 class Account

  attr_reader :transactions

  def initialize
    @transactions = []
  end

  def balance
    @transactions.map(&:amount).sum
  end

  # new method to get the balance after a given transaction
  def balance_after(transaction)
    index = transactions.find_index(transaction)
    transactions[0..index].map(&:amount).sum
  end

  def deposit(amount)
    @transactions << Transaction.new(amount, self)
  end

  def withdraw(amount)
    @transactions << Transaction.new(-amount, self)
  end

  def header
    puts '   date   || credit || debit || balance || '
    puts '-------------------------------------------'
  end

  def print_statement
    header
    @transactions.map(&:formate)
  end
end

Next I added the account info into the Transaction and a method that calls balance_after() method on the account reference, passing a reference to current transaction.

 class Transaction
  attr_reader :amount, :timestamp, :account

  def initialize(amount, account)
    @amount = amount
    @timestamp = Time.now.strftime("%d/%m/%Y")
    @account = account
  end

  def balance()
    @account.balance_after(self)
  end

  def formate
    if @amount > 0
      puts "#{@timestamp}||#{@amount}||  || #{balance} ||"
    elsif
      puts "#{@timestamp}||  ||#{@amount}|| #{balance} ||"
    end
  end
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...