(* Title: 	StdRec
	Author: Yamama Khadduri
	Revised: 09-OCT-2002
	Description: Creates new records for students & keep track of their attendance to class.
	
	Interface:
	INTERFACE StdRec;
		PROCEDURE NewStd (StdID:INTEGER);
		PROCEDURE Attendance (StdID:INTEGER):INTEGER;
	END StdRec.
*)
	
MODULE StdRec;

IMPORT Out;

	(* TYPE *)
	TYPE
		Std*= POINTER TO StudentsRec; (*pointer to a record*)
		StudentsRec = RECORD;
			StdID: INTEGER;		  (*Student ID field*)
			Presence: INTEGER;	  (*Number of Presence field*)
			Position:Std; 		  (*Position of the record*)
		END;
		
	VAR Head:Std; (*A pointer pointing to a record*)
		
(******************* NewStd ********************)
	PROCEDURE NewStd*(StdID:INTEGER);
		VAR Student:Std; (*A pointer to a record*)
	BEGIN
		IF SearchData(StdID) THEN
			NEW(Student); (*Creating a new record with the pointer name Student*)
			Student^.StdID:=StdID; (*Storing data in its fields*)
			Student^.Presence:=1;
			Student^.Position:=Head; (*The record pointer points to Head's location*)
			Head:=Student; (*putting the location of the new record in Head*)
		ELSE
			RETURN;
		END;
	END NewStd;

(****************** Attendance *******************)
	PROCEDURE Attendance*(StdID:INTEGER):INTEGER;
		VAR ptr:Std;
	BEGIN
		ptr:= SearchData(StdID); (*using the record of the returned pointer*)
		IF ptr THEN 
			RETURN 0; (*if student is not stored, he has 0 presences*)
		ELSE
			ptr^.Presence:= ptr^.Presence +1;
			RETURN ptr^.Presence; (*returning the value stored in Presence of that student*)
		END; 	(*end of IF statement*)
	END Attendance;
	
(***************** SearchData ********************)	
	PROCEDURE SearchData(StdID:INTEGER):Std;
		VAR ptr:Std; (*creating a new pointer*)
	BEGIN
		ptr:=Head; 	(*pointing ptr to the last record created*)
		(*checking if the pointer is NIL(end of records/no records) and if student existed*)
		
		WHILE (ptr#NIL) & (ptr^.StdID # StdID) DO
			ptr:=ptr^.Position; (*going to the next record*)
		END;
		
		RETURN ptr; (*returning the pointer to the found record*)
	END SearchData;

(******************** Test **********************)
(* Pretend this is a separate Test module that Imports StdRec *)
	PROCEDURE Test*();
	BEGIN
		NewStd(0099999);
		NewStd(0088888);
		NewStd(0077777);
		Out.Int(Attendance(0099999),2);
		Out.Int(Attendance(0099999),2);
		Out.Int(Attendance(0088888),2);
	END Test;
(****************** Module Body ****************)	
BEGIN
	Test();
	
END StdRec.
