-
-
Save markserver/280e1bf00e5f223de535 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'highline/import' | |
require 'colorize' | |
require 'mysql2' | |
class User | |
def initialize | |
@db = Mysql2::Client.new( | |
:host => '', | |
:username => '', | |
:password => '', | |
:database => '' | |
) | |
end | |
def login(username, password) | |
result = @db.query("SELECT UserID, CONCAT_WS(' ', FirstName, LastName) AS Name, Password, SHA2('#{password}', 256) AS InputPassword FROM User WHERE Username = '#{username}' LIMIT 1") | |
@user_id = result.first['UserID'] | |
@name = result.first['Name'] | |
result.first['InputPassword'] == result.first['Password'] | |
end | |
def get_accounts | |
@db.query("SELECT AccountID FROM Account WHERE UserID = #{@user_id}") | |
end | |
end | |
class Account | |
def initialize(account_id) | |
@account_id = account_id | |
@db = Mysql2::Client.new( | |
:host => '', | |
:username => '', | |
:password => '', | |
:database => '' | |
) | |
end | |
def statement | |
end | |
def deposit(amount) | |
@db.query("CALL Deposit(#{@account_id}, #{amount})") | |
true | |
rescue Mysql2::Error | |
false | |
end | |
def withdrawal(amount) | |
if get_balance >= amount | |
@db.query("CALL Withdrawal(#{@account_id}, #{amount})") | |
true | |
end | |
rescue Mysql2::Error | |
false | |
end | |
def transfer(recipient_id, amount) | |
if get_balance >= amount | |
@db.query("CALL Transfer(#{@account_id}, #{recipient_id}, #{amount})").inspect | |
true | |
end | |
rescue Mysql2::Error | |
false | |
end | |
def get_balance(account_id = @account_id) | |
result = @db.query("SELECT Balance FROM Balance WHERE AccountID = #{account_id} ORDER BY Date DESC LIMIT 1") | |
result.first['Balance'] | |
end | |
def get_user(account_id) | |
result = @db.query("SELECT CONCAT(FirstName, ' ', LastName) AS Name FROM Account LEFT OUTER JOIN User ON Account.UserID = User.UserID WHERE AccountID = #{account_id}") | |
unless result.count == 0 | |
result.first['Name'] | |
end | |
end | |
end | |
system('cls') | |
puts ('Benvenuto in MBI - Marco Bank Interface').colorize(:mode => :bold, :color => :blue) | |
username = ask ('Inserisci il tuo nome utente: ').colorize(:yellow) | |
password = ask ("<%= color('Inserisci la tua password: ', :yellow) %>") { |q| q.echo = '*' } | |
user = User.new | |
unless user.login(username, password) | |
abort('Autenticazione non riuscita, i dati inseriti sono errati') | |
end | |
puts 'Scegli il numero di conto da utilizzare:'.colorize(:background => :blue) | |
choose do |menu| | |
menu.index = :letter | |
menu.index_suffix = ") " | |
menu.prompt = 'Conto: ' | |
user.get_accounts.each do |account| | |
menu.choice :"Conto N. #{account['AccountID']}" do | |
@account = Account.new(account['AccountID']) | |
end | |
end | |
end | |
loop do | |
puts 'Scegli l\'operazione da effettuare:'.colorize(:background => :blue) | |
choose do |menu| | |
menu.prompt = 'Operazione: ' | |
menu.choice :'Estratto Conto' do | |
puts "Estratto Conto per " | |
end | |
menu.choice :Deposito do | |
amount = ask("<%= color('Indica la somma da versare sul conto: ', :yellow) %> ", Integer) | |
if agree("<%= color('Sei sicuro di voler effettuare questa operazione?, :yellow) %> ", true) | |
@account.deposit(amount) | |
end | |
end | |
menu.choice :Ritiro do | |
amount = ask("<%= color('Indica la somma da ritirare dal conto: ', :yellow) %> ", Integer) | |
if agree("<%= color('Sei sicuro di voler effettuare questa operazione?, :yellow) %> ", true) | |
if @account.withdrawal(amount) | |
puts 'Operazione effettuata con successo'.colorize(:green) | |
else | |
puts 'Operazione fallita, saldo insufficiente'.colorize(:red) | |
end | |
end | |
end | |
menu.choice :Bonifico do | |
recipient_id = ask("<%= color('Indica il numero di conto che deve ricevere il bonifico: ', :yellow) %> ", Integer) | |
counterpart = @account.get_user(recipient_id) | |
if counterpart | |
print 'Controparte trovata: ' | |
puts counterpart.colorize(:cyan) | |
else | |
puts 'Operazione fallita, conto della controparte sconosciuto'.colorize(:red) | |
next | |
end | |
amount = ask("<%= color('Indica la somma da bonificare: ', :yellow) %> ", Integer) | |
if agree('Sei sicuro di voler effettuare questa operazione? ', true).colorize(:yellow) | |
if @account.transfer(recipient_id, amount) | |
puts 'Operazione effettuata con successo'.colorize(:green) | |
else | |
puts 'Operazione fallita, saldo insufficiente'.colorize(:red) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment