home | hometown | racing |recent |resume |schedule |photos


b a c k

Declarations

Function Query

Procedure Add

Procedure Delete

Procedure PrepairFile

Procedure Print

Procedure Quit

Main

 

catalog.adb: Here is an example of Ada programming that I completed in a data structures class.

with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Sequential_IO;
with Vstrings;
with VStrings.IO;
procedure Catalog is
--------------------------------------------
--Author: William Martin
--Last modified: 11/02/99
--
--This program creates and holds 
--data for music collections.
--
--CSC223
---------------------------------------------

   Type MusicType is (Rock, Classical, Country, Jazz, Blues,
                      Alternative, Q); --catagories of                      
                                       --music
   Type QueryType is (Add, Delete, Print, Quit); --functions          
                                                 --available to user 
   MaxLinelength: Constant Positive := 35;
   Type Recording is                    --one record/one cd
      record
         Name  : VStrings.VString(MaxLength => MaxLineLength);
         Group : VStrings.VString(MaxLength => MaxLineLength);
         Genre : MusicType := Q;
   end record;
  Subtype CatalogRange is Integer range 1..500;--available number of 
                                               --records
   Type RecordingsList is Array (CatalogRange) of Recording;
   Package Record_IO     is new Ada.Sequential_IO (element_Type =>
                                Recording);
   Package Query_IO      is new Ada.Text_IO.Enumeration_IO
                                (Enum => QueryType);
   Package MusicType_IO  is new Ada.Text_IO.Enumeration_IO
                                (Enum => MusicType);
   RecordFile : Record_IO.File_Type;
---------------------------------------------------------------------
      Procedure PrepairFile (Collection : Out RecordingsList;
                             Size       : Out Integer) is
                                    --this procedure prepares a file  
                                    --for input from user
         Index      : Integer;      --item number(key) for recording
         Selection  : Recording;    -- 
      begin
         Record_IO.Open (File => RecordFile,  --data file
                       Mode => Record_IO.In_File,
                       Name => "record.dat");
         Index := 0;                   --this loop reads from the 
         loop                          --file and collects input 
                                       --from the user. 
            exit when Record_IO.End_Of_File(RecordFile);
            Record_IO.Read (File => RecordFile,
                            Item => Selection);
            if Selection.Genre /= Q then
               Index := Index + 1;
               Collection(Index) := Selection;
            end if;
         end loop;
         Size := Index;
      Exception            --handels wrong/nonexistant input of 
                           --file name
         when Record_IO.NAME_ERROR =>
            Ada.Text_IO.Put_Line ("Inventory File Does Not Exist....");
            Ada.Text_IO.Put_Line ("Creating record.dat!");
            Ada.Text_IO.New_Line (5);
            Record_IO.Create (File => RecordFile,
                            Mode => Record_IO.In_File,
                            Name => "record.dat");
         Size := 0;
      end PrepairFile;
---------------------------------------------------------------------
---------------------------------------------------------------------
      Function Query return QueryType is   --this function determines
                                           --which operation the user 
                                           --would like to perform.
   Selection : QueryType;
      begin
         Ada.Text_IO.Put_Line ("----------------------------------" &
                               "-----");
         Ada.Text_IO.Put_Line ("Please type one of the selections " &
                               "below");
         Ada.Text_IO.Put_Line ("----------------------------------" &
                               "-----");
         Ada.Text_IO.Put_Line ("   Add      - Add New Recording");
         Ada.Text_IO.Put_Line ("   Delete   - Delete A Recording");
         Ada.Text_IO.Put_Line ("   Print    - Print List Of Recordings");
         Ada.Text_IO.Put_Line ("   Quit     - Quit Program");
         Ada.Text_IO.Put_Line ("----------------------------------" &
                               "-----");
         loop         --this loop get the desired function from the user
            Ada.Text_IO.Put ("ENTER SELECTION => ");
            Validation:
            begin
               Query_IO.Get (Selection);
               exit;
            exception
               when Ada.Text_IO.DATA_ERROR =>
                  Ada.Text_IO.Put_Line ("Invalid Selection, Please Try" &
                                        " Again");
                  Ada.Text_IO.Skip_Line;
            end Validation;
         end loop;
         return Selection;
      end Query;
---------------------------------------------------------------------
---------------------------------------------------------------------
      Procedure Add (Collection : In Out RecordingsList; --this proc.  
                     Size       : In Out Integer) is     --adds an 
                                                         --element 
                                                         --to the data.
      Selection     : Recording;       --one new element 
      begin
         Ada.text_IO.Skip_Line;
         Ada.Text_IO.Put ("Enter New Title => ");
         VStrings.IO.Get_Line (Item      => Selection.Name,
                               MaxLength => MaxLineLength);
         Ada.Text_IO.Put ("Enter Name Of Artists => ");
         VStrings.IO.Get_Line (Item      => Selection.Group,
                               MaxLength => MaxLineLength);
         Ada.Text_IO.Put_Line ("Enter Genre. Your selections are:");
         Ada.Text_IO.Put_Line (" Rock, Classical, Country, Jazz, ");
         Ada.Text_IO.Put_Line (" Blues, Alternative");
         loop
            Ada.Text_IO.Put ("Enter Genre => ");
            Validation:      --this loop valadates the user input.
            Begin
               MusicType_IO.Get (Item => Selection.Genre);
               Ada.Text_IO.Skip_Line;
               exit;
            exception
               when Ada.Text_IO.DATA_ERROR =>
                  Ada.Text_IO.Put_Line ("Invalid Selection! Please " &
                                        "Try Again!");
                   Ada.Text_IO.Skip_Line;
             end Validation;
          end loop;
          Size := Size + 1;
          Collection(size) := Selection;
      end Add;
