Skip to content

Instantly share code, notes, and snippets.

@rsl
Forked from btn0s/bank_proj
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsl/11037873 to your computer and use it in GitHub Desktop.
Save rsl/11037873 to your computer and use it in GitHub Desktop.
# Tip: If you name your gist foo.rb it will highlight the code as Ruby.
class Account
# Two things here. First you never use the reader methods you define here.
# You can either refactor the code to use them [see other gist]
# or just remove them and keep using the instance variables.
# Second, usually Rubyists [we have a name!] group class-level code
# like attr_reader or defining constants apart from the method definitions
# with newlines. Multiple calls of similar types [like another attr_reader here]
# would be back-to-back [no newlines] though. Just a readability thing.
attr_reader :name, :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
# I like to outdent private and protected declarations with a newline after. Readability again.
private
def pin
@pin = 1234
end
def pin_error
"Invalid Pin"
end
# Instead of having public methods down here, most Rubyists just put all the public methods at the top
# where you have initialize defined. This is totally valid. Just a style tip you will see.
# Methods are public by default so usually people just leave them in that public area
# and only switch to private/protected at the end of the class definition.
public
def display_balance(pin_number)
puts pin_number == pin ? "Balance: $#{@balance}." : pin_error
end
def withdraw(pin_number, amount)
if pin_number == pin
@balance -= amount
puts "Withdrew $#{amount}. New balance: $#{@balance}."
else
pin_error
end
end
def deposit(pin_number, amount)
if pin_number == pin
@balance += amount
puts "$#{amount} successfully despoited into your account, #{name}. New balance: $#{@balance}"
else
pin_error
end
end
end
class Machine
def initialize
startup
end
def startup
login = [1, 2]
puts "+--------------------+"
puts "|Welcome to your bank|"
puts "+--------------------+"
puts "| 1-login 2-create |"
puts "+--------------------+"
puts " "
print "..."
# Don't be afraid of making whatever strtp is clear by naming the variable clearly.
# Ruby values clarity and [wait for it...] readability.
strtp = gets.chomp.to_i
if strtp == login[0]
user_login
else
"Error"
end
end
def user_login
print "Username: "
@user_name = gets.chomp
@account = Account.new(@user_name, 1_000_000)
print "Enter PIN: "
@user_pin = gets.chomp.to_i
user_control
end
def user_control
control = [1, 2, 3]
puts "Welcome, #{@user_name} what would you like to do?"
puts "1) Check Balance"
puts "2) Withdraw"
puts "3) Deposit"
print "... "
# Another place where the variable seems... not Rubyish.
# user_control is better. Anything clear though.
# Abbreviations are for PHP and Perl. ;)
usrcntrl = gets.chomp.to_i
# Hint: You want to read about switch statements. No spoiler. ;)
if usrcntrl == control[0]
@account.display_balance(@user_pin)
elsif usrcntrl == control[1]
print "Enter amount to withdraw($): "
w_amount = gets.chomp.to_i
@account.withdraw(@user_pin, w_amount)
elsif usrcntrl == control[2]
print "Enter amount to deposit($): "
d_amount = gets.chomp.to_i
@account.deposit(@user_pin, d_amount)
else
"Error"
end
end
end
your_bank = Machine.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment