BEGIN
	CLASS Consumer( prod );
   		REF(Producer) prod; ! constructor parameter;
		HIDDEN p1, p2, status, done, Main;
	BEGIN
		INTEGER p1, p2, status;
		BOOLEAN done;
		PROCEDURE main;
		BEGIN
			INTEGER money, receipt;

			money := 1;
			WHILE NOT done DO BEGIN
				OutText( "cons receives: " );
				OutInt( p1, 3 );
				OutText( ", " );
				OutInt( p2, 3 );
				status := status + 1;
				OutText( " and pays $" );
				OutInt( money, 3 ); OutImage;
				receipt := prod.payment( money );
				OutText( "cons receipt #" );
				OutInt( receipt, 3 ); OutImage;
				money := money + 1;
			END;
			OutText( "cons stops" ); OutImage;
		END;
		INTEGER PROCEDURE delivery( p1p, p2p );
			INTEGER p1p, p2p;
		BEGIN
			p1 := p1p;
			p2 := p2p;
			Resume( THIS Consumer );
			delivery := status;
		END;
		PROCEDURE stop;
		BEGIN
			done := TRUE;
			Call( THIS Consumer );
		END;
		! Consumer constructor code;
		status := 0;
		done := FALSE;
		Detach;
		main;
	END Consumer;

	CLASS Producer;
		HIDDEN cons, N, money, receipt, Main;
	BEGIN
		REF(Consumer) cons;
		INTEGER N, money, receipt;
		PROCEDURE main;
		BEGIN
			INTEGER i, p1, p2, status;

			FOR i := 1 STEP 1 UNTIL N DO BEGIN  
				p1 := RandInt( 1, 100, p1 );
				p2 := RandInt( 1, 100, p2 );
				OutText( "prod delivers: " );
				OutInt( p1, 3 ); OutText( ", " );
				OutInt( p2, 3 ); OutImage;
				status := cons.delivery( p1, p2 );
				OutText( "prod status: " );
				OutInt( status, 3 ); OutImage;
			END;
			cons.stop;
			OutText( "prod stops" ); OutImage;
		END;
		INTEGER PROCEDURE payment( moneyp );
			INTEGER moneyp;
		BEGIN
			money := moneyp;
			OutText( "prod payment of $" );
			OutInt( money, 3 ); OutImage;
			Resume( THIS Producer );
			receipt := receipt + 1;
			payment := receipt;
		END;
		PROCEDURE start( Np, consp );
			INTEGER Np;
			REF(Consumer) consp;
		BEGIN
			N := Np;
			cons :- consp;
			Resume( THIS Producer );
		END;
		! Producer constructor code;
		receipt := 0;
		Detach;
		main;
	END Producer;
	! program main equivalent;
	REF(Producer) prod;
	REF(Consumer) cons;
	prod :- NEW Producer;
	cons :- NEW Consumer( prod );
	prod.start( 5, cons );
END;

! Local Variables: ;
! tab-width: 4 ;
! compile-command: "cim ProdCons.sim" ;
! End: ;
