REM *------------- Section 1.4 --------------------*
REM *           ADD TO STOCK FILE		   *
REM *		-----------------		   *
REM *	UPDATED: February 5, 1983		   *
REM *	BY:	 Edward A. Valenzuela		   *
REM *	VERSION: 1.31				   *
REM *----------------------------------------------*

rem ----- requires following subroutines -----
rem          stkbas.bas   ... for jump vectors
rem	     scrnin.bas
rem          stkcreat.bas ... for blank menu
rem          qksort$.bas  ... alphabetical sort
rem          stkchang.bas ... for screen display
	
770 rem ... print screen ...

print clear$; \
	fn.down$(2); \
	fn.right$(15); "STOCK ADD/DELETE PROGRAM"
print fn.down$(2); fn.right$(4); "0 = return to main menu"
print fn.right$(4);              "1 = ADD to stock file"
print fn.right$(4);              "2 = DELETE stock from file"

print down$; fn.right$(2); "INSTRUCTIONS: enter menu selection"
print fn.right$(2); bell$;
      input "               and hit return"; field%
  if field% = 0 then goto 900
on field% gosub 771,772

RETURN

771 rem ... ADD TO STOCK FILE ...
    morestock$ = "y"

775 while morestock$ = "y"
    rem ... open files, read in all data ...
 GOSUB 875
 GOTO  872

 rem ----- SUBROUTINE TO READ AND BACKUP FILE -----
875 rem .. read in file ..
    if end #8 then 1791
    if end #9 then 1792 
    OPEN   "B:STKPRICE.DAT" AS 8
    OPEN   "B:STKFIL01.DAT" recl 80 as 9
  goto 1793

1791 print "no such file: B:STKPRICE.DAT exists"
     print "check disk on DRIVE B"
     input "enter y to resume"; y$
     INITIALIZE
     GOTO 871
1792 print "no such file: B:STKFIL01.DAT exists"
     print "check disk on DRIVE B"
     input "enter y to resume"; y$
     INITIALIZE
     GOTO 871

1793 if end #8 then 1794
     if end #9 then 1795 
GOSUB 320 	: rem ... read in stock price header
GOSUB 330	: rem ... read in stock price file
GOSUB 325	: rem ... read in S&P 500 price ...
1794 number.stock% = nstk%

GOSUB 315	: rem ... read in stock data file
1795 CLOSE 8,9

CREATE   "B:STKFIL01.BAK" recl 80 as 9
GOSUB 340	: rem ... write out stock data file
CLOSE 9  

871 RETURN
  rem ------ END OF READ AND BACKUP SUBROUTINE ----- 

872  nstk%=nstk%+1
  nitems% = nstk% + 1
  oldprice(nitems%) = oldprice(nstk%)
  newprice(nitems%) = newprice(nstk%)

GOSUB 710	: rem .. create input screen
GOSUB 715	: rem .. enter new stock data

rem ... read in current prices for new stocks ...

print clear$
print fn.line$(13); fn.right$(2); "INSTRUCTIONS:"
print fn.right$(2); bell$; "ENTER current stock price for :"; namstk$(nstk%)
  input NEWprice(nstk%)
  OLDprice(nstk%) = NEWprice(nstk%)

GOSUB 1681
GOTO  1682

REM *** SUBROUTINE ALPHABETIZE STOCK FILE ***
 
rem ... find the position of the new stock ...
rem ... find the length of the smallest string ...
1681 small%=24
    for i% = 1 to nstk%
     if len(namstk$(i%)) < small% then small% = len(namstk$(i%))
    next i%

rem ... pad the small names with z's ...
  for i%=1 to nstk%
    x$(i%) = UCASE$(namstk$(i%))
    if len(x$(i%)) <= (small% + 3) then x$(i%) = x$(i%) + "zzzz"
    x$(i%) = left$(x$(i%),small% + 4)
  next i%

    nadd%=1
  for i% = 1 to nstk%-1
    if x$(nstk%) > x$(i%) then nadd% = nadd% + 1
  next i%

RETURN : rem *** END SUBROUTINE ***

1682 print namstk$(nstk%); " is to be added after "; namstk$(nadd%-1)
print " in the "; nadd%; " position "

rem ... write the newfile ...

  open "B:STKPRICE.DAT" RECL 30 AS 8	
CREATE "B:STKFIL01.DAT" recl 80 as 9

rem ... write out the new number of stocks ...

GOSUB 345	: rem .. write out header
  i% = 1
  j% = 1
