with Ada.Strings.Bounded;

package Bookstore.Books is

   ----------------------------------------------------------------------
5   -- type Book                                                        --
   --                                                                  --
   -- The only kind of items available in our example.                 --
   -- To get more items, we have to define an root type and extend it. --
   ----------------------------------------------------------------------
10
   type Book is tagged private;

   type Book_Array is array (Natural range <>) of Book;

15   ----------------------------------------------------------------------
   -- used types                                                       --
   --                                                                  --
   -- the following constant integer values come from the JEE tutorial --
   --  SQL files                                                       --
20   ----------------------------------------------------------------------

   Id_Str_Length          : constant Integer := 8;
   Description_Str_Length : constant Integer := 30;
   Name_Str_Length        : constant Integer := 24;
25   Title_Str_Length       : constant Integer := 96;

   package Id_Strings is
      new Ada.Strings.Bounded.Generic_Bounded_Length (Id_Str_Length);
   use Id_Strings;
30   package Description_Strings is
      new Ada.Strings.Bounded.Generic_Bounded_Length (Description_Str_Length);
   use Description_Strings;
   package Name_Strings is
      new Ada.Strings.Bounded.Generic_Bounded_Length (Name_Str_Length);
35   use Name_Strings;
   package Title_Strings is
      new Ada.Strings.Bounded.Generic_Bounded_Length (Title_Str_Length);
   use Title_Strings;

40   subtype Id_Str          is Id_Strings.Bounded_String;
   subtype Description_Str is Description_Strings.Bounded_String;
   subtype Name_Str        is Name_Strings.Bounded_String;
   subtype Title_Str       is Title_Strings.Bounded_String;

45   type Value is delta 0.01 digits 8 range 0.0 .. 100000.0;

   type Year           is new Integer range -4000 .. 4000;
   type Inventory_Size is new Natural;

50   -----------------
   -- Create_Book --
   -----------------

   function Create_Book
55     (ID            : in Id_Str;
      Surname       : in Name_Str;
      First_Name    : in Name_Str;
      Title         : in Title_Str;
      Price         : in Value;
60      On_Sale       : in Boolean;
      Calendar_Year : in Year;
      Description   : in Description_Str;
      Inventory     : in Inventory_Size)
     return Book;
65
   -------------------------
   -- Set_xyz and Get_xyz --
   -------------------------

70   procedure Set_ID            (B             : in out Book;
                                ID            : in     Id_Str);
   procedure Set_Description   (B             : in out Book;
                                Description   : in     Description_Str);
   procedure Set_Price         (B             : in out Book;
75                                Price         : in     Value);
   procedure Set_Inventory     (B             : in out Book;
                                Inventory     : in     Inventory_Size);
   procedure Set_First_Name    (B             : in out Book;
                                First_Name    : in     Name_Str);
80   procedure Set_Surname       (B             : in out Book;
                                Surname       : in     Name_Str);
   procedure Set_Title         (B             : in out Book;
                                Title         : in     Title_Str);
   procedure Set_Calendar_Year (B             : in out Book;
85                                Calendar_Year : in     Year);
   procedure Set_On_Sale       (B             : in out Book;
                                On_Sale       : in     Boolean);

   function Get_ID            (B : in Book) return Id_Str;
90   function Get_Description   (B : in Book) return Description_Str;
   function Get_Price         (B : in Book) return Value;
   function Get_Inventory     (B : in Book) return Inventory_Size;
   function Get_First_Name    (B : in Book) return Name_Str;
   function Get_Surname       (B : in Book) return Name_Str;
95   function Get_Title         (B : in Book) return Title_Str;
   function Get_Calendar_Year (B : in Book) return Year;
   function Is_On_Sale        (B : in Book) return Boolean;

   ---------------
100   -- To_String --
   ---------------

   function To_String (B : in Book) return String;

105   ---------------------------------------------------------------
   -- package Strings                                           --
   --  defines renames to the different 'to_bounded_string' and --
   --  'to_string' functions of the previous instantiations of  --
   -- Ada.Strings.Bounded.Generic_Bounded_Length                --
110   ---------------------------------------------------------------

   package Strings is
      function To_ID
        (Source : String; Drop : Ada.Strings.Truncation := Ada.Strings.Error)
115        return Id_Strings.Bounded_String
        renames Id_Strings.To_Bounded_String;
      function To_Name
        (Source : String; Drop : Ada.Strings.Truncation := Ada.Strings.Error)
        return Name_Strings.Bounded_String
120        renames Name_Strings.To_Bounded_String;
      function To_Title
        (Source : String; Drop : Ada.Strings.Truncation := Ada.Strings.Error)
        return Title_Strings.Bounded_String
        renames Title_Strings.To_Bounded_String;
125      function To_Description
        (Source : String; Drop : Ada.Strings.Truncation := Ada.Strings.Error)
        return Description_Strings.Bounded_String
        renames Description_Strings.To_Bounded_String;

130      function To_String
        (Source : Id_Strings.Bounded_String) return String
        renames Id_Strings.To_String;
      function To_String
        (Source : Name_Strings.Bounded_String) return String
135        renames Name_Strings.To_String;
      function To_String
        (Source : Title_Strings.Bounded_String) return String
        renames Title_Strings.To_String;
      function To_String
140        (Source : Description_Strings.Bounded_String) return String
        renames Description_Strings.To_String;
   end Strings;

private
145
   type Book is tagged
      record
         ID            : Id_Str;
         Surname       : Name_Str;
150         First_Name    : Name_Str;
         Price         : Value;
         Title         : Title_Str;
         On_Sale       : Boolean;
         Calendar_Year : Year;
155         Description   : Description_Str;
         Inventory     : Inventory_Size;
      end record;

end Bookstore.Books;
160



top