PROGRAM FOGSTORE;
(* By Ron Forsythe *)

VAR
  OPONE,OPTWO,OPTHREE: REAL;

FUNCTION SALESTAX(AMOUNT: REAL): REAL;
CONST
  RATE = 0.06;
BEGIN
  SALESTAX := AMOUNT * RATE;
END;(* SALESTAX *)

PROCEDURE WRITECHR(CHRIN : CHAR; NUM :INTEGER);
VAR
  CTR : INTEGER;
BEGIN
  FOR CTR := 1 TO NUM DO
    WRITE(CHRIN);
END;(* WRITECHR *)

PROCEDURE PAUSE;
VAR
  CH : CHAR;
BEGIN
  WRITELN;
  WRITE('                -------> TYPE <RETURN> TO CONTINUE <-------');
  READLN(CH);
END; (* PAUSE *)

PROCEDURE ERROR;
BEGIN
  WRITE(CHR(7));
  WRITECHR(' ',14);
  WRITELN('-----> NOT A VALID INPUT, PLEASE TRY AGAIN <-----');
END;(* ERROR *)

PROCEDURE TERMINATE;
BEGIN
  WRITE(CHR(26));
  WRITELN('OKAY...........SO LONG');
END; (* TERMINATE *)

PROCEDURE CHANGE(TOTAL: REAL);
TYPE
  DENOMINATION = ARRAY[1..8] OF CHAR;
VAR
  NAMES: ARRAY[1..9] OF DENOMINATION;
  VALUES: ARRAY[1..9] OF INTEGER;
  AMOUNT: REAL;
  COUNT,CHANGE, I: INTEGER;
BEGIN
  NAMES[1] := 'TWENTIES';  VALUES[1] := 2000;
  NAMES[2] := 'TENS    ';  VALUES[2] := 1000;
  NAMES[3] := 'FIVES   ';  VALUES[3] :=  500;
  NAMES[4] := 'ONES    ';  VALUES[4] :=  100;
  NAMES[5] := 'HALVES  ';  VALUES[5] :=   50;
  NAMES[6] := 'QUARTERS';  VALUES[6] :=   25;
  NAMES[7] := 'DIMES   ';  VALUES[7] :=   10;
  NAMES[8] := 'NICKLES ';  VALUES[8] :=    5;
  NAMES[9] := 'PENNIES ';  VALUES[9] :=    1;

  WRITE('AMOUNT TENDERED? ');
  READLN(AMOUNT);
  AMOUNT := AMOUNT - TOTAL;
  WRITELN;WRITELN('CHANGE IS ',AMOUNT:10:2);
  WRITELN;
  IF AMOUNT < 327.68 THEN
  CHANGE := ROUND(100.0 * AMOUNT);
    FOR I := 1 TO 9 DO
    BEGIN
      COUNT := CHANGE DIV VALUES[I];
      CHANGE := CHANGE MOD VALUES[I];
      IF COUNT > 0 THEN
      WRITELN(NAMES[I],': ',COUNT);
    END;
  PAUSE;
END;(* PROCEDURE CHANGE *)

PROCEDURE CHECKNADD(OPTION: CHAR; QUAN,PRICE: REAL;VAR OK:BOOLEAN);
BEGIN
  OK := TRUE;
  CASE OPTION OF
    '1' : IF (PRICE < 5.00) OR (PRICE > 150.00)
            THEN OK := FALSE
          ELSE OPONE := OPONE + (QUAN * PRICE);
    '2' : IF (PRICE < 2.00) OR (PRICE > 25.00)
            THEN OK := FALSE
          ELSE OPTWO := OPTWO + (QUAN * PRICE);
    '3' : IF (PRICE < 0.50) OR (PRICE > 15.0)
            THEN OK := FALSE
          ELSE OPTHREE := OPTHREE + (QUAN * PRICE);
    ELSE ERROR
   END; (* CASE *)
END; (* CHECKNADD *)

PROCEDURE DAYTOTAL;
VAR
  TOTAL: REAL;
