-
-
Save btn0s/11024717 to your computer and use it in GitHub Desktop.
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 |
UPDATE here's what I got to. havent done much googling cause im mad tired but i organized that stuff I guess, but now im stuck trying to get the Account class initialized from inside the instance of the Machine class... i was just messing around and got some different errors(?)
=> #<Machine:0x00000002a3b018 @user_name="1", @account=#<Account:0x00000001d67c8
8 @name="1", @balance=1000000, @pin=1234>, @user_pin=1>
I dunno what that is haha, but thats what i get and its obviously not working... so im leaving it where it is and im off to sleep. !!
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:
your_bank = Machine.new
your_bank.startup
# OR just
Machine.new.startup
# since you're not reusing that your_bank local variable [yet]
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!
the code at the bottom (outside of the class) is the stuff i want to get organized into its own classes and methods...