2014-12-06 3 views
0

저는 Ruby 버전 2.1.5p273 이하를 사용하는 newb Ruby 사용자입니다. 입금 및 인출에 대한 사용자 입력이 필요한 Atm 시뮬레이터 프로그램을 작성한 후 잔액을 표시합니다. 나는 if, elses 및 loops로 어려움을 겪고있다. 나는 처음에 의사 결정 진술을하고 싶습니다. 사용자가 철수, 입금, 잔액을 확인하거나 세션을 종료 할 것인지 묻습니다. 나는 또한 의사 결정 진술을 맨 끝에두고 싶다. 사용자가 계속하기를 원하면 묻는다. (처음으로 되돌아 가거나 세션을 종료 할 것이다). 내가 원했던 것의 내 일반적인 생각은 아래와 같을 것입니다. 전반적인 프로그램은 아이디어 코드 아래에 있습니다. 나는 그것이 틀렸다는 것을 알고있다. 그러나 그것이 내가보기를 원하는 것일 뿐이므로, 그것을 정확하고 작동하는 코드로 만드는데 도움이된다면 크게 감사 할 것이다.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.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 

가 여기에 은행 계좌 코드입니다 :

은 여기 ATM의

# 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

+0

@jmgeronimo 환영합니다. 답변을 수락 해 주셔서 감사합니다. 루비를 배우는 즐거움을 기원합니다. –