Skip to content

Instantly share code, notes, and snippets.

@dengsauve
Last active March 3, 2017 13:21
Show Gist options
  • Save dengsauve/4fc4df0b71fda295eb02a084753a04f6 to your computer and use it in GitHub Desktop.
Save dengsauve/4fc4df0b71fda295eb02a084753a04f6 to your computer and use it in GitHub Desktop.
Challenge #304 [Easy] Little Accountant
1000;Cash;
1020;Account Receivables;
1100;Lab Equipement;
1110;Office Supplies;
2000;Notes Payables;
2010;Account Payables;
2110;Utilities Payables;
3000;Common Stock;
4000;Commercial Revenue;
4090;Unearned Revenue;
5000;Direct Labor;
5100;Consultants;
5500;Misc Costs;
7140;Rent;
7160;Telephone;
9090;Dividends;
1000;JAN-16;100000;0;
3000;JAN-16;0;100000;
7140;JAN-16;36000;0;
1000;JAN-16;0;36000;
1100;FEB-16;80000;0;
1000;FEB-16;0;60000;
2000;FEB-16;0;20000;
1110;FEB-16;17600;0;
2010;FEB-16;0;17600;
1000;MAR-16;28500;0;
4000;MAR-16;0;28500;
2010;MAR-16;17600;0;
1000;MAR-16;0;17600;
5000;APR-16;19100;0;
1000;APR-16;0;19100;
1000;APR-16;32900;0;
1020;APR-16;21200;0;
4000;APR-16;0;54100;
1000;MAY-16;15300;0;
1020;MAY-16;0;15300;
1000;MAY-16;4000;0;
4090;MAY-16;0;4000;
1110;JUN-16;5200;0;
2010;JUN-16;0;5200;
5100;JUN-16;19100;0;
1000;JUN-16;0;19100;
4120;JUN-16;5000;0;
1000;JUN-16;0;5000;
7160;JUL-16;2470;0;
2010;JUL-16;0;2470;
5500;JUL-16;3470;0;
1000;JUL-16;0;3470;
$debug = false
# Creates Program Data References
calendar = {"JAN"=>0, "FEB"=>1, "MAR"=>2, "APR"=>3, "MAY"=>4, "JUN"=>5,
"JUL"=>6, "AUG"=>7, "SEP"=>8, "OCT"=>9, "NOV"=>10, "DEC"=>11}
chart, journal = {}, []
File.open('chart.txt').each { |line| chart[line.split(';')[0]]=line.split(';')[1] }
File.open('journal.txt').sort.each { |line| journal << line.chomp.split(';')}
# User Instructions + Input
puts "\nUser input has the following form:\nAAAA BBBB CCC-XX DDD-XX EEE\n
AAA starting account (* for 1st account),\nBBB ending account (* for last account),
CCC-YY first period (* for 1st period),\nDDD-YY last period (* for last period),
EEE is output format (values are TEXT or CSV).\n\n"
user_request = "* 2000 * FEB-16 text".split#gets.chomp.split
# Parsing User Data
start_account, end_account, start_date, end_date, output_format = user_request
report = []
journal.each do |entry|
if (start_account=='*' || entry[0][0..start_account.length-1].to_i >= start_account.to_i) \
and (end_account=='*' || entry[0][0..end_account.length-1].to_i <= end_account.to_i)
if (start_date=='*' || (calendar[entry[1][0..2]] >= calendar[start_date[0..2]] \
and entry[1][4..5].to_i >= start_date[4..5].to_i)) \
and (end_date=='*' || (calendar[entry[1][0..2]] <= calendar[end_date[0..2]] \
and entry[1][4..5].to_i <= end_date[4..5].to_i))
report << entry
end end end
account, debit, credit, total_debit, total_credit = report[0][0], 0, 0, 0, 0
journal.each { |entry| debit, credit = debit+entry[2].to_i, credit+entry[3].to_i }
if output_format.downcase == 'text'
# Prints Title Data
puts "Total Debit :#{debit} Total Credit :#{credit}"
puts "Balance from account #{report[0][0]} to #{report[-1][0]} from period #{start_date} to #{end_date}\n\nBalance:"
# Prints Header of Report - TEXT
puts "ACCOUNT".ljust(17)+"|DESCRIPTION |".ljust(17)+"DEBIT|".rjust(17)+"CREDIT|".rjust(17)+"BALANCE|".rjust(17)
puts "-"*86
# Prints Body of Report - TEXT
report.each do |record|
if record[0] != account
puts account.ljust(17)+"|#{chart[account]}".ljust(17)+"|"+"#{debit}|".rjust(17)\
+"#{credit}|".rjust(17)+"#{debit-credit}|".rjust(17)
account, debit, credit = record[0], 0, 0
end
debit, credit, total_debit, total_credit = debit+record[2].to_i, credit+record[3].to_i, total_debit+record[2].to_i, total_credit+record[3].to_i
end
puts account.ljust(17)+"|#{chart[account]}".ljust(17)+"|"+"#{debit}|".rjust(17)+"#{credit}|".rjust(17)+"#{debit-credit}|".rjust(17)
puts "TOTAL".ljust(17)+"|".ljust(17)+"|"+"#{total_debit}|".rjust(17)+"#{total_credit}|".rjust(17)+"#{total_debit-total_credit}|".rjust(17)
elsif output_format.downcase == 'csv'
# Prints Header of Report - CSV
puts 'ACCOUNT;DESCRIPTION;DEBIT;CREDIT;BALANCE;'
# Prints Body of Report - CSV
report.each do |record|
if record[0] != account
puts account+';'+chart[account]+';'+debit.to_s+';'+credit.to_s+';'+(debit-credit).to_s+';'
account, debit, credit = record[0], 0, 0
end
debit, credit, total_debit, total_credit = debit+record[2].to_i, credit+record[3].to_i, total_debit+record[2].to_i, total_credit+record[3].to_i
end
puts account+';'+chart[account]+';'+debit.to_s+';'+credit.to_s+';'+(debit-credit).to_s+';'
puts "TOTAL;;"+total_debit.to_s+';'+total_credit.to_s+';'+(total_debit-total_credit).to_s+';'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment