home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 568a.lha / RexxRMF_v1.1 / videorental.rexx < prev   
OS/2 REXX Batch file  |  1991-09-11  |  18KB  |  555 lines

  1. /* --------------------------------------------------------------- 
  2.  
  3.    Example illustrating use of 'linking' multiple files and indexes 
  4.  
  5.    This example 'links' three files together in an attempt to simulate
  6.    a 'relational' database for a fictious video rental store.
  7.  
  8.    File 1 - videofile    - 
  9.             contains all videos in the store
  10.             primary key is a 'videonumber' which can occur
  11.             multiple times because of multiple copies of the
  12.             same video.
  13.             alternate keys are videorating
  14.                                videocopynumber
  15.                                renting customer number
  16.                                hold for customer number
  17.  
  18.    File 2 - customerfile - 
  19.             contains names and addresses of registered 
  20.             video patrons.
  21.             primary key is a unique 'customernumber'
  22.             alternate keys are customerlastname
  23.                                customerbalance (if any)
  24.  
  25.    File 3 - rentalfile   - 
  26.             rental history of each video by customer
  27.             primary key is 'videonumber'
  28.             alternate keys are customernumber
  29.                                rentaldate
  30.                                videcopynumber
  31.  
  32.       I hope the relationship between the tree files is obvious.
  33.       The rentalfile links the videofile to the customer file. 
  34.  
  35.            
  36.  * --------------------------------------------------------------- */
  37.  
  38. /* ------------------- Record Layout Strings --------------------- */
  39.  
  40. customerrecord = " custnumber custfirst custlast custaddr custcity custstate,
  41.                    custzip custphone custareacode custdeposit custbalance"
  42.  
  43. videorecord    = " videonum videotitle videocopy videorating videopudate,
  44.                    videopuprice videosaleprice videoavail videohold videocust"
  45.  
  46. rentalrecord   = " rentalcustnumber rentalvideonum rentalvideocopy rentaldate"
  47.  
  48.  
  49. /* --- make library available --- */
  50.  
  51. x = addlib("RexxRMF.library",0,-30,0) 
  52.  
  53.  
  54.  
  55. if exists("videofile") = 1 then 
  56.    loadfile = 0
  57. else
  58.    loadfile = 1
  59.    
  60. /* will allow duplicate keys in primary index for videos */
  61. /* primary index is video number */
  62.  
  63. rmfvideos    = open_rmf("videofile")
  64. say "videofile " any_err(rmfvideos)
  65.  
  66. /* will NOT allow duplicate keys in primary index for customers */
  67. /* primary index is customer number */
  68.  
  69. rmfcustomers = open_rmf("customerfile",1)
  70. say "customersfile " any_err(rmfcustomers)
  71.  
  72.  
  73. /* will allow duplicates in any index for rentals */
  74.  
  75. rmfrentals   = open_rmf("rentalfile")
  76. say "rentalfile " any_err(rmfrentals)
  77.  
  78.  
  79. if loadfile = 1 then   /* create data/index files */
  80.    do
  81.    r = loaddata()
  82.    if r = 0 then /* file creation failed */      
  83.       do
  84.           if rmfvideos ~= '0000 0000'x then 
  85.              call close_rmf(videofile,1)
  86.           if rmfcustomers ~= '0000 0000'x then 
  87.              call close_rmf(rmfcustomers,1)
  88.           if rmfrentals ~= '0000 0000'x then 
  89.              call close_rmf(rmfrentals,1)
  90.           if exists('videofile') then 
  91.              delete 'videofile'
  92.           if exists('customerfile') then 
  93.              delete 'customerfile'
  94.           if exists('rentalfile') then 
  95.              delete 'rentalfile'
  96.           if exists('videofile.rmfindex') then 
  97.              delete 'videofile.rmfindex'
  98.           if exists('customerfile.rmfindex') then 
  99.              delete 'customerfile.rmfindex'
  100.           if exists('rentalfile.rmfindex') then 
  101.              delete 'rentalfile.rmfindex'
  102.           say "  ... BIG ERROR !!!  Unable to create data/index files  ... "
  103.           exit
  104.       end
  105.    end
  106.    
  107. x = open(outwin,"CON:0/0/640/200//s")
  108.  
  109. /* just some console escape sequences */
  110. eraseconsolescreen = '9b48 9b4a'x   
  111. offsetwrites = '9b39 3978'x
  112. offsettop = '9b39 3979'x
  113. resettop = '9b79'x
  114. resetwrites = '9b78'x
  115. EOD = '9b4a'x
  116. EOL = '9b4b'x
  117. CURSORUP = '9b46'x
  118. CURSORDOWN = '9b45'x
  119.  
  120.  
  121.  
  122. /* --------- Our Menu --------- */
  123.  
  124. do forever 
  125.    zout = writech(outwin,rowcol(1,1)EOD)
  126.    zout = writeln(outwin,'0a'x copies(' ',16)color(2,1)"Your Get Rich Quick Video Rental Store" color(1,0) '0a'x)
  127.    zout = writeln(outwin,'0a'x copies(' ',28) color(1,0)"M  E  N  U"'0a'x'0a'x)
  128.    zout = writeln(outwin,'0a'x copies(' ',24) color(1,0)"1)." color(3,0)" Customer Inquiry")
  129.    zout = writeln(outwin,'0a'x copies(' ',24) color(1,0)"2)." color(3,0)" Video Inquiry")
  130.    zout = writeln(outwin,'0a'x copies(' ',24) color(1,0)"X)." color(3,0)" Enter" color(2,0)"X"color(3,0)" to exit")
  131.    zout = writeln(outwin,'0a'x'0a'x'0a'x)
  132.    zout = writech(outwin, copies(' ',23) color(1,0)"Enter Option 1 or 2 or X: ")
  133.    op = readln(outwin)
  134.    select 
  135.       when op = 1 then /* our CUSTOMER INQUIRY */
  136.         do
  137.            do forever
  138.               zout = writech(outwin,rowcol(1,1)EOD)
  139.               x = customerhistory() 
  140.               if x > 50 then leave
  141.            end
  142.         end
  143.       when op = 2 then /* our VIDEO INQUIRY */
  144.         do
  145.            zout = writech(outwin,rowcol(1,1)EOD)
  146.            vx = videohistory(0)
  147.            do forever
  148.               vx = videohistory(vx)
  149.               if vx > 99999 then leave
  150.               if vx = 0 then leave
  151.            end
  152.         end
  153.       when op = 'X' then leave
  154.       when op = 'x' then leave
  155.       otherwise nop
  156.    end
  157. end
  158.  
  159.  
  160. /* ---------- Save Files --------- */
  161.  
  162. call writech(outwin,rowcol(1,1)EOD)
  163. call writech(outwin,rowcol(12,32)color(2,1)"Saving Video Data"color(1,0))
  164. x = close_rmf(rmfvideos)
  165.  
  166. call writech(outwin,rowcol(1,1)EOD)
  167. call writech(outwin,rowcol(12,32)color(2,1)"Saving Customer Data"color(1,0))
  168. x = close_rmf(rmfcustomers)
  169.  
  170. call writech(outwin,rowcol(1,1)EOD)
  171. call writech(outwin,rowcol(12,32)color(2,1)"Saving Rental Data"color(1,0))
  172. x = close_rmf(rmfrentals)   
  173.  
  174. x = close(outwin)
  175.  
  176. exit
  177.  
  178. /*-------------------------------------------------------------------------*
  179.  *-                                                                       -*
  180.  *-------------------------------------------------------------------------*/
  181. customerhistory:
  182.  
  183. zout = writech(outwin,'0a'x "Enter Customer Name or Number ?")
  184.  
  185. customer = readln(outwin)
  186.  
  187. customer = strip(customer)
  188.  
  189. if customer = '' then return 99
  190.  
  191. if datatype(customer) = 'NUM' then  /* entered a customer number */
  192.    do
  193.       l = length(customer)
  194.       if l > 4 then
  195.          do
  196.             zout = writeln(outwin,"Invalid customer number, must be 4digits or less")
  197.             return 0
  198.          end
  199.          
  200.       customer = (copies('0',(4-l)) || customer)  /* append leading zeros */
  201.    end
  202.       
  203. else          /* else if not numeric assume entered a customer last name */
  204.    customer = upper(customer)
  205.  
  206. /* READ THE CUSTOMER FILE */
  207. /* the primary index (0) indexes on customer number */
  208. /* the first alternate (1) indexes on customer last name */
  209.  
  210. if datatype(customer) = 'NUM' then  /* READ BY CUSTOMER NUMBER */
  211.  
  212.    x = read_rmf_record(rmfcustomers,0,customerrecord,customer,'K')
  213.  
  214. else /* READ BY CUSTOMER LAST NAME */
  215.  
  216.    x = read_rmf_record(rmfcustomers,1,customerrecord,customer,'K')
  217.  
  218.  
  219. if x = 0 then 
  220.    do
  221.       zout = writeln(outwin,'0a'x'0a'x"Customer:" customer "NOT FOUND")
  222.       zout = writech(outwin,'0a'x'0a'x"Press Return to Continue ...")
  223.       x = readln(outwin)
  224.       return 0
  225.    end      
  226.  
  227.    
  228. /* DISPLAY CUSTOMER RECORD */
  229.  
  230. zout = writeln(outwin,'0a'x color(3,0)"Cust#:" color(2,0)custnumber)
  231. zout = writeln(outwin,color(3,0)" Name :" color(2,0)custfirst custlast )
  232. zout = writeln(outwin,color(3,0)" Addrs:" color(2,0)custaddr )
  233. zout = writeln(outwin,color(3,0)" City :" color(2,0)custcity custstate custzip )
  234. zout = writeln(outwin,color(3,0)" Phone:" color(2,0)'('custareacode')' custphone )
  235.       
  236. zout = writeln(outwin,rowcol(5,40)color(3,0)"Deposit On File:" color(2,0)custdeposit )
  237. zout = writeln(outwin,rowcol(6,40)color(3,0)"Balance Due:" color(2,0)custbalance)
  238. zout = writeln(outwin,rowcol(9,1))
  239. zout = writech(outwin,offsetwrites)
  240. zout = writeln(outwin,color(2,1)"Film# CP  Rented       Title"copies(' ',23)"Rating"color(1,0)'0a'x)
  241. zout = writech(outwin,offsettop)
  242.  
  243.  
  244. /* READ THE RENTAL FILE FOR THIS CUSTOMER */
  245.  
  246. x = read_rmf_record(rmfrentals,1,rentalrecord,custnumber,'K')
  247.  
  248. if x = 0 then zout = writeln(outwin, "No rental records found")
  249.  
  250. displayed = 0
  251.  
  252. do while x = 1  /* the NEXT_RMF_RECORD below will set x to zero */
  253.  
  254.    /* READ THE VIDEO FILE TO GET VIDEO INFO */
  255.    y = read_rmf_record(rmfvideos,0,videorecord,rentalvideonum,'K')
  256.    if y = 1 then 
  257.       do
  258.          title = substr(videotitle,1,32," ")
  259.          zout = writeln(outwin, rentalvideonum rentalvideocopy rentaldate title videorating)
  260.          displayed = displayed + 1
  261.       end
  262.    if displayed = 9 then 
  263.       do   
  264.          x = writech(outwin,'0a'x'0a'x"Press Return to Continue ...")
  265.          x = readln(outwin)
  266.          x = writech(outwin,'9b46 9b4b'x) /* move up and erase line */
  267.          x = writech(outwin,'9b46'x)      /* to effect scrolling    */
  268.          x = writech(outwin,'9b46'x)
  269.          displayed = 0
  270.       end
  271.       
  272.    /* get the next rental record for this customer */
  273.    x = next_rmf_record(rmfrentals,1,rentalrecord)   
  274.  
  275. end
  276.  
  277. x = writech(outwin,'0a'x"**** **** End of List **** ****")
  278. x = writech(outwin,'0a'x'0a'x"Press Return to Continue ...")
  279. x = readln(outwin)
  280. zout = writech(outwin,resetwrites)
  281. zout = writech(outwin,resettop)
  282. return 1
  283.  
  284. /*-------------------------------------------------------------------------*
  285.  *-                                                                       -*
  286.  *-------------------------------------------------------------------------*/
  287. videohistory:
  288.  
  289. parse arg vidnumber
  290.  
  291. if vidnumber = 0 then 
  292.    do
  293.       /* GET LIST OF VIDEO NUMBERS */
  294.       vidnode = find_pos(rmfvideos,0,1)    /* find first record in index     */
  295.  
  296.       zout = writech(outwin,copies(' ',29) color(2,1)"Video Numbers" color(1,0) '0a'x "  ")
  297.  
  298.       displayed = 0
  299.       linecnt   = 0
  300.       do while vidnode ~= '0000 0000'x /* vidnode is a POINTER to node in the tree */
  301.  
  302.          thevideonumberis =  "   " || key_value(vidnode)
  303.  
  304.          zout = writech(outwin,(color(2,1) || thevideonumberis || color(1,0)) )
  305.  
  306.          displayed = displayed + 1
  307.  
  308.          if displayed > 7 then
  309.             do
  310.                displayed = 0
  311.                zout = writech(outwin,(color(2,1) || "  " || '0a'x || color(1,0) || "   ") )
  312.                linecnt = linecnt + 1
  313.                if linecnt > 8 then leave
  314.             end
  315.  
  316.          vidnode = next_unique(rmfvideos,0)  /* low-level functions dont do any I/O */
  317.  
  318.       end
  319.  
  320.       vidnumber = get_a_video_number()
  321.       
  322. end  /* if vidnumber = 0 */
  323. zout = writech(outwin,color(1,0))
  324.  
  325.  
  326.  
  327. if vidnumber > 99999 then return 999999
  328. if vidnumber = 0 then return 999999
  329.  
  330. /* READ THE VIDEO FILE TO GET VIDEO INFO */
  331.  
  332. do forever
  333.    x = read_rmf_record(rmfvideos,0,videorecord,vidnumber,'K')
  334.    
  335.    if x = 1 then leave  /* found video in file */
  336.    
  337.    zout = writeln(outwin,rowcol(12,1)"Video Number:" vidnumber "NOT FOUND")
  338.    zout = writech(outwin, rowcol(21,1))
  339.    zout = writech(outwin,"Press Return to Continue ...")
  340.    zin  = readln(outwin)
  341.    
  342.    vidnumber = get_a_video_number()
  343.    
  344.    if vidnumber > 99999 then return 999999
  345.    if vidnumber = 0 then return 999999
  346.    
  347. end
  348.  
  349. currentcust = ''   
  350. currenthold = ''
  351.  
  352. videocust = random(1,100) /* generate a customer number */
  353.  
  354. if videocust > 0 then /* READ CUSTOMER FILE FOR CURRENT RENTER */
  355.    do
  356.       videocust = copies('0',(4-length(videocust))) || videocust
  357.       custfound = read_rmf_record(rmfcustomers,0,customerrecord,videocust,'K')
  358.       if custfound then
  359.          currentcust = custfirst custlast
  360.       else
  361.          videocust = '0000'
  362.    end   
  363.  
  364. videohold = random(1,100) /* generate a customer number */
  365. if videohold = videocust then
  366.    videohold = 0
  367.  
  368. if videohold > 0 then /* READ CUSTOMER FILE FOR, HOLD FOR CUSTOMER */
  369.    do
  370.       videohold = copies('0',(4-length(videohold))) || videohold
  371.       holdfound = read_rmf_record(rmfcustomers,0,customerrecord,videohold,'K')
  372.       if holdfound then 
  373.          currenthold = custfirst custlast
  374.       else
  375.          videohold = '0000'
  376.    end   
  377.  
  378. /* HOW MANY COPIES WE GOT */
  379. count = key_count(rmfvideos,0,vidnumber,'K')
  380.  
  381. zout = writeln(outwin,rowcol(12,1)" Video#:" videonum " Number of copies:" count)
  382. title = substr(videotitle,1,32,' ')
  383. zout = writech(outwin,color(3,0)" Title           :")
  384. zout = writeln(outwin,color(2,0)title color(3,0) "Copy#:" color(2,0)videocopy color(3,0)"Rating:" color(2,0)videorating)
  385. zout = writeln(outwin,color(3,0)"   Date Purchased:"color(2,0)videopudate)
  386. zout = writeln(outwin,color(3,0)"   Purchase Price:"color(2,0)videopuprice)
  387. zout = writeln(outwin,color(3,0)"   Sale Price    :"color(2,0)videosaleprice)
  388. zout = writeln(outwin,color(3,0)"   Available     :"color(2,0)videoavail)
  389. zout = writeln(outwin,color(3,0)"   Hold For      :"color(2,0)videohold color(3,0)"    Name:" color(2,0)currenthold)
  390. zout = writeln(outwin,color(3,0)"   Current Renter:"color(2,0)videocust color(3,0)"    Name:" color(2,0)currentcust)
  391.    
  392. zout = writech(outwin, rowcol(21,1))
  393. x = writech(outwin, color(1,0) "Press Return to Continue ...")
  394. x = readln(outwin)
  395.  
  396. return get_a_video_number()
  397.  
  398.  
  399.  
  400.  
  401. /*-------------------------------------------------------------------------*
  402.  *-                                                                       -*
  403.  *-------------------------------------------------------------------------*/
  404.  
  405. get_a_video_number:
  406.  
  407. do forever
  408.    zout = writech(outwin,rowcol(12,1)EOD)
  409.    zout = writech(outwin,rowcol(21,1) color(1,0)"Enter Video Number ?")
  410.    vidn = readln(outwin)
  411.    if vidn = '' then leave
  412.    if datatype(vidnumber) = 'NUM' then
  413.       do
  414.          vl = length(vidn)
  415.          if vl > 5 then
  416.             do
  417.                zout = writech(outwin, color(1,0) rowcol(12,1) EOD)
  418.                zout = writeln(outwin,"Invalid Video Number, must be 5digits or less")
  419.                zout = writech(outwin, rowcol(21,1))
  420.                zout = writech(outwin,"Press Return to Continue ...")
  421.                zin  = readln(outwin)
  422.             end
  423.  
  424.          vidn = (copies('0',(5-vl)) || vidn) /* append leading zeros */
  425.          if vidn = '00000' then return 999999
  426.          return vidn
  427.       end
  428.    else 
  429.       do
  430.          zout = writeln(outwin,"Invalid Video Number, must be Numeric")
  431.          zout = writech(outwin, rowcol(21,1))
  432.          zout = writech(outwin,"Press Return to Continue ...")
  433.          zin  = readln(outwin)
  434.       end
  435. end
  436.  
  437. return 999999
  438.  
  439.  
  440. /*-------------------------------------------------------------------------*
  441.  *-                                                                       -*
  442.  *-------------------------------------------------------------------------*/
  443.  rowcol:
  444.  parse arg row,col
  445.  outb = '9b'x || row || ';' || col || 'H'
  446.  return outb
  447.  
  448. /*-------------------------------------------------------------------------*
  449.  *-                                                                       -*
  450.  *-------------------------------------------------------------------------*/
  451.  color:
  452.  parse arg fg,bg
  453.  outb = '9b'x || '0;3' || fg || ';4' || bg || 'm'
  454.  return outb
  455.  
  456.  
  457.  
  458. /*-------------------------------------------------------------------------*
  459.  *-                                                                       -*
  460.  *- Load RMF files with data                                              -*
  461.  *- This is only called when the rmf files do not exists                  -*
  462.  *-                                                                       -*
  463.  *-------------------------------------------------------------------------*/
  464.  
  465. loaddata:
  466.  
  467.  
  468. x = open(viddata,"viddata",'R')
  469. if x = 0 then 
  470.    do 
  471.       zout = writeln(outwin, "could not open video data file")
  472.       return 0
  473.    end
  474.    
  475. x = open(rentdata,"rentdata",'R')
  476. if x = 0 then
  477.    do 
  478.       zout = writeln(outwin, "could not open rental data file")
  479.       return 0
  480.    end
  481.    
  482. x = open(custdata,"custdata",'R')
  483. if x = 0 then
  484.    do 
  485.       zout = writeln(outwin, "could not open customer data file")
  486.       return 0
  487.    end
  488.  
  489. /* LOAD CUSTOMER DATA */
  490. do forever
  491.    cdata = readln(custdata)
  492.    if eof(custdata) = 1 then leave
  493.    if substr(cdata,1,1) = '#' then iterate
  494.    parse var cdata custnumber ',' custfirst ',' custlast ',' custaddr ',' ,
  495.                    custcity ',' custstate ',' custzip ',' custphone ',' , 
  496.                    custareacode ',' custdeposit ',' custbalance
  497.    
  498.    lastname = upper(custlast)
  499.    
  500.    if custbalance > 0 then
  501.       x = write_rmf_record(rmfcustomers,customerrecord,custnumber,1,lastname)
  502.    else
  503.       x = write_rmf_record(rmfcustomers,customerrecord,custnumber,1,lastname,2,custbalance)
  504.    
  505.    if x = 0 then 
  506.       do   
  507.          say "Write Failed " any_err(rmfcustomers)
  508.          say custfirst custlast custnumber
  509.       end
  510. end
  511.  
  512. /* LOAD VIDEO DATA */
  513. do forever
  514.    vdata = readln(viddata)
  515.    if eof(viddata) = 1 then leave
  516.    if substr(vdata,1,1) = '#' then iterate
  517.    parse var vdata videonum ',' videotitle ',' videocopy ',' videorating ',' ,
  518.                    videopudate ',' videopuprice ',' videosaleprice ',' ,
  519.                    videoavail ',' videohold ',' videocust
  520.                    
  521.    filmcopy = strip((videonum || videocopy))
  522.    
  523.    if videoavail = 'Y' then
  524.       x = write_rmf_record(rmfvideos,videorecord,videonum,1,videorating,4,filmcopy)
  525.    else
  526.    if videohold > 0 then  /* hold video for this customer */
  527.       x = write_rmf_record(rmfvideos,videorecord,videonum,1,videorating,2,videohold,4,filmcopy)
  528.    else
  529.       if videocust > 0 then /* this customer has it */
  530.          x = write_rmf_record(rmfvideos,videorecord,videonum,1,videorating,3,videocust,4,filmcopy)
  531.    
  532.    if x = 0 then 
  533.       do
  534.          say "Write failed" any_err(rmfvideos)
  535.          say videonum videocopy
  536.       end
  537. end
  538.  
  539. /* LOAD RENTAL DATA */
  540. do forever
  541.    rdata = readln(rentdata)
  542.    if eof(rentdata) = 1 then leave
  543.    if substr(rdata,1,1) = '#' then iterate
  544.    parse var rdata rentalcustnumber ',' rentalvideonum ',' rentalvideocopy ',' rentaldate
  545.    filmcopy = strip( (rentalvideonum || rentalvideocopy) )
  546.    x = write_rmf_record(rmfrentals,rentalrecord,rentalvideonum,1,rentalcustnumber,2,rentaldate,3,filmcopy)
  547. end
  548.  
  549. x = close(viddata)
  550. x = close(rentdata)
  551. x = close(custdata)
  552.  
  553. return 1
  554.  
  555.