Advertisement
Guest User

Untitled

a guest
May 26th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.62 KB | None | 0 0
  1. require 'yaml'
  2.  
  3. $config = YAML.load_file(ARGV.first || 'config.yml')
  4.  
  5. class Authorize
  6.  
  7.   ACCOUNTS = $config ['accounts']
  8.  
  9.   attr_reader :pin, :password
  10.   attr_accessor :user_data
  11.   def initialize
  12.     @pin = input_pin
  13.     @password = input_password
  14.     @user_data = nil
  15.  
  16.     check_auth!
  17.   end
  18.  
  19.   private
  20.  
  21.   def input_pin
  22.     print 'Please Enter Your Account Number: '
  23.     pin = gets.to_i
  24.   end
  25.  
  26.   def input_password
  27.     print 'Enter Your Password: '
  28.     password = gets.chomp
  29.   end
  30.  
  31.   def check_auth!
  32.     if ACCOUNTS.has_key?(pin) && password == ACCOUNTS[pin]['password']
  33.       @user_data = { :name => ACCOUNTS[pin]['name'], :balance => ACCOUNTS[pin]['balance'] }
  34.       puts 'Cheking info....'
  35.       sleep 0.3
  36.     else
  37.      raise InvalidLoginInput, 'invalid login or password'
  38.     end
  39.   end
  40.  
  41. end
  42.  
  43. class Atm
  44.  
  45.   BANKNOTES = $config['banknotes']
  46.   MAX_WITHDRAW = 90_000
  47.  
  48.   attr_reader :name
  49.   attr_accessor :balance, :valid, :stash, :user_transaction
  50.   def initialize name = '', balance = 0, valid = false, stash = BANKNOTES, user_transaction = {}
  51.     @name = name
  52.     @balance = balance
  53.     @valid = valid
  54.     @stash = chop_zero(stash)
  55.     @user_transaction = user_transaction
  56.  
  57.     validate_atm!
  58.   end
  59.  
  60.   def menu
  61.     [
  62.       "Please Choose From the Following Options:",
  63.       "1. Display Balance",
  64.       "2. Withdraw",
  65.       "3. Log Out",
  66.       "Enter chiose: "
  67.     ]
  68.   end  
  69.  
  70.   def choise
  71.     loop do
  72.       choise = gets.to_i
  73.       case choise
  74.         when 1 then display_balance
  75.         when 2 then withdraw
  76.         when 3 then logout
  77.         else
  78.           print "Invalid command!!!\nEnter chiose: "
  79.           choise
  80.       end
  81.       break if choise == 3
  82.     end    
  83.   end
  84.  
  85.  
  86.   private
  87.  
  88.   def notes_sum hsh
  89.     hsh.inject(0) { |sum, (note, qty)| sum += note * qty }
  90.   end
  91.  
  92.   def chop_zero hsh
  93.     hsh.select!{ |note, qty| qty > 0 }
  94.   end
  95.  
  96.   def validate_atm!
  97.     Authorize.new.user_data.each { |k,v| instance_variable_set("@#{k}", v) }
  98.     @valid = true
  99.     puts "Hello,#{name}!"
  100.     menu.each { |s| puts s }
  101.     choise
  102.   end
  103.  
  104.   def display_balance
  105.     puts "Your Current Balance is ₴#{@balance}"
  106.     choise
  107.   end
  108.  
  109.   def withdraw
  110.     print 'Enter Amount You Wish to Withdraw: '
  111.     amount = gets.to_i
  112.     check_withdraw(amount)
  113.   end  
  114.  
  115.   def check_withdraw amount
  116.    
  117.     if amount > MAX_WITHDRAW
  118.       puts "ERROR: INSUFFICIENT FUNDS!! PLEASE ENTER A DIFFERENT AMOUNT:"
  119.       withdraw
  120.     elsif amount > notes_sum(stash)
  121.       puts "ERROR: THE MAXIMUM AMOUNT AVAILABLE IN THIS ATM IS ₴#{notes_sum(stash)}"
  122.       withdraw
  123.     else
  124.       process_atm(amount, user_transaction)  
  125.     end
  126.  
  127.   end
  128.  
  129.   def process_atm amount, user_transaction
  130.     stash.each do |note, qty|
  131.      
  132.       if amount >= note * qty
  133.         user_transaction.store(note, qty)
  134.         amount -= (note * qty)
  135.       else
  136.         user_transaction.store(note, amount.divmod(note).first)
  137.         amount -= note * amount.divmod(note).first
  138.       end
  139.          
  140.     end
  141.  
  142.       if amount != 0
  143.         puts "ERROR: THE AMOUNT YOU REQUESTED CANNOT BE COMPOSED FROM BILLS AVAILABLE IN THIS ATM. PLEASE ENTER A DIFFERENT AMOUNT: "
  144.         withdraw
  145.       else  
  146.         puts "succes withdraw #{chop_zero(user_transaction).inspect}"
  147.         puts "stash before #{stash.inspect}"
  148.         @stash.merge!(user_transaction) {|note, stash_qty, withdrawn| stash_qty - withdrawn }
  149.         puts "stash updated #{stash.inspect}"
  150.       end  
  151.  
  152.   end
  153.  
  154.   def logout
  155.     puts "Logged out"
  156.     atm = Atm.new
  157.   end
  158.  
  159. end
  160.  
  161.  
  162.  
  163.  
  164. class InvalidLoginInput < StandardError; end
  165.  
  166.  
  167.  
  168.  
  169. atm = Atm.new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement