Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHarrison Hondo
7,283 PointsRight column won't align correctly
my file prints out something like this where the "Amount" and "60.00" are slightly off of where they should be
Description Amount
Beginning Balance 0.00
Paycheck 100.00
Groceries -40.00
Balance: 60.00
class BankAccount
attr_reader :name
def initialize(name)
@name = name
@transactions = []
add_transaction("Beginning Balance", 0)
end
def credit(description, amount)
add_transaction(description, amount)
end
def debit(description, amount)
add_transaction(description, -amount)
end
def add_transaction(description, amount)
@transactions.push(description: description, amount: amount)
end
def balance
balance = 0
@transactions.each do |transaction|
balance += transaction[:amount]
end
return balance
end
def to_s
"Name: #{name}, Balance: #{sprintf("%0.2f", balance)}"
end
def print_register
puts "#{name}'s Bank Account"
puts "-" * 40
puts "Description".ljust(30) +"Amount".rjust(10)
@transactions.each do |transaction|
puts transaction[:description].ljust(30) + "\t" + sprintf("%0.2f", transaction[:amount]).rjust(10)
end
puts "-" * 40
puts "Balance:".ljust(30) + sprintf("%0.2f", balance).rjust(10)
puts "-" * 40
end
end
bank_account = BankAccount.new("Jason")
bank_account.credit("Paycheck", 100)
bank_account.debit("Groceries", 40)
puts bank_account
puts "Register:"
bank_account.print_register
2 Answers
Raymond Wach
7,961 PointsHi Harrison Hondo, I'm not exactly sure what's happening on the line with:
"\t" + sprintf("%0.2f", transaction[:amount]).rjust(10)
It looks like that should work as it is. But I was able to fix what I believe is the issue you're having (the right justification doesn't include the part after the decimal point so the cents places stick out past "amount") just by changing it to rjust(8)
.
Pedro Pascual Lorenzo
5,802 PointsHi Harrison,
I think you have to remove the tab caracter "\t" , your final should look like: :
puts transaction[:description].ljust(30) + sprintf("%0.2f", transaction[:amount]).rjust(10)