BEGIN
  WRITECHR('=',79);WRITELN;
  WRITECHR(' ',33);WRITELN('DAILY TOTALS');
  WRITECHR('=',79);
  WRITELN;WRITELN;WRITELN;
  WRITECHR(' ',33);WRITELN('SOLD       TAX      TOTAL');
  WRITELN;
  WRITE('SOFTWARE..................... ',OPONE:8:2);
  WRITELN(SALESTAX(OPONE):10:2,(OPONE + SALESTAX(OPONE)):10:2);
  WRITE('BOOKS AND TAPES.............. ',OPTWO:8:2);
  WRITELN(SALESTAX(OPTWO):10:2,(OPTWO + SALESTAX(OPTWO)):10:2);
  WRITE('MISC. MERCHANDISE............ ',OPTHREE:8:2);
  WRITELN(SALESTAX(OPTHREE):10:2,(OPTHREE + SALESTAX(OPTHREE)):10:2);
  TOTAL := OPONE + OPTWO + OPTHREE;
  WRITELN;
  WRITE('TOTAL........................ ',TOTAL:8:2);
  WRITELN(SALESTAX(TOTAL):10:2,(TOTAL + SALESTAX(TOTAL)):10:2);
  WRITELN;WRITELN;WRITELN;
  PAUSE;
END; (* DAYTOTAL *)

PROCEDURE REGISTER;
VAR
  OPTION: CHAR;
  OK: BOOLEAN;
  PRICE,QUAN,SUBTOTAL,TAX,TOTAL: REAL;
BEGIN
  SUBTOTAL := 0;
  WRITE(CHR(26));
  WRITECHR('=',79); WRITELN;
  WRITECHR(' ',33);
  WRITELN('CASH REGISTER');
  WRITECHR('=',79); WRITELN;
  WRITELN; WRITELN;
  WRITECHR(' ',15); WRITELN('1 = SOFTWARE ($5.00 TO $150.00)');
  WRITELN;
  WRITECHR(' ',15); WRITELN('2 = BOOKS AND TAPES ($2.00 TO $25.00)');
  WRITELN;
  WRITECHR(' ',15); WRITELN('3 = MISC. MERCHANDISE ($.50 TO $15.00)');
  WRITELN;
  WRITECHR('_',79); WRITELN;
  WRITE('ENTER YOUR CHOICE - 1,2,3 OR ANY OTHER KEY FOR SUBTOTAL --> ');
  READLN(OPTION);
  WHILE OPTION IN ['1','2','3'] DO
    BEGIN
      WRITE('QUANTITY? ');READLN(QUAN);
      WRITE('PRICE? ');READLN(PRICE);
      CHECKNADD(OPTION,QUAN,PRICE,OK);
      IF NOT OK
        THEN ERROR
      ELSE
        SUBTOTAL := SUBTOTAL + (QUAN * PRICE);
      WRITELN;
      WRITE ('ENTER YOUR CHOICE - 1,2,3 OR ANY OTHER KEY FOR SUBTOTAL --> ');
      READLN(OPTION);
    END; (* WHILE *)
  WRITELN;
  WRITE(CHR(26));
  WRITELN('SUBTOTAL = ',SUBTOTAL:8:2);
  TAX := SALESTAX(SUBTOTAL);
  WRITELN('SALESTAX = ',TAX:8:2);
  WRITECHR(' ',11);WRITECHR('-',8);WRITELN;
  TOTAL := SUBTOTAL+TAX;
  WRITELN('   TOTAL = ',TOTAL:8:2);
  CHANGE(TOTAL);
END;(* REGISTER *)

PROCEDURE MAINMENU;
VAR
  CHOICE : CHAR;
BEGIN
  REPEAT
    WRITELN(CHR(26));
    WRITECHR('=',79); WRITELN;
    WRITECHR(' ',30);WRITELN('THE FOG COMPUTER STORE');
    WRITECHR('=',79);
    WRITELN;WRITELN;WRITELN;
    WRITELN('YOUR SELECTIONS');
    WRITECHR('-',15);
    WRITELN;WRITELN;
    WRITELN('1  CASH REGISTER');
    WRITELN('2  DAILY TOTALS');
    WRITELN('3  QUIT PROGRAM');
    WRITELN;WRITELN;
    WRITECHR('_',79); WRITELN;
    REPEAT
      WRITE('PLEASE MAKE YOUR SELECTION 1,2 OR 3 --> ');
      READLN(CHOICE);
      IF NOT (CHOICE IN ['1','2','3'])
      THEN ERROR;
    UNTIL CHOICE IN ['1','2','3'];
    WRITELN(CHR(26));
    CASE CHOICE OF
      '1' : REGISTER;
      '2' : DAYTOTAL;
      '3' : TERMINATE;
    END; (* CASE *)
  UNTIL CHOICE = '3';
END; (* MAINMENU *)

BEGIN
  OPONE := 0; OPTWO := 0; OPTHREE := 0;
  MAINMENU;
END.
END.
