Part 8: Arrays

 
 

8.1   Specification

        The bank can have many customers. A customer accesses their account through an ATM (Automatic Teller Machine). The machineprocesses customer input until a special exit code of 666 is entered into the ATM.
 
 

8.2   Analysis

        A particular customer now has to be found from the set of customers. The ATM has to read a valid identifier, find that customer, and transfer control to that customer.

        The system runs until the special end of system sentinel (666) is input to the ATM. The loop code will thus look something like

               from
               until system_exit
               loop
                       read_id
                       if valid_id
                       then process_customer
                       else say_error
                       end
               end
 
 

8.3   Design

        The customers are implemented as an array, so the index of the customer in the array can be used as a unique key.
 
 

8.4   Charts

        The client chart for the system is shown below. The bank creates and uses an array of customers. A customer uses a password, and their account via the menu.

The class diagrams for all classes in the system are unchanged.
 
 

8.5   Solution code

        The full class listing for BANK is shown on the next page. No other classes are changed.

class BANK

creation {ANY}
        make

feature{NONE}
        patrons: ARRAY [CUSTOMER]
        count: INTEGER
        end_id: INTEGER is 666

feature {ANY}
        make is
                        -- make the customers, run the atm system, add interest
               do
                       welcome
                       !!patrons.make (1, 100)
                       add_patrons
                       run_atm
                       add_interest
                       io.new_line
               end -- make

feature {NONE}
        welcome is
                        -- welcome the user
               do
                       io.putstring("%N******************************************%
                       %*************")
                       io.putstring ("%N* Welcome to myBank,%
                       % where your money is my money *")
                       io.putstring("%N******************************************%
                       %*************")
               end -- welcome

        add_patrons is
                        -- add customers until the user says to stop
               local patron: CUSTOMER
               do
                       from ask_for_more
                       until no_more
                       loop
                                !!patron.make
                                count := count + 1
                                patrons.put (patron, count)
                                ask_for_more
                       end
               end -- add_patrons

        ask_for_more is
                        -- ask if their are more customers to add, read reply
               do
                       io.putstring ("%NNew customers (Y/N) ? ")
                       io.readchar
                       io.next_line
               end -- ask_for_more

        no_more: BOOLEAN is
                        -- did the user say no more?
               do
                       Result := io.lastchar.upper = 'N'
               end -- no_more

        run_atm is
                        -- run the ATM and teller until system is shut down
               do
                       show_atm_header
                       from read_id
                       until end_input
                       loop
                               if valid_id
                               then patrons.item (io.lastint).login
                               else io.putstring ("%TInvalid customer id. Try again.%N%N")
                               end
                               read_id
                       end
               end -- run_atm

        show_atm_header is
                        -- show start of atm system
               do
                       io.putstring ("%N************************************")
                       io.putstring ("%N* ATM system operational *")
                       io.putstring ("%N************************************%N%N")
               end -- show_atm_header

        read_id is
                        -- ask for a user id, read reply
               do
                       io.putstring ("%TEnter customer id: ")
                       io.readint
               end -- read_id

        end_input: BOOLEAN is
                        -- was the end id input?
               do Result := io.lastint = end_id end -- end_input

        valid_id: BOOLEAN is
                        -- is the input id a valid customer index?
               do Result := io.lastint <= count end -- valid_id

        add_interest is
                        -- add interest for every customer
               local i: INTEGER
               do
                       from i := 1
                       until i > count or else patrons.item (i) = Void
                       loop
                               patrons.item (i).account.add_interest
                               i := i + 1
                       end
               end -- add_interest

end -- class BANK       end
               end -- add_interest

end -- class BANK