Last active
August 29, 2015 14:00
-
-
Save btn0s/11024717 to your computer and use it in GitHub Desktop.
this is a little bank project i got off ofa website im learning Ruby on. I have got the basic layout for seeing balance, depositing, withdrawing, etc... but there are a couple things i want to do. 1) maybe add more classes for the machine itself, a user that can have multiple accounts in his name 2) a "login/create user" startup in that machine …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account | |
attr_reader :name, :balance | |
def initialize(name, balance=100) | |
@name = name | |
@balance = balance | |
end | |
private | |
def pin | |
@pin = 1234 | |
end | |
def pin_error | |
"Invalid Pin" | |
end | |
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 "..." | |
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 "... " | |
usrcntrl = gets.chomp.to_i | |
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 |
i forked yr gist btw with a few notes. more style things than anything. no spoilers but a hint about a new kind of statement you might not know but would like. if you look on the details of yr gist, you can see there's a fork. click on that and it will bring you to a list of forks, mines the only one i'm sure. just showing you how to use that vs linking directly to my gist.
and now it's coffee time.
Thank you! Sweet so basically its doing exactly what I wanted? now i need to add some loops to keep it running until the user exits, and maybe a "press 1 for English" to initialize the whole thing... thank you though and thanks for the style notes!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that's not an error. that's just what instances look like. i can see there that you successfully assigned an Account instance to @account inside the machine instance and the individual instance variables within it.
usually you wouldn't want to use the #startup method inside the #initialize method. the common way of doing what you're trying is: