1 | BEGIN |
---|
2 | CLASS Consumer( prod ); |
---|
3 | REF(Producer) prod; ! constructor parameter; |
---|
4 | HIDDEN p1, p2, status, done, Main; |
---|
5 | BEGIN |
---|
6 | INTEGER p1, p2, status; |
---|
7 | BOOLEAN done; |
---|
8 | PROCEDURE main; |
---|
9 | BEGIN |
---|
10 | INTEGER money, receipt; |
---|
11 | |
---|
12 | money := 1; |
---|
13 | WHILE NOT done DO BEGIN |
---|
14 | OutText( "cons receives: " ); |
---|
15 | OutInt( p1, 3 ); |
---|
16 | OutText( ", " ); |
---|
17 | OutInt( p2, 3 ); |
---|
18 | status := status + 1; |
---|
19 | OutText( " and pays $" ); |
---|
20 | OutInt( money, 3 ); OutImage; |
---|
21 | receipt := prod.payment( money ); |
---|
22 | OutText( "cons receipt #" ); |
---|
23 | OutInt( receipt, 3 ); OutImage; |
---|
24 | money := money + 1; |
---|
25 | END; |
---|
26 | OutText( "cons stops" ); OutImage; |
---|
27 | END; |
---|
28 | INTEGER PROCEDURE delivery( p1p, p2p ); |
---|
29 | INTEGER p1p, p2p; |
---|
30 | BEGIN |
---|
31 | p1 := p1p; |
---|
32 | p2 := p2p; |
---|
33 | Resume( THIS Consumer ); |
---|
34 | delivery := status; |
---|
35 | END; |
---|
36 | PROCEDURE stop; |
---|
37 | BEGIN |
---|
38 | done := TRUE; |
---|
39 | Call( THIS Consumer ); |
---|
40 | END; |
---|
41 | ! Consumer constructor code; |
---|
42 | status := 0; |
---|
43 | done := FALSE; |
---|
44 | Detach; |
---|
45 | main; |
---|
46 | END Consumer; |
---|
47 | |
---|
48 | CLASS Producer; |
---|
49 | HIDDEN cons, N, money, receipt, Main; |
---|
50 | BEGIN |
---|
51 | REF(Consumer) cons; |
---|
52 | INTEGER N, money, receipt; |
---|
53 | PROCEDURE main; |
---|
54 | BEGIN |
---|
55 | INTEGER i, p1, p2, status; |
---|
56 | |
---|
57 | FOR i := 1 STEP 1 UNTIL N DO BEGIN |
---|
58 | p1 := RandInt( 1, 100, p1 ); |
---|
59 | p2 := RandInt( 1, 100, p2 ); |
---|
60 | OutText( "prod delivers: " ); |
---|
61 | OutInt( p1, 3 ); OutText( ", " ); |
---|
62 | OutInt( p2, 3 ); OutImage; |
---|
63 | status := cons.delivery( p1, p2 ); |
---|
64 | OutText( "prod status: " ); |
---|
65 | OutInt( status, 3 ); OutImage; |
---|
66 | END; |
---|
67 | cons.stop; |
---|
68 | OutText( "prod stops" ); OutImage; |
---|
69 | END; |
---|
70 | INTEGER PROCEDURE payment( moneyp ); |
---|
71 | INTEGER moneyp; |
---|
72 | BEGIN |
---|
73 | money := moneyp; |
---|
74 | OutText( "prod payment of $" ); |
---|
75 | OutInt( money, 3 ); OutImage; |
---|
76 | Resume( THIS Producer ); |
---|
77 | receipt := receipt + 1; |
---|
78 | payment := receipt; |
---|
79 | END; |
---|
80 | PROCEDURE start( Np, consp ); |
---|
81 | INTEGER Np; |
---|
82 | REF(Consumer) consp; |
---|
83 | BEGIN |
---|
84 | N := Np; |
---|
85 | cons :- consp; |
---|
86 | Resume( THIS Producer ); |
---|
87 | END; |
---|
88 | ! Producer constructor code; |
---|
89 | receipt := 0; |
---|
90 | Detach; |
---|
91 | main; |
---|
92 | END Producer; |
---|
93 | ! program main equivalent; |
---|
94 | REF(Producer) prod; |
---|
95 | REF(Consumer) cons; |
---|
96 | prod :- NEW Producer; |
---|
97 | cons :- NEW Consumer( prod ); |
---|
98 | prod.start( 5, cons ); |
---|
99 | END; |
---|
100 | |
---|
101 | ! Local Variables: ; |
---|
102 | ! tab-width: 4 ; |
---|
103 | ! compile-command: "cim ProdCons.sim" ; |
---|
104 | ! End: ; |
---|