Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Banking
- puts "RubyVirtuaBanking."
- attr_reader :name, :balance, :time_creation, :date
- require 'date'
- @@attemts_counter = 3
- @@users = []
- #@@user_counter = 0
- def initialize(name, balance, pin)
- @time_creation = "#{Time.now.hour}:#{Time.now.min}:#{Time.now.sec}"
- @date = Date.today
- @name = name
- @balance = balance
- @pin = pin
- @@users << self
- end
- def account_info
- puts "Balance info:"
- puts "1. Account holder - #{@name.capitalize};"
- puts "2. Account created - 1) date #{@date}; 2) time #{@time_creation};"
- puts "3. Balance - $#{@balance}."
- end
- def self.search_user
- =begin
- def self.display_users
- puts @@users
- end
- =end
- def withdraw(pin_provided, amount)
- @withdraw_amount = amount
- if pin_provided == pin
- if @balance >= amount
- @balance -= amount
- puts "Amount to withdraw - $#{amount}. Current balance - $#{@balance}."
- else
- insuficient_funds
- end
- else
- loop do
- pin_error
- @@attemts_counter -= 1
- puts @@attempts_counter == 0 ? "No attempts left! Account blocked!" : "You have #{@@attempts_counter} more attempts left."
- break if @@attempts_counter == 0
- puts "Retry PIN: "
- pin_retry = gets.chomp.to_i
- self.withdraw(pin_retry, amount)
- end
- end
- #redo unless pin_provided == pin
- end
- def deposit_funds(pin_provided, amount)
- if pin_provided == pin
- @balance += amount
- puts "You have added $#{amount} to your account. Current balance - $#{@balance}."
- else
- pin_error
- end
- end
- def display_balance
- puts "Current balance - $#{@balance}"
- end
- private
- def pin
- @pin
- end
- def pin_error
- puts "Access denied: PIN provided is incorrect!"
- end
- def insuficient_funds
- puts "Insufficient funds. Your balance is $#{@balance}."
- puts "Your withdrawal - $#{@withdraw_amount} - exceeds your balance by $#{@balance - @withdraw_amount}."
- end
- end
- acc_001 = Banking.new("andrius", 12548, "sareika84")
- acc_002 = Banking.new("jobikutelis", 100_000, "sausainis13")
- loop do
- =begin
- print "Enter account name: "
- acc_name = gets.chomp.to_s.downcase
- print "Enter PIN to match the account #{acc_name.upcase}: "
- pin_input = gets.chomp
- print "\n"
- =end
- print "Enter PIN to match the account ACC_001: ", "\n"
- pin_input = gets.chomp
- puts "Take action:"
- puts "S - show account details;"
- puts "B - display balance;"
- puts "W - withdraw funds;"
- puts "D - deposit funds;"
- puts "Q - quit the VirtuaBanking", "\n"
- print "Action - "
- action = gets.chomp.upcase
- case action
- when "S"
- acc_001.account_info
- when "B"
- acc_001.display_balance
- when "W"
- print "Input amount to withdraw: "
- amount_withdraw = gets.chomp.to_i
- acc_001.withdraw(pin_input, amount_withdraw)
- when "D"
- print "Input amount to deposit: "
- amount_deposit = gets.chomp.to_i
- acc_001.deposit_funds(pin_input, amount_deposit)
- when "Q"
- puts "Thank you for using RubyVirtuaBanking"
- break
- else
- print "No such action available. Please try again!"
- #retry if action == ["S", "B", "W", "D", "Q"]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement