Created
December 5, 2014 20:44
-
-
Save adnandossaji/9ed51635b41cef1fa505 to your computer and use it in GitHub Desktop.
creditCard
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/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) |
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
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 |
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
empty |
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
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