Advertisement
Guest User

Untitled

a guest
May 15th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Banking
  2.   puts "RubyVirtuaBanking."
  3.   attr_reader :name, :balance, :time_creation, :date
  4.   require 'date'
  5.   @@attemts_counter = 3
  6.   @@users = []
  7.   #@@user_counter = 0
  8.    
  9.   def initialize(name, balance, pin)
  10.     @time_creation = "#{Time.now.hour}:#{Time.now.min}:#{Time.now.sec}"
  11.     @date = Date.today
  12.     @name = name
  13.     @balance = balance
  14.     @pin = pin
  15.     @@users << self
  16.   end
  17.  
  18.   def account_info
  19.     puts "Balance info:"
  20.     puts "1. Account holder - #{@name.capitalize};"
  21.     puts "2. Account created - 1) date #{@date}; 2) time #{@time_creation};"
  22.     puts "3. Balance - $#{@balance}."
  23.   end
  24.  
  25.   def self.search_user
  26.    
  27. =begin
  28.   def self.display_users
  29.       puts @@users
  30.   end
  31. =end
  32.  
  33.   def withdraw(pin_provided, amount)
  34.     @withdraw_amount = amount
  35.     if pin_provided == pin
  36.       if @balance >= amount
  37.         @balance -= amount
  38.         puts "Amount to withdraw - $#{amount}. Current balance - $#{@balance}."
  39.       else
  40.         insuficient_funds
  41.       end
  42.     else
  43.       loop do
  44.     pin_error
  45.     @@attemts_counter -= 1
  46.     puts @@attempts_counter == 0 ? "No attempts left! Account blocked!" : "You have #{@@attempts_counter} more attempts left."
  47.     break if @@attempts_counter == 0
  48.     puts "Retry PIN: "
  49.     pin_retry = gets.chomp.to_i
  50.     self.withdraw(pin_retry, amount)
  51.       end
  52.     end
  53.     #redo unless pin_provided == pin
  54.   end
  55.  
  56.   def deposit_funds(pin_provided, amount)
  57.     if pin_provided == pin
  58.       @balance += amount
  59.       puts "You have added $#{amount} to your account. Current balance - $#{@balance}."
  60.     else
  61.       pin_error
  62.     end
  63.   end
  64.  
  65.   def display_balance
  66.     puts "Current balance - $#{@balance}"
  67.   end
  68.      
  69.   private
  70.  
  71.   def pin
  72.     @pin
  73.   end
  74.  
  75.   def pin_error
  76.     puts "Access denied: PIN provided is incorrect!"
  77.   end
  78.  
  79.   def insuficient_funds
  80.     puts "Insufficient funds. Your balance is $#{@balance}."
  81.     puts "Your withdrawal - $#{@withdraw_amount} - exceeds your balance by $#{@balance - @withdraw_amount}."
  82.   end
  83. end
  84.  
  85. acc_001 = Banking.new("andrius", 12548, "sareika84")
  86. acc_002 = Banking.new("jobikutelis", 100_000, "sausainis13")
  87.  
  88.  
  89. loop do
  90. =begin
  91. print "Enter account name: "
  92.         acc_name = gets.chomp.to_s.downcase
  93.     print "Enter PIN to match the account #{acc_name.upcase}: "
  94.         pin_input = gets.chomp
  95.     print "\n"
  96. =end
  97.     print "Enter PIN to match the account ACC_001: ", "\n"
  98.     pin_input = gets.chomp
  99.     puts "Take action:"
  100.     puts "S - show account details;"
  101.     puts "B - display balance;"
  102.     puts "W - withdraw funds;"
  103.     puts "D - deposit funds;"
  104.     puts "Q - quit the VirtuaBanking", "\n"
  105.     print "Action - "
  106.         action = gets.chomp.upcase
  107.  
  108. case action
  109.  
  110. when "S"
  111.     acc_001.account_info
  112. when "B"
  113.     acc_001.display_balance
  114. when "W"
  115.     print "Input amount to withdraw: "
  116.         amount_withdraw = gets.chomp.to_i
  117.     acc_001.withdraw(pin_input, amount_withdraw)
  118. when "D"
  119.     print "Input amount to deposit: "
  120.         amount_deposit = gets.chomp.to_i
  121.     acc_001.deposit_funds(pin_input, amount_deposit)
  122. when "Q"
  123.     puts "Thank you for using RubyVirtuaBanking"
  124.     break
  125. else
  126.     print "No such action available. Please try again!"
  127.     #retry if action == ["S", "B", "W", "D", "Q"]
  128. end
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement