4A.x Data items forsøg



+++alternativ, da pi ikke har så meget med bank at gøre:

Before introducing classes we described a specific account object in this way:

Account_1010: obj
   owner: val "John Smith"
   balance: var real 

As this is the description of a specific account for a specific person, the owner is specified to be a value (val) of type String, withe the value “John Smith” i.e. a data item that cannot change value. +++ slipper pi, men må bruge String.

When explaining subclasses we introduced special kind of account, a savings account, represented the class SavingsAccount as a subclass of Account. In addition the release date property introduced before, savings accounts often comes with a limit on how many times it s allowed to withdraw during a month or year. To simplify the example we assume that the maximum number of times applies to the life time of the account:

class SavingsAccount: Account
   releaseDate: var Date
   maxNumberOfWithdraws: var Integer
   noOfWithdraws: var Integer
   setMax(var m: Integer):
      maxNumberOfWithdraws := m
   withdraw(amount: var Real) -> real:
      if ((today > releaseDate) or
         (noOfWithdraws < maxNumberOfWithdraws)) :then
         balance := balance - amount
         noOfWithdraws:= noOfWithdraws + 1
      :else
         console.print("It is not possible to withdraw")
      return balance
   ...

noOfWithdraws is an example of a variable data-item that represents an integer value property, the number of times withDraw has been called.

As for Real, an Integer may as well have been declared as a constant, in case it is the same value for all savings accounts:

maxNumberOfWithdraws: val 1024

We may like or not, but banks often have customers that they prioritize, with the implication that other customers are not prioritized. This is readily represented by a data item of type Boolean:

class Customer(name: var String):
   addr: var String         
   email: var String 
   priorityCustomer: var Boolean

A data-item of type Boolean and may have the value True or False. The priorityCustomer data item is a variable that may hold the values True or False at different point during the program execution.

A custemer become a priority customer by performing the following assignment:

   priorityCustomer := True

If the priorityCustomer had been declared like this:

   priorityCustomer: val False

then a customer would never be able to become a priority customer, as it will not be possible to change the value of priorityCustomer.

class Customer(name: var String):
   addr: var String         
   email: var String 
   customerLevel: var Char

Probably you would not have both priorityCustomer and customerLevel, but if you had this would be reflection in this setting of customerLevel:

 if (priorityCustomer) :then
       customerLevel := 'A'
    :else 
       customerLevel := 'B'