% "cargen.tu"
% The module CarGen defines the types and constants used
% for generating unique cars, and provides a single subprogram,
% generateCar that does the generation.


unit
module CarGen

    import Car in "car.tu"
    export generateCar


    % The range of car manufacture years that can be generated.
    const lowestYear := 1970
    const highestYear := 1997

    % The number of different car makes and colours that can be generated.
    const numMakes := 16
    const numColours := 10

    % The list of different car makes and colours that can be generated.
    const makeList : array 1 .. numMakes of string :=
        init ("Yugo", "Ferrari", "Taurus", "Pinto", "BMW", "Escort",
        "Camry", "VW Bug", "Volvo", "Citroen", "Neon", "Stanza",
        "Pathfinder", "Caravan", "Sunbird", "Porsche")

    const colourList : array 1 .. numColours of string :=
        init ("Black", "Red", "Yellow", "Orange", "Green", "White", "Taupe",
        "Persimmon", "Plaid", "Eggplant")

    % The ID number to assign to the next car generated.
    % The first car will get ID number 1.
    %     As with students, generating the car ID numbers in sequence is
    % adequate for the simulation.
    var nextIDNum := 1



    % randomMake
    %----------------------------------------------------------------
    % Return a random car make from the list of possibilities.

    function randomMake : string
        var i : int
        randint (i, 1, numMakes)
        result makeList (i)
    end randomMake



    % randomColour
    %----------------------------------------------------------------
    % Return a random car colour from the list of possibilities.

    function randomColour : string
        var i : int
        randint (i, 1, numColours)
        result colourList (i)
    end randomColour



    % randomYear
    %----------------------------------------------------------------
    % Return a random year from the list of possibilities.

    function randomYear : int
        var i : int
        randint (i, lowestYear, highestYear)
        result i
    end randomYear



    % generateID
    %----------------------------------------------------------------
    % Return a unique car ID number.

    function generateID : int
        const answer := nextIDNum
        nextIDNum += 1
        result answer
    end generateID



    % generateCar
    %----------------------------------------------------------------
    % Create and return a random car.

    function generateCar (currentTime : int) : Car.carType
        var newCar : Car.carType
        Car.setMake (newCar, randomMake)
        Car.setColour (newCar, randomColour)
        Car.setYear (newCar, randomYear)
        Car.setID (newCar, generateID)
        Car.setTimeArrived (newCar, currentTime)

        result newCar
    end generateCar

end CarGen
