1

I have been trying to figure out how to make the postcondition work for the following code. There are 3 classes, Bank is the client of Customer, and Customer is the client of Account

Here is the Bank Class, I just cant pass the postcondition other_customer_unchanged

    new (name1: STRING)
        -- Add a new customer named 'name1'
        -- to the end of list `customers'
    require
        customer_not_already_present:customer_exists(name1)=false

    do
        customers.force (create {CUSTOMER}.make (name1))
        count := customers.count

    ensure
        total_balance_unchanged:
            sum_of_all_balances = old sum_of_all_balances
        num_customers_increased:count /= old count and old count+1=count
        total_unchanged:total = old total
        customer_added_to_list:
            customer_exists (name1)
            and then customers[customer_id (name1)].name ~ name1
            and then customers[customer_id (name1)].balance ~ zero
        other_customers_unchanged:
            customers_unchanged_other_than(name1, old customers.deep_twin)
    end

Here is the feature of customers_unchanged_other_than

customers_unchanged_other_than (a_name: STRING;old_customers:like customers): BOOLEAN
        -- Are customers other than `a_name' unchanged?
    local
        c_name: STRING
    do
        from
            Result := true
            customers.start
        until
            customers.after or not Result
        loop
            c_name := customers.item.name
            if c_name /~ a_name then
                Result := Result and then
                    old_customers.has (customers.item)
            end
            customers.forth
        end
    ensure
        Result =
            across
                customers as c
            all
                c.item.name /~ a_name IMPLIES
                    old_customers.has (c.item)
            end
    end

and I have redefined the is_equal feature in customer class

is_equal (other: like Current): BOOLEAN
    do
        Result := name ~ other.name and balance = other.balance
    ensure then
        Result = (name ~ other.name and balance = other.balance)
    end

I have looked into what is in the old customer.deep_twin, it does contain the item of the customer, but somehow when it uses .has feature, it just makes the Result false. Any help is greatly appreciated :)

swordgit
  • 115
  • 10

1 Answers1

1

I presume from your code that customers and old_customers are of type descendant of CONTAINER (ARRAY, LIST, STACK, QUEUE, etc.) You can then use customers.compare_objects (or old_customers.compare_objects) to ask the CONTAINER to use is_equal instead of "=" when searching.

Louis M
  • 576
  • 2
  • 4
  • Yes customers and old_customers are of type LIST. Do you mean changing old_customers.has to old_customers.compare_objects? – swordgit Jan 26 '16 at 04:16