WHILE i% <= nadd%-1
 key%(i%) = i%
 print #9, j%; nshrs%(i%), namstk$(i%), symbol$(i%), xchng$(i%), \
    purdate$(i%), commission(i%), totpurprice(i%), key%(i%), dividend(i%)
 print #8, j%+1; newprice(i%), oldprice(i%)
  i% = i% + 1
  j% = j% + 1
WEND

 key%(nstk%) = i%
 i% = nstk%	: rem .. original position of added stock data

 print #9, j%; nshrs%(i%), namstk$(i%), symbol$(i%), xchng$(i%), \
    purdate$(i%), commission(i%), totpurprice(i%), key%(i%), dividend(i%)
 print #8, j%+1; newprice(i%), oldprice(i%)

  i% = nadd%
  j% = j% + 1
WHILE i% <= nstk%-1
 key%(i%) = i%
 print #9, j%; nshrs%(i%), namstk$(i%), symbol$(i%), xchng$(i%), \
    purdate$(i%), commission(i%), totpurprice(i%), key%(i%), dividend(i%)
 print #8, j%+1; newprice(i%), oldprice(i%)
  i% = i% + 1
  j% = j% + 1
WEND
 
  i% = nitems%
 print #8, j%+1; newprice(i%), oldprice(i%)

CLOSE 8,9

rem ... UPDATE THE HISTORICAL PRICE FILES FOR STOCK ADDITION ...
print clear$; fn.line$(4); bell$
  print "UPDATING HISTORICAL PRICE FILES FOR LATEST"
  print "  STOCK ADDITION ... you may wish to do something"
  print "  else for the next few minutes ..."
    GOSUB 7900

rem ... ask if another stock to be added ...
 
print clear$; fn.line$(13); fn.right$(2); "INSTRUCTIONS:"
 morestock$ = fn.entry$(0,44, \
  "input another stock? y or n?",13,16)

wend 
RETURN

rem ------------END OF SUBROUTINE ADD TO FILE---------------

772 rem ... DELETE STOCK FROM FILE SECTION ...
    rem ... open files, read in all data ...

 GOSUB 875	: rem .. form backup, read data

781 rem ... enter name of stock to be deleted ...

TITLE1$ = "STOCK DELETION PROGRAM"
TITLE2$ = "enter stock name to be deleted"

delete$="T"
GOSUB 7305 rem ... print title, ask for input, look for match
 if match(namstk$,namstk$(nrec%),1) > 0 then close 9 \
    else RETURN

rem ... ask if this is the correct stock to delete ...

print fn.line$(13); fn.right$(16); \
      "IS THIS THE CORRECT STOCK TO DELETE ?"
print fn.right$(16); bell$;
  INPUT "y or n?"; y$

if y$ = "y" then goto 767 else goto 781

767 rem ... continue reading the file ...


  if end #8 then 768
  if end #9 then 768
OPEN "B:STKPRICE.DAT" AS 8
OPEN "B:STKFIL01.DAT" RECL 80 AS 9

GOSUB 320	: rem .. read header

   i% = 1  
 WHILE i% <= nrec%
      GOSUB 325	: rem .. read stock price
      GOSUB 310	: rem .. read in stock data
   i% = i% + 1
   key%(i%) = i%
 WEND

   i% = nrec%
 WHILE i% <= number.stock% - 1
      GOSUB 325	: rem .. read in stock price
      GOSUB 310 : rem .. read in stock data
	rem .. for kth stock and store in ith place
   i% = i% + 1
   key%(i%) = i%
 WEND

   i% = number.stock%
      GOSUB 325 : rem .. read S&P 500 price

768 CLOSE 8,9

rem ... re-write to STOCK.PRICE.DAT
rem ...      and to STOCK.FILE.DAT

  if end #8 then 769
  if end #9 then 769
OPEN "B:STKPRICE.DAT" RECL 30 AS 8
OPEN "B:STKFIL01.DAT" RECL 80 AS 9

nstk% = number.stock% - 1  
  GOSUB 340	: rem .. write out stock file
  
  GOSUB 345	: rem .. write out header
  GOSUB 355	: rem .. write out price file
  GOSUB 350	: rem .. write S&P 500 price

769 CLOSE 8,9

rem ... UPDATE HISTORICAL PRICE FILES FOR STOCK DELETION ...
  print clear$; fn.line$(4); bell$
  print "UPDATING HISTORICAL PRICE FILES FOR LATEST"
  print "  STOCK DELETION ... you may wish to do something"
  print "  else for the next few minutes ..."
  GOSUB 7950

RETURN