---------------------------------------------------------------------
---------------------------------------------------------------------
      Procedure Print (Collection : In RecordingsList;--prints list
                       Size       : In Integer) is    --of current 
                                                      --elements
         Answer : Character;
      begin
         Ada.Text_IO.New_Line (2);
         Ada.Text_IO.Put_Line ("----------------------------------");
         Ada.Text_IO.Put_Line ("             TITLE");
         Ada.Text_IO.Put_Line ("----------------------------------");
         for Index in 1..Size loop         --loop outputs data to screen
            Ada.Integer_Text_IO.Put (Index);
            Ada.Text_IO.Put (". ");
            VStrings.IO.Put (Collection(Index).Name);
            Ada.Text_IO.New_Line;
         end loop;
         Ada.Text_IO.Put_Line ("----------------------------------");
         Ada.Text_IO.Put_Line ("Press any key then ENTER to return");
         Ada.Text_IO.Put_Line ("       to the main menu");
         Ada.Text_IO.Get (Answer);
      end Print;
---------------------------------------------------------------------
---------------------------------------------------------------------
      Procedure Delete (Collection : In Out RecordingsList;
                        Size       : In Out Integer) is --Deletes one 
                                                        --element from 
                                                        --data
      use VStrings;
         Index        : Integer;            --number of items
         Success      : Boolean;            --determines success of delete
         ItemToDelete : Recording;          --requested item to be deleted
    begin
         Ada.Text_IO.Put ("Enter Item To Delete => ");
         Ada.Text_IO.Skip_Line;
         VStrings.IO.Get_Line (Item      => ItemToDelete.Name,
                               MaxLength => MaxLineLength);
         Index   := 1;
         Success := False;
         loop                           --this loop finds and 
                                        --deletes item
           if Collection(Index).Name = ItemToDelete.Name then
             Success := True;
             loop
               Collection(Index) := Collection(Index+1);
               Index := Index +1;
               exit when Index >= Size;
             end loop;
           end if;
           exit when Index >= Size or Success = True;
           Index := Index+1;
         end loop;
         if Success = True then
           Size := Size - 1;
         else
           Ada.Text_IO.Put ("Can not find the title named ");
           VStrings.IO.Put (ItemToDelete.Name);
           Ada.Text_IO.Put_Line (". Try viewing the list and check" &
                                 " your spelling");
         end if;
      end Delete;
---------------------------------------------------------------------
---------------------------------------------------------------------
      Procedure Quit (Collection : In RecordingsList; --function exits
                      Size       : In Integer;        --from program
                      Quitting   : Out Boolean) is
         Answer    : Character;            --user response 
         Selection : Recording;            --element from data
      begin
         Ada.Text_IO.New_Line (2);
         Ada.Text_IO.Put_Line ("Would you like to Save your changes?");
         Ada.Text_IO.Put ("PUT ANSWER (Y or N)=> ");
         loop      --loop determines if user wishes to exit the program
            Ada.text_IO.Get (Answer);
            if Answer = 'Y' or Answer = 'y' then
               Record_IO.Delete (RecordFile);
               Record_IO.Create (File => RecordFile,
                                 Mode => Record_IO.Out_File,
                                 Name => "record.dat");
               for Index in 1..Size loop
                  Selection := Collection(Index);
                  Record_IO.Write (File => RecordFile,
                                   Item => Selection);
               end loop;
               Record_IO.Close (RecordFile);
               exit;
            elsif Answer = 'N' or Answer = 'n' then
               Record_IO.Close (RecordFile);
               exit;
            else
               Ada.Text_IO.Put_Line("Wrong Answer! Please answer with" &
                                    " Y or N.");
               Ada.Text_IO.Skip_Line;
            end if;
         end loop;
         Quitting := True;
      end Quit;
---------------------------------------------------------------------
   FileSize       : Integer;            --current number of items
   MyList         : RecordingsList;     --the current list of items
   QuitProgram    : Boolean;            --response to quit program function
   begin    --main program
   QuitProgram := False;
   FileSize    := 0;
   PrepairFile (MyList, FileSize);
   loop         --main program loop takes user input and executes function
      case Query is
         when Add =>
            Add (MyList, FileSize);
         when Delete =>
            Delete (MyList, FileSize);
         when Print =>
            Print (MyList, FileSize);
         when Quit =>
            Quit(MyList, FileSize, QuitProgram);
      end case;
      Ada.Text_IO.Put_Line ("----------------------------------" &
                            "-----");
      Ada.Text_IO.Put_Line ("Proccessing.......................");
      Ada.Text_IO.Put_Line ("----------------------------------" &
                            "-----");
      Ada.text_IO.New_Line (2);
      if QuitProgram = True then
         exit;
      end if;
   end loop;
end Catalog;    --end of program

[ t o p ]


© Copyright 2001 William L. Martin All rights reserved.