Skip to content

Instantly share code, notes, and snippets.

@rozzzly
Forked from adnandossaji/basicCC.rb
Last active August 29, 2015 14:10
Show Gist options
  • Save rozzzly/17d7a5a535ce80d0eb39 to your computer and use it in GitHub Desktop.
Save rozzzly/17d7a5a535ce80d0eb39 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
load 'creditCard.rb'
require 'sqlite3'
begin
db = SQLite3::Database.new "test.db"
db = SQLite3::Database.open "test.db"
db.execute "CREATE TABLE IF NOT EXISTS People(Id INTEGER PRIMARY KEY,
Name TEXT, Credit TEXT, Limit INT, Price INT)"
rescue SQLite3::Exception => e
puts "Exception occurred"
puts e
ensure
db.close if db
end
data = ARGF.read
people = {}
def run(file, people)
rand = Random.rand(9999)
file.each_line do |line, index|
line = line.split(' ')
function = line[0]
if function == 'Add'
name, cc, limit = line[1..-1]
limit = limit[1..-1].to_i
people[name] = Credit.new(name, cc, limit)
db.execute "INSERT INTO People VALUES(1,name,cc,limit,0)"
elsif function == 'Charge'
name, amount = line[1..-1]
amount = amount[1..-1].to_i
if people[name].nil?
puts "Could not find #{name}"
else
people[name].charge(amount)
end
elsif function == 'Credit'
name, amount = line[1..-1]
amount = amount[1..-1].to_i
if people[name].nil?
puts "Could not find #{name}"
else
people[name].credit(amount)
end
end
puts people
end
end
run(data, people)
####### to run file.txt #######
#
# sudo ruby ./basicCC.rb < file.txt
#
# file.txt
#
# Add Tom 4111111111111111 $1000
#
# Add Lisa 5454545454545454 $3000
#
# Add Quincy 1234567890123456 $2000
#
# Charge Tom $500
#
# Charge Tom $800
#
# Charge Lisa $7
#
# Credit Lisa $100
#
# Credit Quincy $200
#
####### to run STDIN #######
#
# echo "Add Tom 4111111111111111 $1000" | ruby basicCC.rb
# echo "Add Lisa 5454545454545454 $3000" | ruby basicCC.rb
# echo "Add Quincy 1234567890123456 $2000" | ruby basicCC.rb
# echo "Charge Tom $500" | ruby basicCC.rb
# echo "Charge Tom $800" | ruby basicCC.rb
# echo "Charge Lisa $7" | ruby basicCC.rb
# echo "Credit Lisa $100" | ruby basicCC.rb
# echo "Credit Quincy $200" | ruby basicCC.rb
#
####### to run in shell #######
#
# tom = Credit.new("Tom", "4111111111111111", 1000)
# lisa = Credit.new("Lisa", "5454545454545454", 3000)
# quincy = Credit.new("Quincy", "1234567890123456", 2000)
# tom.charge(500)
# tom.charge(800)
# lisa.charge(7)
# lisa.credit(100)
# quincy.credit(200)
class Credit
# a program that will add new credit card accounts, process
# charges and credits against them, and display summary information
attr_reader :name, :limit, :luhn_check, :balance
def initialize(name, cc, limit = 0, balance = 0)
if cc.length > 19
puts '#{name}: Credit card length out of bounds'
end
@name = name
@limit = limit
@luhn_check = luhn_check(cc)
@balance = balance
puts "#{name}'s account has been created!"
end
def charge(amount)
if @luhn_check == false
puts "#{@name}: error"
elsif amount + @balance > @limit
puts 'declined'
else
@balance += amount
puts "#{@name} $#{@balance}"
end
end
def credit(amount)
if @luhn_check == false
puts "#{@name}: error"
else
@balance -= amount
puts "#{@name} $#{@balance}"
end
end
def balance()
puts "#{@name} $#{@balance}"
end
def luhn_check(cc)
digits = cc.scan(/./).map(&:to_i) # creates an array of ints
check = digits.pop # pops off the last index
sum = digits.reverse.each_slice(2).map do |num, index| # reversed array and then executes methods
[(num * 2).divmod(10), index || 0] # multiplies every other num by 2
end.flatten.inject(:+) # puts into sum
(10 - sum % 10) == check # if last digit = 10 - sum % 10 == true
end
end
Add Tom 4111111111111111 $1000
Add Lisa 5454545454545454 $3000
Add Quincy 1234567890123456 $2000
Charge Tom $500
Charge Tom $800
Charge Lisa $7
Credit Lisa $100
Credit Quincy $200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment