if statement - Ruby program help (ATM program) -
i'm newb ruby user using ruby version 2.1.5p273 , below created atm simulator program takes user input of deposits , withdrawals, , displays balance after. struggling ifs, elses , loops. want put decision making statement in beginning, asks if user wants withdraw, deposit, check balance, or end session. want put decision making statement in end, asks if user wants continue (which go beginning, or end session). general idea of want below, overall program below idea code. know it's wrong it's want like, in making correct , working code appreciated.
print "would (w)ithdraw, (d)eposit, or (c)heck balance or (e)nd session? if "(w)ithdraw" # i'd make "press w withdraw" bank_account.withdraw elsif "(d)eposit" # i'd make "press d deposit" bank_account.deposit elsif "(c)heck balance" # i'd make "press c check balance" bank_account.show_balance elseif "(e)nd session" # i'd make "press e end session" end #this program atm simulator, takes user input of deposits , withdrawals, , displays balance after. class bankaccount def initialize(name) @transations = [] @balance = 0 end def deposit print "how deposit? " amount = gets.chomp @balance += amount.to_f puts "$#{amount} deposited." end def withdraw print "how withdraw?" amount = gets.chomp @balance -= amount.to_f puts "#{amount} withdrawn" end def show_balance puts "your balance #{@balance}" end end bank_account = bankaccount.new("justin g") bank_account.class # => bankaccount print "welcome jay's atm!\n" bank_account.deposit bank_account.show_balance bank_account.withdraw `enter code here`bank_account.show_balance puts "thank you"
this rudimentary should started. please let me know if have additional questions on i'm doing in code. part should self-explanatory if you're familiar other object-oriented programming languages.
here's atm:
# atm.rb require './bank_account.rb' cmd = "" account = bankaccount.new("justin g") puts "***welcome #{account.name}'s atm***\n\n" while cmd != "e" puts "would (w)ithdraw, (d)eposit or (c)heck balance?" puts "you can (e)nd session." cmd = gets.chomp case cmd when "w" puts "how withdraw?" amount = gets.chomp # expect float # handle incorrect input somehow, either here or # preferably in bankaccount#withdraw method account.withdraw(amount) when "d" puts "how deposit?" amount = gets.chomp # expect float # handle incorrect input somehow, either here or # preferably in bankaccount#deposit method account.deposit(amount) when "c" puts "your balance $%.2f\n" % account.balance else # didn't understand command puts "didn't understand command. try again." unless cmd == "e" end end
here's bank account code:
# bank_account.rb class bankaccount attr_reader :name, :balance def initialize(name) @name = name @transactions = [] @balance = 0.0 end def withdraw(amount) # todo: check amount valid, else error @balance -= amount.to_f # todo: check if sufficient funds available puts "$%.2f withdrawn.\n" % amount end def deposit(amount) # todo: check amount valid, else error @balance += amount.to_f puts "$%.2f deposited.\n" % amount end end
Comments
Post a Comment