这是我得到的错误:
banking.rb:110:语法错误,意外的keyword_true,期待 keyword_then或';'或者' \ n' 如果transfer_valid?= true则 ^ banking.rb:115:警告:否则没有救援就没用了
这是我的代码:
#---------------BANK ACCOUNT CLASS---------------#
class BankAccount
def initialize(name, balance=1000, status="open")
@name=name
@balance=balance
@status=status
end
#Change name
def name=(name)
@name=name
end
#Returns account name
def name
@name
end
#Sets account status
def status=(status)
@status=status
end
#Method lets you deposit money to account
def deposit(amount)
@balance+=amount.to_i
end
#Method lets you set your balance
def balance=(balance)
@balance=balance
end
#Returns your balance
def balance
@balance
end
#Method displays the balance
def display_balance
return "Your Balance is $#{@balance}."
end
#Method checks if account is valid
def valid?
if balance>0 and status=="open" then
return true
else
return false
end
end
#Method closes the account
def close_account
@status="closed"
end
#Method returns the status of the account
def status
return "@status"
end
#---------------TRANSFER CLASS---------------#
class Transfer
def initialize(sender, receiver, funds)
@sender=sender
@receiver=receiver
@funds=funds
@status="pending"
#WILL NEED TO FIGURE OUT A WAY FOR IT TO WORK FOR EVERY ACCOUNT
@sender_funds=sender_account_name.balance
@sender_account_status=sender_account_name.status
@receiver_funds=receiver_account_name.funds
@receiver_account_status=receiver_account_name.status
end
#Returns sender account name
def sender
@sender
end
#Change receiver
def receiver=(receiver)
@receiver=receiver
end
#Returns receiver account name
def receiver
@receiver
end
#Returns tranfer status
def status
@status
end
#Returns if accounts are valid for transfer
def transfer_valid?
if @sender_account_status=="open" && @receiver_account_status=="open" && @sender_funds-@funds>=0
return true
else
return false
end
end
#Transfers the money
def transfer
if transfer_valid?=true then
@receiver_funds+=@funds
@sender_funds-=@funds
else
@status="failed"
end
end
#Returns sender account funds after transfer
def sender_funds_after_transfer
@sender_funds
end
#Returns receiver account funds after transfer
def receiver_funds_after_transfer
@receiver_funds
end
end
#------------FOR TEST-------------#
puts "Enter the sender_account_name"
sender_account_name=gets.chomp.lowcase
sender_account_name=BankAccount.new(sender_account_name)
puts "Enter the receiver_account_name"
receiver_account_name=gets.chomp.lowcase
receiver_account_name=BankAccount.new(receiver_account_name)
puts "Sender has #{sender_account_name.balance}$ in his account."
puts "Receiver has #{receiver_account_name.balance}$ in his account."
#Makes the transfer
first_transfer=Transfers.new(sender_account_name, receiver_account_name, 20)
#Return account funds
puts "Sender has #{sender_account_name.sender_funds_after_transfer}$ in his account."
puts "Receiver has #{receiver_account_name.receiver_funds_after_transfer}$ in his account."
我可能以不正确的方式调用类中的方法。我不熟悉课程,所以我不知道该怎么做。
非常感谢您的回答和帮助。
答案 0 :(得分:2)
if transfer_valid?=true then
更改为
if transfer_valid?