with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Containers.Vectors; use Ada.Containers;
with Ada.Finalization; use Ada.Finalization;
procedure jdoodle is
type Phone_Number is range 0 .. 9_999_999_999; -- 10‑digit numbers only
type Phone_Entry is record
Name : Unbounded_String;
Number : Phone_Number;
end record;
package Entry_Vectors is new Ada.Containers.Vectors
(Index_Type => Positive,
Element_Type => Phone_Entry);
use Entry_Vectors;
type Safe_Entry_List is new Controlled with record
List : Entry_Vectors.Vector;
end record;
overriding procedure Finalize (Obj : in out Safe_Entry_List) is
begin
Clear (Obj.List);
end Finalize;
Phone_Book_Entries : Safe_Entry_List;
function Get_Line return Unbounded_String is
Line : String (1 .. 200);
Last : Natural;
begin
Get_Line (Line, Last);
return To_Unbounded_String (Line (1 .. Last));
end Get_Line;
procedure Add_Entry (Name : Unbounded_String; Number : Phone_Number) is
begin
Append (Phone_Book_Entries.List,
Phone_Entry'(Name => Name, Number => Number));
end Add_Entry;
procedure Show_All is
begin
Put_Line ("--- Phone Book ---");
for C of Phone_Book_Entries.List loop
Put_Line (To_String (C.Name) & " : " &
Phone_Number'Image (C.Number));
end loop;
end Show_All;
begin
Put_Line ("Enter three contacts (name + 10‑digit number).");
for I in 1 .. 3 loop
Put ("Name: ");
declare
Name_In : constant Unbounded_String := Get_Line;
Number_Str : String(1..20);
Last : Natural;
begin
Put ("Number (0..9999999999): ");
Get_Line(Number_Str, Last);
declare
Num_In : Phone_Number := Phone_Number'Value(Number_Str(1..Last));
begin
Add_Entry (Name_In, Num_In);
exception
when Constraint_Error =>
Put_Line ("Number out of range – entry skipped.");
end;
end;
end loop;
New_Line;
Show_All;
end jdoodle;