Previous topic | Ada Home Page | Index

Array of Records - Example

Problem

A system is needed that will allow a car rental company to record details of its cars.

The program will allow the information to be updated as cars are hired out and returned.

The following information is required for each car:

Program Design


Code

-----------------------------------------------------------
--  dealer.ada - car hire firm car list program
--
--  Skansholm p 326-328
--  adapted by: Lawrie Brown / 9 Aug 94
-----------------------------------------------------------

with TEXT_IO;

procedure dealer is

    -- specify I/O libraries required
    package INT_IO is new TEXT_IO.INTEGER_IO (INTEGER);
    use TEXT_IO, INT_IO;

    -- define various types used
    type Year_type is range 1900 .. 2000;
    type Weight_type is range 100 .. 10000;  -- in kg
    type Power_type is digits 4;             -- in kW

    -- record used to store client details
    type Person is record
        name :           String(1..20);
        client_number :  String(1..10);
        address :        String(1..30);
    end record;

    -- record used to store info on cars
    type Car_type is record
        reg_number : String(1..6);
        make       : String(1..20);
        model_year : Year_type;
        weight     : Weight_type;
        power      : Power_type;
        client     : Person;
    end record;

    -- array used for cars database
    type Car_list is array (POSITIVE range <>) of Car_type;

    -- database of cars owned by dealer
    cars_owned : Car_list(1..5) := (
       ("YPC123", "Subaru DL wagon     ", 1990, 1800, 70.0,
          ("John Smith          ", "smithj    ",
           "123 Apple St, Dunlop ACT 2907 ") ),
       ("YPC145", "Subaru DL wagon     ", 1991, 1800, 70.0,
          ("Mary Jones          ", "jonesm    ",
           "45 Banana St, Dunlop ACT 2907 ") ),
       ("YPC268", "Mazda 828           ", 1989, 2400, 75.0,
          ("                    ", "          ",
           "                              ") ),
       ("YNZ843", "Mazda 828           ", 1990, 2400, 75.0,
          ("Jill Gordan         ", "gordanj   ",
           "192 Apple St, Dunlop ACT 2907 ") ),
       ("YPR826", "Holden Commodore    ", 1992, 3000, 120.0,
          ("                    ", "          ",
           "                              ") ) );

    -- some useful constants
    nobody : constant Person := (name => (others=>' '),
                           client_number => (others=>' '),
                           address => (others=>' '));

    -- global variables used by dealer
    rego : String(1..6);      -- rego number of desired car
    p    : NATURAL;           -- index into database of cars



    ------------------------------------------------------
    --  search_car - search cars database for desired rego
    ------------------------------------------------------
    function search_car(c:Car_list; rego:String)
                     return NATURAL is

    begin -- search_car
        -- now look for the car with wanted rego
        for n in c'RANGE loop
            if c(n).reg_number = rego then
                return n;                -- gotcha
            end if;
        end loop;
        return 0;                        -- out of luck
    end search_car;

    ------------------------------------------------------
    -- hire_car - fill in client details for hired car
    ------------------------------------------------------
    procedure hire_car(acar: in out Car_type) is
        c : CHARACTER; -- yes/no response
        l : INTEGER;   -- length of string entered

    begin -- hire_car
        PUT("Is this car being hired? ");
        GET(c); SKIP_LINE;

        if ((c = 'y') or (c = 'Y')) then
            acar.client := nobody;  -- clear client details
            PUT("Enter clients name: ");
            GET_LINE(acar.client.name, l);

            PUT("Enter clients number: ");
            GET_LINE(acar.client.client_number, l);

            PUT("Enter clients address: ");
            GET_LINE(acar.client.address, l);
        end if;
    end hire_car;

    ------------------------------------------------------
    -- return_car - delete client details for returned car
    ------------------------------------------------------
    procedure return_car(acar: in out Car_type) is
        c : CHARACTER; -- yes/no response
        l : INTEGER;   -- length of string entered

    begin -- return_car
        PUT("Is this car being returned? ");
        GET(c); SKIP_LINE;

        if ((c = 'y') or (c = 'Y')) then
            acar.client := nobody;  -- erase client details
        end if;
    end return_car;

-----------------------------------------------------------
begin -- dealer

    loop    -- keep processing requests
        PUT("Enter the registration number (or exit): ");
        GET(rego); SKIP_LINE;

        exit when rego = "exit  ";    -- all done

        p := search_car(cars_owned, rego); -- find car

        -- respond appropriately
        if p = 0 then
           PUT_LINE("No car with this registration number");
        elsif cars_owned(p).client = nobody then
            PUT_LINE("Car is not hired out");
            hire_car( cars_owned(p) );
        else
            PUT_LINE("Car is hired out to:");
            PUT_LINE(cars_owned(p).client.name);
            PUT_LINE(cars_owned(p).client.address);
            return_car( cars_owned(p) );
        end if;
    end loop;
end dealer;


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 27 Feb 96