Skip to content

Instantly share code, notes, and snippets.

@rozzzly
Created December 5, 2014 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rozzzly/aff35a0cb25c512068f8 to your computer and use it in GitHub Desktop.
Save rozzzly/aff35a0cb25c512068f8 to your computer and use it in GitHub Desktop.
adnan herlp
#!/usr/bin/ruby
load 'creditCard.rb'
require 'sqlite3'
begin
db = SQLite3::Database.new "test.db" # ! automaticly creates new db, overwrites anything you have currently --> info in db will just be your last action
db = SQLite3::Database.open "test.db"
db.execute "CREATE TABLE IF NOT EXISTS People(Id INTEGER PRIMARY KEY, # ! fuck the 'Id', SQLite has and auto index, dont have to worry about autoinc, or nuffin
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 #ARGF.read is a function @see http://apidock.com/ruby/ARGF/read use
people = {}
def run(file, people)
rand = Random.rand(9999)
file.each_line do |line, index| # should work okay if stdin (having the each_line is redundant becausse there will only ever be one line), but with file, no file handle is opened so its going to try to make args out of supplied filename gotta fix that
line = line.split(' ')
function = line[0] # hold up! check to make sure it even has arguments
if function == 'Add'
name, cc, limit = line[1..-1]
limit = limit[1..-1].to_i
people[name] = Credit.new(name, cc, limit) # wait fist check, to make sure user doesnt already exist
db.execute "INSERT INTO People VALUES(1,name,cc,limit,0)" # remove first column -- rightnow it will always try to creat a row at rowid == 1. you want more records possily update cc,limit for name if exists.
elsif function == 'Charge'
name, amount = line[1..-1]
amount = amount[1..-1].to_i
if people[name].nil? # load list of people
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment