-- pragma Ada_2012;
with ada.text_io;
procedure colors is
use ada.text_io; -- Use can be in declaration section
-- Enumeration type definition
type Color is (red, blue, green, yellow, orange, black, purple);
type Days is (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
subtype Week_Days is Days range Monday .. Friday;
-- Create (at compile time) a new package for IO of colors
-- Uses generic package Enumeration_IO in Text_IO.
-- Red is a NEWly created LITERAL in the language
-- A literal is a sequence of characters that represents itself
-- Examples: 5, true, 2.1, true, 'a', "A"
-- vs Expressions. Examples: 2+3, 2<3, 1+1.1, not false, 'a', "A"& "B"
package Color_IO is new Ada.Text_IO.Enumeration_IO(Color);
use Color_IO; -- We
package Days_IO is new Ada.Text_IO.Enumeration_IO(Days);
use Days_IO; -- We
-- c: Color := Monday; -- Doesn't work: strongly typed
-- c: Color := 1; -- Compile error
c: Color;
begin
put("Enter your favorite color: ");
Color_IO.get(c);
put("Your favorite color is " );
put(c);
new_line;
put("The next color is" & Color'Succ(c)'img );
new_line;
put_line("I like " & c'img & "too!");
if c = red then
put("You're a red kind of person");
end if;
-- if c > Color'Val(Color'Pos(Color'First) + Color'Size/2)
-- if c > Color'Val(Color'Pos(Color'First) +
-- (Color'Pos(Color'Last) - Color'Pos(Color'First))/2)
if c > yellow
then
put("You're a large color type of person");
else
put("You're a small color type of person");
end if;
exception
when Data_Error =>
put_line("Invalid Color! Valid colors are:");
for somecolor in Color loop
-- for somecolor in Color'Range loop
put_line(Color'Image(somecolor));
end loop;
end colors;