2014-12-06 45 views
0

我是一個使用Ruby版本2.1.5p273及以下版本的newb Ruby用戶我創建了一個Atm模擬器程序,它接受用戶輸入存款和取款,然後顯示餘額。我正在與ifs,elses和循環鬥爭。我想在開始時提出一個決策聲明,詢問用戶是否想要提取,存款,查看餘額或結束會話。我還希望最終做出決策聲明,詢問用戶是否想要繼續(這將返回到開始或結束會話)。我對我想要的樣子的一般想法會在下面,整個程序在想法代碼下面。我知道這是錯誤的,但它正是我想要它的樣子,所以任何幫助將它變成正確和有效的代碼將不勝感激。Ruby程序幫助(ATM程序)

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" 

回答

0

這是相當簡陋但應該讓你開始。如果您對代碼中的內容有任何疑問,請告訴我。大多數情況下,如果你熟悉其他面向對象的編程語言,它應該是不言而喻的。

這是你的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 
+0

太謝謝你了!這是完美的,我保存在兩個.rb文件,atm.rb和bank_account.rb,運行它在CMD和所有功能正常工作,存款取消支票餘額和結束。我要求的主要捷徑也在那裏。接下來我要做的是添加一個輸出收據文件。我會自己解決這個問題,如果我遇到麻煩,我希望你會再次出現。再次感謝你! – jmgeronimo 2014-12-06 04:19:04

+0

@jmgeronimo不客氣。感謝您接受答案。我希望你喜歡學習Ruby。 – 2014-12-06 04:25:15