Programming challenge

new topic     » topic index » view thread      » older message » newer message

Following is a Euphoria translation of the first program I ever
got paid to write. (Actually, this is a slightly simplified version -
but more about that later.)
Perhaps it would make a nice tutorial. Any volunteers to write
the vars/functions/procedures needed to make it work?

Regards,
Irv

-- PAY.EX - a payroll program written in Euphoria
-- version 1.4

-- fixed stuff the client wants to see:

constant payrollfile = "PAYROLL.DAT", -- logs checks written
         errorfile   = "PAYROLL.ERR", -- logs errors

         minhrs = 0, -- set some reasonable limits
         maxhrs = 80, -- no more than 80 hours per pay period
         minwage = 5.25, -- minimum hourly wage
         maxwage = 35.00, -- maximum hourly we are willing to pay

 taxtable =            -- a lookup table for taxrates based on gross pay:
  {-- FROM, THRU, RATE (%)
    {{   0,  100}, 10},  -- for gross greater than $0 and <= $100, tax is 10%
    {{ 100,  200}, 20},  -- $100.01 thru $200 is taxed at 20%
    {{ 200,  400}, 30},  -- $200.01 thru $400 is taxed at 30%
    {{ 400, 9999}, 40},  -- gross of more than $400 is taxed at 40%
   }

 employeetable =       -- a alookup table for employee data:
  {-- NICKNAME,  FULLNAME
    {"Juan",    "Juan A. Hernandez"},
    {"Sue",     "Susan J. Wilson"},
    {"SamA",    "Samuel N. Arthur"},
    {"Joe",     "Joseph A. Nelson"},
    {"Lee",     "Lee Wu"},
    {"SamB",    "Samantha Burns"}
   }

--
-- "include" your variables, functions and procedures here
--

-- We'll get input from the command line, because this program
-- must be able to write checks one at a time "on demand".
-- We don't want to preclude the possibility of feeding it
-- a long list of employees, so we should avoid any fancy
-- user-interface routines for now.

cmd = command_line()
if length(cmd) = 5 then    -- valid number of arguments supplied
   data_is_valid = TRUE

else                       -- invalid number of arguments supplied
   notify("Syntax is:",
          "ex pay employee-nickname hours-worked pay-rate")
   abort(1)                -- no point in going any further
end if

-- The notify() function can be as simple or as fancy as
-- you want, as long as it is short and re-usable.

emp[nick] = cmd[3]    -- get nickname, hours, payrate from command line
emp[name] = lookup(emp[nick],employeetable)   -- lookup in employee file
emp[hours] = validate(cmd[4],{minhrs,maxhrs})   -- and check for
emp[payrate] = validate(cmd[5],{minwage,maxwage}) -- reasonable values.

if data_is_valid then
   emp[gross] = emp[hours] * emp[payrate]     -- compute gross pay
   taxrate = lookup(emp[gross],taxtable)       -- get tax rate from table
   emp[tax] = emp[gross] * taxrate              -- compute tax
   emp[net] = emp[gross] - emp[tax]              -- compute net pay

   if emp[net] > 1.00 then -- we don't bother writing checks for < $1.00
      PrintCheck(emp)       -- write the check
      log(payrollfile,emp)   -- log the transaction
   end if

else                  -- there is an error in the input data
   log(errorfile,emp)  -- log the error and quit with message
   notify("Error",
          "Unreasonable hours or payrate entered - see file "& errorfile)
   abort(1)
end if

-- notes:
---------------------------------------------------------------------------
-- FORMATS
---------------------------------------------------------------------------
-- for now, the CHECK can simply be two lines to the screen:
-- Name                 Hours   Payrate     Gross      Tax      Net
-- Juan A. Hernandez     40.0     12.00    480.00   120.00   360.00
---------------------------------------------------------------------------
-- THE LOGFILE FORMATS

-- for the PAYROLL file:
-- 1998/12/28 14:10 Juan   40.0     12.00       480.00   120.00    360.00

-- for the ERROR file:
-- 1998/12/28 14:10 Juan   40.0    333.00
---------------------------------------------------------------------------
-- Big extra credits if the PAYERR.DAT entry indicates the cause of the
-- problem, i.e.:

-- 1998/12/28 14:10 Juan   40.0    333.00        <== excess hourly pay!

-- The challenge here is to do it without a bunch of messy code!

-- PS:
-- Note that there is more than one form of lookup table:
--  1. lookup within a range of values (min--max)
--  2. lookup by a key.
--  There might also be a lookup in a set, i.e. {1,3,4,7}, not used here.
--
-- It may be possible to code a single lookup function to
-- handle any of these without including extra parameters in the call,
-- which makes for much cleaner code.
--
-- end of assignment

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu