Ruby程序帮助(ATM程序)

时间:2014-12-06 03:07:18

标签: ruby if-statement conditional

我是使用Ruby版本2.1.5p273的新手Ruby用户,我创建了一个Atm模拟器程序,用户输入存款和取款,然后显示余额。我正在努力与ifs,elses和循环。我想在开头做一个决策制定声明,询问用户是否想要提款,存款,检查余额或结束会话。我还想最后提出一个决策制定声明,询问用户是否想要继续(这将返回到开头,或者结束会话)。我对我想要的样子的总体想法如下,整个程序低于想法代码。我知道这是错的,但这正是我希望它看起来的样子,所以任何帮助使它成为正确和正常工作的代码将非常感激。

print "Would you like to (w)ithdraw, (d)eposit, or (c)heck your balance or (e)nd your session?
if "(w)ithdraw"  # i'd like to make this do a "press w for withdraw"           
   bank_account.withdraw
elsif "(d)eposit"   #  i'd like to make this do a "press d for deposit"
   bank_account.deposit
elsif "(c)heck your balance" #  i'd like to make this do a "press c to check your balance" 
bank_account.show_balance
elseif "(e)nd your session" #  i'd like to make this do a "press e to end your session"
end




#This program is an ATM simulator, it takes user input of deposits and withdrawals, and then     displays the balance after.

class BankAccount

  def initialize(name)
   @transations = []
   @balance = 0
  end

  def deposit
   print "How much would you like to deposit? "
   amount = gets.chomp
   @balance += amount.to_f
   puts "$#{amount} deposited."
  end

  def withdraw
   print "How much would you like to withdraw?"
   amount = gets.chomp
   @balance -= amount.to_f
   puts "#{amount} withdrawn"
  end

  def show_balance
   puts "Your balance is #{@balance}"
  end


end

bank_account = BankAccount.new("Justin G")
bank_account.class # => BankAccount

print "Welcome to Jay's ATM!\n"
bank_account.deposit
bank_account.show_balance
bank_account.withdraw
`enter code here`bank_account.show_balance
puts "Thank you"

1 个答案:

答案 0 :(得分:1)

这是相当简陋的,但应该让你开始。如果您对我在代码中所做的事情有其他疑问,请与我们联系。如果您熟悉其他面向对象的编程语言,那么在大多数情况下它应该是相当不言自明的。

这是你的ATM:

# atm.rb

require './bank_account.rb'

cmd = ""
account = BankAccount.new("Justin G")

puts "***Welcome to #{account.name}'s ATM***\n\n"

while cmd != "e" do
  puts "Would you like to (w)ithdraw, (d)eposit or (c)heck your balance?"
  puts "You can also (e)nd your session."
  cmd = gets.chomp

  case cmd
  when "w"
    puts "How much would you like to withdraw?"
    amount = gets.chomp # expect this to be a float

    # handle incorrect input somehow, either here or
    # preferably in the BankAccount#withdraw method
    account.withdraw(amount)
  when "d"
    puts "How much would you like to deposit?"
    amount = gets.chomp # expect this to be a float

    # handle incorrect input somehow, either here or
    # preferably in the BankAccount#deposit method
    account.deposit(amount)
  when "c"
    puts "Your balance is $%.2f\n" % account.balance
  else
    # didn't understand the command
    puts "Didn't understand your command. Try again." unless cmd == "e"
  end
end

这是银行帐户代码:

# bank_account.rb
class BankAccount
  attr_reader :name, :balance

  def initialize(name)
    @name = name
    @transactions = []
    @balance = 0.0
  end

  def withdraw(amount)
    # TODO: check that amount is valid, else error
    @balance -= amount.to_f
    # TODO: check if sufficient funds available
    puts "$%.2f successfully withdrawn.\n" % amount
  end

  def deposit(amount)
    # TODO: check that amount is valid, else error
    @balance += amount.to_f
    puts "$%.2f successfully deposited.\n" % amount
  end
end