In this section, we will walk through the process of building a complete Ada project from start to finish. This will include setting up the project structure, writing the code, compiling, and running the project. By the end of this section, you should have a solid understanding of how to manage and develop a full Ada application.

Project Overview

We will create a simple library management system. The system will allow users to:

  • Add new books to the library.
  • List all books in the library.
  • Search for a book by title.
  • Remove a book from the library.

Project Structure

A well-organized project structure is crucial for maintaining and scaling your application. Here is the recommended structure for our Ada project:

LibraryManagement/
├── src/
│   ├── main.adb
│   ├── book.ads
│   ├── book.adb
│   ├── library.ads
│   ├── library.adb
├── obj/
├── bin/
├── Makefile
  • src/: Contains all the source code files.
  • obj/: Contains the compiled object files.
  • bin/: Contains the final executable.
  • Makefile: Automates the build process.

Step-by-Step Guide

  1. Setting Up the Project

Create the project directory and subdirectories:

mkdir -p LibraryManagement/src LibraryManagement/obj LibraryManagement/bin
cd LibraryManagement

  1. Writing the Code

2.1. Book Package

Create src/book.ads:

-- book.ads
package Book is
    type Book_Type is record
        Title  : String (1 .. 100);
        Author : String (1 .. 100);
        ISBN   : String (1 .. 13);
    end record;

    procedure Initialize (B : out Book_Type; Title, Author, ISBN : String);
    procedure Display (B : in Book_Type);
end Book;

Create src/book.adb:

-- book.adb
package body Book is
    procedure Initialize (B : out Book_Type; Title, Author, ISBN : String) is
    begin
        B.Title := Title;
        B.Author := Author;
        B.ISBN := ISBN;
    end Initialize;

    procedure Display (B : in Book_Type) is
    begin
        Put_Line ("Title: " & B.Title);
        Put_Line ("Author: " & B.Author);
        Put_Line ("ISBN: " & B.ISBN);
    end Display;
end Book;

2.2. Library Package

Create src/library.ads:

-- library.ads
with Book;

package Library is
    type Library_Type is array (1 .. 100) of Book.Book_Type;
    procedure Add_Book (Lib : in out Library_Type; B : Book.Book_Type);
    procedure List_Books (Lib : in Library_Type);
    function Search_Book (Lib : in Library_Type; Title : String) return Integer;
    procedure Remove_Book (Lib : in out Library_Type; Title : String);
end Library;

Create src/library.adb:

-- library.adb
with Ada.Text_IO; use Ada.Text_IO;
with Book;

package body Library is
    procedure Add_Book (Lib : in out Library_Type; B : Book.Book_Type) is
    begin
        for I in Lib'Range loop
            if Lib(I).Title = "" then
                Lib(I) := B;
                exit;
            end if;
        end loop;
    end Add_Book;

    procedure List_Books (Lib : in Library_Type) is
    begin
        for I in Lib'Range loop
            if Lib(I).Title /= "" then
                Book.Display(Lib(I));
                New_Line;
            end if;
        end loop;
    end List_Books;

    function Search_Book (Lib : in Library_Type; Title : String) return Integer is
    begin
        for I in Lib'Range loop
            if Lib(I).Title = Title then
                return I;
            end if;
        end loop;
        return 0;
    end Search_Book;

    procedure Remove_Book (Lib : in out Library_Type; Title : String) is
    begin
        for I in Lib'Range loop
            if Lib(I).Title = Title then
                Lib(I).Title := "";
                Lib(I).Author := "";
                Lib(I).ISBN := "";
                exit;
            end if;
        end loop;
    end Remove_Book;
end Library;

2.3. Main Program

Create src/main.adb:

-- main.adb
with Ada.Text_IO; use Ada.Text_IO;
with Book;
with Library;

procedure Main is
    Lib : Library.Library_Type;
    B   : Book.Book_Type;
begin
    -- Initialize the library
    for I in Lib'Range loop
        Lib(I).Title := "";
    end loop;

    -- Add a book
    Book.Initialize(B, "The Pragmatic Programmer", "Andrew Hunt", "978-0201616224");
    Library.Add_Book(Lib, B);

    -- List all books
    Put_Line("Listing all books:");
    Library.List_Books(Lib);

    -- Search for a book
    Put_Line("Searching for 'The Pragmatic Programmer':");
    if Library.Search_Book(Lib, "The Pragmatic Programmer") /= 0 then
        Put_Line("Book found!");
    else
        Put_Line("Book not found.");
    end if;

    -- Remove a book
    Library.Remove_Book(Lib, "The Pragmatic Programmer");

    -- List all books again
    Put_Line("Listing all books after removal:");
    Library.List_Books(Lib);
end Main;

  1. Compiling the Project

Create a Makefile in the root directory:

# Makefile
GPRBUILD = gprbuild
GPRCLEAN = gprclean
PROJECT = LibraryManagement

all: build

build:
	$(GPRBUILD) -P $(PROJECT).gpr

clean:
	$(GPRCLEAN) -P $(PROJECT).gpr

Create a project file LibraryManagement.gpr:

project LibraryManagement is
   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Main use ("main.adb");
   for Exec_Dir use "bin";
end LibraryManagement;

  1. Running the Project

To build and run the project, use the following commands:

make
./bin/main

Conclusion

In this section, we have built a complete Ada project from scratch. We covered setting up the project structure, writing the code for different packages, compiling the project, and running the final executable. This should give you a solid foundation for managing and developing Ada applications.

© Copyright 2024. All rights reserved