CS4150 Assignemt 4 Ada Book

There are 4 files:
1) assign4.gpr
2) books.adb
3) books.ads
4) test_book.adb


Source Code:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
assign4.gpr
----------------------------------------------------------------------------------------------------------------------------------

project Assign4 is

   for Source_Files use ("books.adb", "books.ads", "test_book.adb");
   for Main use ("test_book.adb");

end Assign4;
---------------------------------------------------------------------------------------------------------------------------------


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
books.adb
---------------------------------------------------------------------------------------------------------------------------------
with Ada.Characters.Handling;
use Ada.Characters.Handling;

package body Books is

   -----------------
   -- create_book --
   -----------------

   function create_book
     (author: in String;
      title: in String;
      leaves: in PAGES)
      return Book
   is
      boo : Book;
   begin
      if author'Length > MAX_AUTHOR_NAME_SIZE then
         boo.author := author(1 .. MAX_AUTHOR_NAME_SIZE);
      else
         boo.author (1 .. author'Length) := author;
      end if;
      if title'Length > MAX_TITLE_SIZE then
         boo.title := title(1 .. MAX_TITLE_SIZE);
      else
         boo.title (1 .. title'Length) := title;
      end if;

        boo.leaves := leaves;
      return boo;
   end create_book;

   ----------------
   -- get_author --
   ----------------

   function get_author (s: in Book) return String is
   begin
      return s.author;
   end get_author;

   ---------------
   -- get_title --
   ---------------

   function get_title (s: in Book) return String is
   begin
      return s.title;
   end get_title;

   ---------------
   -- get_pages --
   ---------------

   function get_pages (s: in Book) return PAGES is
   begin
      return s.leaves;
   end get_pages;

end Books;
------------------------------------------------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
books.ads
------------------------------------------------------------------------------------------------------------------------------------
package Books is

   type Book is private;

   MAX_AUTHOR_NAME_SIZE: constant Positive := 30;

   MAX_TITLE_SIZE: constant Positive := 100;

   MAX_PAGES_SIZE: constant Positive := 10000;

   type PAGES is new Positive range 1 .. MAX_PAGES_SIZE;

   function create_book (author: in String; title: in String; leaves: in PAGES)
                         return Book
   with pre => author'Length > 0 and title'Length > 0;

   function get_author (s: in Book) return String;

   function get_title (s: in Book) return String;

   function get_pages (s: in Book) return PAGES;

private
   type Book is
      record
         author: String (1 .. MAX_AUTHOR_NAME_SIZE) := (others => ' ');
         title: String (1 .. MAX_TITLE_SIZE) := (others => ' ');
         leaves: PAGES;
      end record;

end Books;
-------------------------------------------------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
test_book.adb
-------------------------------------------------------------------------------------------------------------------------------------
with Books, Ada.Text_IO;
use Books, Ada.Text_IO;

procedure Test_Book is

   boo: Book;

   procedure display_book (s: in Book) is

   begin
      put ("Author: ");
      put_line (get_author (s));
      put ("Title: ");
      put_line (get_title (s));
      put ("Pages: ");
      put_line (Positive'Image (Positive(get_pages(s))));
   end display_book;

begin
   boo :=create_book("Dalibor Labudovic","The Best Book Ever",10);
   display_book(boo);
end Test_Book;

Popular posts from this blog

CS3401 Practice Quiz 2 Part 2

CS3401 Practice Quiz 2 Part 1