| Previous | Table of Contents | Next |
Finally, lines 45-61 close the input file and then loop through the summary array to display the results. The formatting parameters Fore and Aft (line 59) for Ada.Float_Text_IO.Put indicate the desired number of displayed places before and after the decimal point. A non-zero Exp calls for the number to be displayed in E-notation and gives the number of places in the exponent. Thus Fore =>1, Aft=>2, Exp=>2 displays -2065.00 as -2.07E+3. This is fine for an engineering application but not for data processing.
10.4.1.11. Exception Handling, Command Parameters, and Information Systems Features
The last inner syntax example, Show_Exception, is a modification of Gadget_Report that illustrates exception handling, the decimal-types and edit-directed-output features of the Ada 95 Information Systems Annex (Annex F), and the standard package Ada.Command_Line. For the input file today.dat as used previously, issuing the operating system command
show_exception today.dat
produces the output
show_exception: Input file is today.dat Gadget Type Transactions Value -------------------------------- RED 0 $0.00 BLUE 1 $25.00 GREEN 2 $2,134.00 YELLOW 0 $0.00 BLACK 1 $2,065.00CR
The command show_exception produces the output
show_exception: No input file provided.
A transaction containing bad data is ignored (in a real application, it would be written to an error report); for the file bad.dat containing a misspelled gadget type (gren),
green 56.00 blue 25.00 gren 20.00 black -20.00
the command show_exception bad.dat results in
show_exception: Input file is bad.dat
Ignoring Bad Transaction
Gadget Type Transactions Value
--------------------------------
RED 0 $0.00
BLUE 1 $25.00
GREEN 1 $56.00
YELLOW 0 $0.00
BLACK 1 $20.00CR
1 with Ada.Text_IO, Ada.Text_IO.Editing;
2 with Ada.Integer_Text_IO;
3 with Ada.Command_Line;
4 use Ada.Text_IO, Ada.Integer_Text_IO;
5 procedure Show_Exception is
6
7 type Money is delta 0.01 digits 12;
8 package Money_In is
9 new Ada.Text_IO.Decimal_IO(Num => Money);
10 package Money_Out is new
11 Ada.Text_IO.Editing.Decimal_Output (Num => Money);
12
13 type Gadgets is (Red, Blue, Green, Yellow, Black);
14 package Gadget_IO is
15 new Ada.Text_IO.Enumeration_IO (Enum => Gadgets);
16
17 type Transaction is record
18 Kind: Gadgets;
19 Amount: Money := 0.00;
20 end record;
21
22 type Database is
23 array(Integer range <>) of Transaction;
24
25 type SummaryEntry is record
26 HowMany: Natural := 0;
27 Net : Money := 0.00;
28 end record;
29
30 type Summary is array(Gadgets) of SummaryEntry;
31
32 TodaysActivity: Database(1..100);
33 TodaysSummary : Summary;
34
35 InputData: Ada.Text_IO.File_Type;
36
37 TransactionCount: Natural := 0;
38 WhichKind: Gadgets;
39
40 begin
41
42 Put(Item => Ada.Command_Line.Command_Name);
43
44 if Ada.Command_Line.Argument_Count = 0 then
45 Put_Line (Item => : No input file provided.);
46 return;
47 else
48 Put_Line(Item => : Input file is
49 & Ada.Command_Line.Argument(Number => 1));
50 end if;
51
52 Open(File => InputData,
53 Mode => Ada.Text_IO.In_File,
54 Name => Ada.Command_Line.Argument(Number => 1));
55
56 while not End_of_File(File => InputData) loop
57 TransactionCount := TransactionCount + 1;
58
59 begin
60 Gadget_IO.Get(File => InputData,
61 Item => TodaysActivity(TransactionCount).Kind);
62 Money_In.Get (File => InputData,
63 Item=>TodaysActivity(TransactionCount).Amount);
64 Skip_line(File => InputData);
65 exception
66 when Constraint_Error =>
67 Put_Line(Item =>
68 Out of range amount; ignoring transaction);
69 Skip_Line(File => InputData);
70 TransactionCount := TransactionCount - 1;
71 when Ada.Text_IO.Data_Error =>
72 Put_Line(Item =>
73 Bad gadget or amount; ignoring transaction);
74 Skip_Line(File => InputData);
75 TransactionCount := TransactionCount - 1;
76 end;
77 end loop;
78
79 Ada.Text_IO.Close (File => InputData);
80
81 for Count in 1..TransactionCount loop
82 WhichKind := TodaysActivity(Count).Kind;
83 TodaysSummary(WhichKind).HowMany :=
84 TodaysSummary(WhichKind).HowMany + 1;
85 TodaysSummary(WhichKind).Net :=
86 TodaysSummary(WhichKind).Net
87 + TodaysActivity(Count).Amount;
88 end loop;
89
90 Ada.Text_IO.Put_Line
91 (Item => Gadget Type Transactions Value);
92 Ada.Text_IO.Put_Line
93 (Item => --------------------------------);
94
95 for WhichKind in Gadgets loop
96 Gadget_IO.Put(Item => WhichKind, Width => 7);
97 Put(Item => TodaysSummary(WhichKind).HowMany,
98 Width => 12);
99 Money_Out.Put(Item => TodaysSummary(WhichKind).Net,
100 Pic => Ada.Text_IO.Editing.To_Picture
101 ($$_$$$_$$9.99CR));
102 Ada.Text_IO.New_Line;
103 end loop;
104
105 exception
106
107 when Ada.Text_IO.Name_Error =>
108 Put_Line (Item => File Not Found, Goodbye.);
109
110 end Show_Exception;
The context clause in line 1 mentions Ada.Text_IO.Editing. This Annex F package provides for COBOL or PL/I-style edit-directed output, which is widely used in business reports. The context clause in line 3 mentions Ada.Command_Line, which provides a platform-independent equivalent of Cs argc/argv capability. Using this package, a program can read its own command-line name as well as any other command-line parameters the user has supplied.
| Previous | Table of Contents | Next |