indexing description: "A concrete state: card in machine" class StateCard inherit AbstractState redefine abort, finish, get_card, get_money end creation make feature make (ma: Machine; c: Card) is do mach := ma; my_card := c; money := 0; end; feature {NONE} mach: Machine; money: integer; -- "Temporary money buffer" my_card: Card; -- Eject the card (without writing to it) and change the state to "no card" eject is local new_state: StateEmpty do io.put_string ("Ejecting card (value = "); io.put_integer (my_card.money); io.put_string (").%N"); !! new_state.make (mach); mach.set_state (new_state); end; -- Write to the card write_card (m: integer) is do io.put_string ("Writing card (new value is "); io.put_integer (m); io.put_string (").%N"); my_card.set_money (m); end; keep_money is do io.put_string ("Keeping money: "); io.put_integer (money); io.new_line; money := 0; end; accept_money (m: integer) is require m <= my_card.max_money - (my_card.money + money); do money := money + m; end; reject_money (m: integer) is do io.put_string ("Too much money, rejecting "); io.put_integer (m); io.put_string (".%N"); end; give_back_money is do io.put_string ("Giving back money: "); io.put_integer (money); io.new_line; money := 0; end; do_abort is do give_back_money; eject; end; feature {ANY} show_state is do io.put_string ("state: card!%N"); io.put_string (" money on card: "); io.put_integer (my_card.money); io.new_line; io.put_string (" max money on card: "); io.put_integer (my_card.max_money); io.new_line; io.put_string (" money in machine: "); io.put_integer (money); io.new_line; end; abort is do do_abort; end; finish is do write_card (my_card.money + money); keep_money; eject; end; insert_card (c: Card): Boolean is do io.put_string ("ERROR: New card while card is already in machine%N"); io.put_string ("==> PANIC ABORT"); do_abort; Result := False; end; add_money (m: integer) is do if m > my_card.max_money - (money + my_card.money) then reject_money (m); else accept_money (m); end; end; get_card: Card is do Result := my_card; end; get_money: Integer is do Result := money; end; end -- class StateCard