000100 identification division.
000200 program-id. TeamRecordsRB.
000300* FORMAT TEAM RECORDS AND PROVIDE A WINNING PCT.
000400* WINNING PERCENTAGE IS CALCULATED BY DIVIDING THE TOTAL
000500* POINTS EARNED BY THE TOTAL AMOUNTS POSSIBLE.  THE RECORD IS
000600* IN WW-LL-TT FORMAT WHERE WW IS WINS, LL IS LOSES AND TT IS TIES.
000700* TWO POINTS FOR A WIN, 0 POINTS FOR A LOSS AND 1 POINT FOR A TIE.
000800* COMPUTE THE TOTAL NUMBER POINTS POSSIBLE BY ADDING UP THE WINS,
000900* LOSSES AND TIES. THEN CALCULATE THE POINTS EARNED. THEN DO THE
001000* DIVISION.
001100* 
001200* THE PROGRAM REQUIRES TWO UNSTRINGS.  ONE TO SPLIT THE THREE
001300* FIELDS (DIVISION, CITY AND RECORD) INTO WORKING STORAGE FIELDS.
001400* THE SECOND UNSTRING NEEDS TO SPLIT THE RECORD INTO WINS, LOSSES
001500* AND TIES.
001600* THE RECORDS DO NOT HAVE LEADING ZEROS AND THIS WILL CAUSE A 
001700* PROBLEM.  SO, AFTER THE RECORD IS SPLIT INTO W, L, & T, THE
001800* SPACE WILL NEED TO BE REMOVED WITH EITHER AN INSPECT STATEMENT
001900* OR USING THE NUMVAL FUNCTION. THEN THE AMOUNTS WILL BE NUMERIC
002000* AND THE WINNING PCT CAN BE CALCULATED.  A NEW RECORD IS WRITTEN
002100* OUT WITH THE DIVISION, CITY, REFORMATTED RECORD AND THE PCT.
002200  
002300* YOU MAY USE THE WORKING STORAGE AND DATA DIVISION PROVIDED,
002400* OR YOUR OWN IF THAT IS EASIER FOR YOU TO FOLLOW.
002500 
002600* BE SURE ADD A DISPLAY A STATEMENT AT THE TOP OF THE PROCEDURE
002700* DIVISION TO USE A WHEN-COMPILED. 
002800  
002900 environment division.
003000 configuration section.
003100 INPUT-OUTPUT SECTION.
003200 FILE-CONTROL.
003300 SELECT LEAGUE-FILE
003400*  ASSIGN TO "C:\Users\Call1\league.dat"
003500   ASSIGN TO "C:\Users\Ryan\Downloads\league.dat"
003600   ORGANIZATION IS LINE SEQUENTIAL
003700   FILE STATUS IS WS-LEAGUE-FILE-STATUS.
003800 SELECT LEAGUE-FRMTD-FILE
003900*  ASSIGN TO "C:\Users\Call1\leagueRfmtd.dat"
004000   ASSIGN TO "C:\Users\Ryan\Desktop\leagueRfmtd.dat"
004100   ORGANIZATION IS LINE SEQUENTIAL
004200   FILE STATUS IS WS-RFMT-FILE-STATUS.
004300         
004400 DATA DIVISION.
004500 FILE SECTION.
004600 FD  LEAGUE-FILE                                    
004700     RECORDING MODE IS F  
004800     DATA RECORD IS LEAGUE-RECORD. 
004900 01  LEAGUE-RECORD               PIC X(24).
005000 
005100 FD  LEAGUE-FRMTD-FILE                                    
005200     RECORDING MODE IS F  
005300     DATA RECORD IS LEAGUE-FRMTD-RECORD. 
005400 01  LEAGUE-FRMTD-RECORD         PIC X(28).
005500
005600 WORKING-STORAGE SECTION.
005700 01  WS-FILLER                  PIC X(133)
005800     VALUE 'START WS SECTION'.
005900 01  WS-LEAGUE-RECORD.
006000     05 WS-DIVISION-CODE  	  PIC X(02).
006100     05 WS-CITY                 PIC X(15).
006200     05 WS-RECORD.
006300        10 WS-RECORD-6          PIC X(06).
006400        10 FILLER               PIC X(02).
006500     05 WS-PCT                  PIC V999.
006600     
006700 01  WS-RFMT-RECORD.
006800     05 WS-RFMT-WINS            PIC X(02) JUSTIFIED RIGHT.
006900     05 WS-RFMT-LOSSES          PIC X(02) JUSTIFIED RIGHT.
007000     05 WS-RFMT-TIES            PIC X(02) JUSTIFIED RIGHT.
007100 01  WS-RFMT-RECORD-NUMERIC REDEFINES
007200     WS-RFMT-RECORD.
007300     05 WS-RFMT-WINS-NUM        PIC 9(02).
007400     05 WS-RFMT-LOSSES-NUM      PIC 9(02).
007500     05 WS-RFMT-TIES-NUM        PIC 9(02).
007600     
007700 01  WS-TOTAL-PTS-AVAILABLE     PIC 9(02) VALUE ZERO.
007800 01  WS-TOTAL-PTS-EARNED        PIC 9(02) VALUE ZERO.
007900 01  WS-WINNING-PCT             PIC V999  VALUE ZERO.
008000
008100 01  WS-LEAGUE-FILE-STATUS      PIC X(02).
008200     88 SUCESSFUL-READ                 VALUE "00".
008300     88 OUT-OF-DATA                    VALUE "10".
008400     88 FILE-ERROR                     VALUES "11" THRU "99".
008500 01  WS-RFMT-FILE-STATUS        PIC X(02).
008600 
008700 PROCEDURE DIVISION.
008800 0000-MAINLINE.
008900     display "PROGRAM COMPILED: " function when-compiled.
009000     DISPLAY 'START: REFORMAT LEAGUE DATA'.
009100     PERFORM 0100-FORMAT-RECORDS.
009200     DISPLAY 'END: REFORMAT LEAGUE DATA'.
009300     STOP RUN.
009400     
009500 0100-FORMAT-RECORDS.
009600     OPEN INPUT  LEAGUE-FILE.
009700     OPEN OUTPUT LEAGUE-FRMTD-FILE
009800     PERFORM 0999-READ-LEAGUE-FILE.
009900     PERFORM 0200-PROCESS-LEAGUE-FILE
010000       UNTIL OUT-OF-DATA
010100          OR FILE-ERROR.
010200     CLOSE LEAGUE-FILE.
010300          
010400 0200-PROCESS-LEAGUE-FILE.           
010500     
010600* THE RECORD NEEDS TO BE SPILT INTO THE CORRECT FIELDS:    
010700     UNSTRING LEAGUE-RECORD delimited by ','
010800         INTO WS-DIVISION-CODE
010900              WS-CITY
011000              WS-RECORD-6
011100         ON OVERFLOW DISPLAY "OVERFLOW 1".
011200 
011300* NOW THE TEAM RECORD NEEDS TO BE SPLIT INTO W - L - T:    
011400     UNSTRING WS-RECORD-6 delimited by '-'
011500         INTO WS-RFMT-WINS
011600              WS-RFMT-LOSSES
011700              WS-RFMT-TIES
011800           on OVERFLOW display "OVERFLOW"
011900           not on OVERFLOW display "NO OVERFLOW".
012000              
012100* USE AN INSPECT OR NUMVAL FUNCTION TO ADD A LEADING ZERO TO 
012200* THE WIN, LOSS & TIE FIELDS, IT NEEDS TO BE DONE FOR EACH FIELD,
012300* SO PROBABLY THREE LINES OF CODE.  
012400     MOVE FUNCTION NUMVAL(WS-RFMT-WINS) TO WS-RFMT-WINS-NUM.
012500     MOVE FUNCTION NUMVAL(WS-RFMT-LOSSES) TO WS-RFMT-LOSSES-NUM.
012600     MOVE FUNCTION NUMVAL(WS-RFMT-TIES) TO WS-RFMT-TIES-NUM.
012700 
012800     COMPUTE WS-TOTAL-PTS-AVAILABLE = 
012900            (WS-RFMT-LOSSES-NUM + WS-RFMT-WINS-NUM
013000                                + WS-RFMT-TIES-NUM) * 2.
013100     COMPUTE WS-TOTAL-PTS-EARNED = 
013200            (WS-RFMT-WINS-NUM * 2) +  WS-RFMT-TIES-NUM.
013300     COMPUTE WS-WINNING-PCT = WS-TOTAL-PTS-EARNED/
013400                              WS-TOTAL-PTS-AVAILABLE.
013500     
013600     move WS-WINNING-PCT to WS-PCT.
013700* THE RECORD NEEDS TO BE STRING BACK TOGETHER AGAIN WITH THE "-"
013800* BEFORE IT IS MOVED TO THE OUTPUT FILE AND REWITTEN.
013900 
014000 
014100     STRING WS-RFMT-WINS-NUM
014200            "-"
014300            WS-RFMT-LOSSES-NUM
014400            "-"
014500            WS-RFMT-TIES-NUM
014600         INTO WS-RECORD.
014700           
014800       
014900     WRITE LEAGUE-FRMTD-RECORD FROM WS-LEAGUE-RECORD.
015000     PERFORM 0999-READ-LEAGUE-FILE.
015100     
015200 0999-READ-LEAGUE-FILE.
015300     READ LEAGUE-FILE.