Consider now our Point of Sale example. We now know how to declare new types, use control structures and write subprograms this is enough to start considering some of the code we might want to write.
Initially we will need to declare some global types, required by our whole project, such as:
type Quantity is new Positive; type Currency is delta0.01 digits 10; type Weight is delta 0.001 digits 6; type Short_Description is new String(1 .. 18); type Full_Description is new String(1 .. 75);
And some global constants:
System_Name : constant String := "Ada Store"; System_Version : constant String := "GENERIC_1.0.0";
Now we can try some more complicated types, structures with actual business meaning, departments items and tenders. The following are structures for departments and items, each item contains a reference to the department it is in (which manages its price range and tax rate for example).
-- department information. declare type Department_Identifier is new Positive range 1 .. 50; type Instance is record Department_Number : Department_Identifier; Description : Short_Description; Min_Price : Currency := 0.00; Max_Price : Currency := 0.00; Tax_Rate : Percentage := 0.00; end record; -- item information. declare type Item_Identifier is new Positive; type Instance is record Item_Number : Item_Identifier; In_Department : Department_Identifier; Display_Descr : Short_Description; Price : Currency; Weight_Required : Boolean; end record;
The next type, a representation of a tender type (type of payment i.e. cash, card) is more complete and even presents its set of operations. Such business objects are usually held in a database, hence the Lookup and Remove subprograms deal with identifiers rather than objects and why the Create_New subprogram is required, it inserts the object in the database.
-- tender information. declare type Tender_Identifier is new Positive range 1 .. 20; type Tender_Type_Instance is record Tender_ID : Tender_Identifier; Description : Short_Description; Min_Amount : Currency := 0.00; Max_Amount : Currency := 0.00; end record; function Lookup (Tender_ID : in Tender_Identifier) return Tender_Type_Instance; procedure Create_New (Tender_ID : in Tender_Identifier; Description : in Short_Description; Min_Amount : in Currency; Max_Amount : in Currency); procedure Remove (Tender_ID : in Tender_Identifier); function Authorise (Tender : in Tender_Type_Instance; Amount : in Currency) return Boolean; function Description (Tender : in Tender_Type_Instance) return Short_Description; Invalid_Identifier : exception;
Items, departments and other database held objects would follow a similar pattern with Create_New, Lookup and Remove subprograms.
What we cannot do so far is package this code in a form that the compiler can truly accept. The next chapter will add a little more to our understanding by introducing Ada packages and describing the Ada compilation model.
Copyright ©
1996 Simon Johnston &
Addison Wesley Longman