I have spent a few days coding an ATM. What I'm looking for is some critique of my work. Is there anything I can do better here?
The entire project has 9 files and one class, a checking account and savings accounts. It also has a section for loans (thought it was different). Any advice, or input would be greatly appreciated.
To copy this from github the link is: https://github.com/13aal/ATM/tree/master
To make it run use run.rb
main.rb source:
#!/usr/bin/env ruby
################
#ATM Rewrite
#
#Creator Lost Bam
#
#11/19/15
##################
require_relative 'checking.rb'
require_relative 'savings.rb'
require_relative 'transfer.rb'
require_relative 'loan.rb'
require_relative './ready/exit.rb'
require_relative './ready/redirect.rb'
class ATM
attr_accessor :name, :checking_account, :savings_account, :pin_number, :transfer, :loan
def initialize( name, checking_account, savings_account, pin_number, credit_score )
@name = name
@checking_account = checking_account
@savings_account = savings_account
@pin_number = pin_number
@credit_score = credit_score
end
def pin
x = 3
while (x > 0) do
puts "Enter PIN(#{x} attempts left):"
pin_num = gets.chomp
case pin_num.to_i
when @pin_number
menu
else
puts "Invalid PIN"
x -= 1
bad_pin if x == 0
end
end
end
def menu
puts <<-END.gsub(/^\s*>/, ' ')
>
>Welcome #{name} thank you for choosing Bank of Bam.
>You may choose from the list below of what you would like to do
>For checking inquiries press '1'
>For savings account information press '2'
>To transfer funds press '3'
>To either apply for a loan, or get information on a loan press '4'
>To exit the system press '5'
>
END
input = gets.chomp
case input.to_i
when 1
checking_information
when 2
savings_information
when 3
transfer_funds
when 4
loan_info
when 5
exit_screen
else
puts "Error Invalid option please try again"
menu
end
end
def bad_pin
abort('Exiting system, all attempts failed...')
exit
end
end
checking.rb source:
private
def checking_information
puts <<-END.gsub(/^\s*>/, ' ')
>
>Your current balance is: #{@checking_account}
>Deposit funds '1'
>Make a withdrawl '2'
>Transfer funds '3'
>Go back '4'
>
END
input = gets.chomp
case input.to_i
when 1
deposit_checking
when 2
withdrawl_checking
when 3
transfer_funds
when 4
redirect
else
puts "Invalid option"
checking_information
end
end
def deposit_checking
puts "Enter amount to deposit:"
amount = gets.chomp.to_i
@checking_account += amount.to_f
puts "Your new balance is #{@checking_account}"
puts "Would you like make another deposit?"
input = gets.chomp
if input == /yes/i
deposit
else
redirect
end
end
def withdrawl_checking
puts "Your current balance is #{@checking_account}. Please enter amount to withdrawl:"
input = gets.chomp.to_i
@checking_account -= input.to_f
puts "Your new balance is #{@checking_account}"
puts "Would you like to make another withdrawl?"
if input =~ /yes/i
withdrawl_checking
else
redirect
end
end
savings.rb source:
private
def savings_information
puts <<-END1.gsub(/^\s*>/,' ')
>
>Your current savings balance is #{@savings_account}
>Deposit funds '1'
>Make a Withdrawl '2'
>Transfer funds '3'
>Go back '4'
>
END1
input = gets.chomp
case input.to_i
when 1
deposit_savings
when 2
withdraw_savings
when 3
transfer_funds
when 4
redirect
else
puts "Invalid option"
savings_information
end
end
def deposit_savings
puts "Your current balance is #{@savings_account}, how much would you like to deposit?"
input = gets.chomp.to_i
@savings_account += input
puts "Your new balance is #{@savings_account}, would you like to make another deposit?"
anthr_deposit = gets.chomp
if anthr_deposit =~ /yes/i
deposit_savings
elsif anthr_deposit =~ /no/i
redirect
else
puts "Invalid option, exiting."
exit_screen
end
end
def withdraw_savings
puts "You current balance is #{@savings_account}, how much would you like to withdraw?"
input = gets.chomp.to_i
@savings_account -= input
puts "Your new balance is #{@savings_account}, would you like to make another withdrawl?"
anthr_withdrawl = gets.chomp
if anthr_withdrawl =~ /yes/i
withdraw_savings
elsif anthr_withdraw =~ /no/i
redirect
else
puts "Invalid option, exiting."
exit_screen
end
end
transfer.rb source:
private
require_relative 'error.rb'
def transfer_funds
puts <<-START.gsub(/^\s*>/, ' ')
>
>Transfer from Savings to Checking '1'
>Transfer from Checking to Savings '2'
>
START
input = gets.chomp
case input.to_i
when 1
puts <<-START1.gsub(/^\s*>/, ' ')
>
>Your current balance in your Savings account is: #{@savings_account}
>Your current balance in your Checking account is: #{@checking_account}
>How much would you like to transfer from your Savings to your Checking?
>
START1
transfer = gets.chomp.to_i if @savings_account > 0
if transfer > @savings_account
error
else
@savings_account -= "#{transfer}".to_f
@checking_account += "#{transfer}".to_f
puts <<-START2.gsub(/^\s*>/, ' ')
>
>Your new balance for your Savings account is #{@savings_account}
>Your new balance for your Checking account is: #{@checking_account}
>Would you like to make another transfer?
>
START2
anthr_transfer = gets.chomp
if anthr_transfer =~ /yes/i
transfer_funds
elsif anthr_transfer =~ /no/i
puts "System will now exit for security"
exit_screen
else
puts "Invalid options"
redirect
end
end
when 2
puts <<-START3.gsub(/^\s*>/, ' ')
>
>Your current balance in your Checking account is: #{@checking_account}
>Your current balance in your Savings account is: #{@savings_account}
>How much would you like to transfer from your Checking to your Savings?
>
START3
transfer = gets.chomp.to_i
if transfer > @checking_account
error
else
@checking_account -= "#{transfer}".to_f
@savings_account += "#{transfer}".to_f
puts <<-START4.gsub(/^\s*>/, ' ')
>
>Your new balance for your Checking account is #{@checking_account}
>Your new balance for your Savings account is: #{@savings_account}
>Would you like to make another transfer?
>
START4
anthr_transfer = gets.chomp
if anthr_transfer =~ /yes/i
transfer_funds
elsif anthr_transfer =~ /no/i
puts "System will now exit for security"
exit_screen
else
puts "Invalid options"
redirect
end
end
end
end
loan.rb source:
private
def loan_info
puts <<-END.gsub(/^\s*>/, ' ')
>
>Hello and welcome to the credit station
>Please choose from the list below
>Would you like to Apply for a loan '1'
>Check credit score '2'
>Go back '3'
>
END
input = gets.chomp
case input.to_i
when 1
apply_credit
when 2
check_score
else
redirect
end
end
def apply_credit
if @credit_score >= 640
accepted
else
denied_loan
end
end
def accepted
loan = %w(100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000 3100 3200 3300 3400 3500 3600 3700 3800 3900 4000 4100 4200 4300 4400 4500 4600 4700 4800 4900 5000 5100 5200 5300 5400 5500 5600 5700 5800 5900 6000 6100 6200 6300 6400 6500 6600 6700 6800 6900 7000 7100 7200 7300 7400 7500 7600 7700 7800 7900 8000 8100 8200 8300 8400 8500 8600 8700 8800 8900 9000 9100 9200 9300 9400 9500 9600 9700 9800 9900 10000)
loan_amount = loan.sample
puts "You have been accepted for a #{loan_amount} loan which will be added to your bank account"
puts <<-END.gsub(/^\s*>/, ' ')
>
>Which account would you like to add that to?
>Checking Account '1'
>Savings Account '2'
>
END
input = gets.chomp
case input.to_i
when 1
@checking_account += loan_amount.to_f
puts "#{loan_amount} has been added to your checking account, your new balance is #{@checking_account}"
puts "Your card will now be returned for security purposes."
exit_screen
when 2
@savings_account += loan_amount.to_f
puts "#{loan_amount} has been added to your savings account, your new balance is #{@savings_account}"
puts "Your card will now be returned for security purposes"
exit_screen
end
end
def check_score
puts "In order to check your score $5.99 will be charged to your account. Do you accept the charges?"
input = gets.chomp
if input =~ /yes/i
@checking_account -= 5.99
puts "Your credit score is #{@credit_score}. Would you like to apply for a loan?"
apply = gets.chomp
if apply =~ /yes/i
apply_credit
else
return
end
else
puts "You will be taken back to the main menu."
menu
end
end
def denied_loan
puts <<-REASON.gsub(/^\s*>/, ' ')
>
>We regret to inform you, but you have been denied
>for the loan. This is do to either you owing money, or
>credit score being to low. In order to be accepted you
>must have a credit score of 640 or higher. Your credit
>score came back to us as #{@credit_score}. We
>apologize for any inconvienence this may have caused
>you. If you feel this was denied in error, please contact
>your bank.
>
>Your card will now be returned to you..
>
REASON
exit_screen
end
exit.rb source:
def exit_screen
puts "Returning card.."
puts "Thank you for choosing Bank of Bam, have a nice day!"
exit
end
redirect.rb source:
def redirect
puts <<-END.gsub(/^\s*>/, ' ')
>
>Please choose from the list below
>Main menu '1'
>Checking inquiries '2'
>Savings account information '3'
>Transfer funds '4'
>Apply or view a loan '5'
>Return card and exit system '6'
>
END
input = gets.chomp
case input.to_i
when 1
menu
when 2
checking_information
when 3
savings_information
when 4
transfer_funds
when 5
loan_info
when 6
exit_screen
else
puts "Invalid option"
redirect
end
end
error.rb source:
def error
puts <<-EOT.gsub(/^\s*>/, ' ')
>
>Cannot complete transaction.
>Either your account is overdrawn, you
>do not have access to this section, or
>you cannot transfer that amount. If you
>feel you have reached this in error please
>contact your bank.
>
>Error #28-971-9087 has been logged#<-Random error I made up
>
>Exiting system for security
>
EOT
exit_screen
end
Run using run.rb source of run.rb:
require_relative 'main.rb' #<=Always require in this file
my_acc = ATM.new("Thomas Park", 5000, 10000, 7856, 640)
my_acc.pin #Checking account
#savings account
#PIN
#credit score