Monday 6 June 2016

Haskell: Design Employee utility for an organization

Assume, you are developing an application for company ABC. Following are the requirements.
a.   There are three kinds of employees: Engineer, Manager, Director
b.   Each employee is identified by unique id
c.    Each employee must have firstName, lastName and gender
d.   Each manager maintains list of engineer ids
e.   Each director maintains list of manager ids

You need to provide following functionality.

a.   Get employee firstName, lastName
b.   Get all the engineer ids associated with a manager
c.    Get all manager ids associated with a director
d.   Get gender of an employee


ABC.hs
module ABC where
    type FirstName = String
    type LastName = String
    type NoOfReportees = Integer
    type EngineerId = Integer
    type ManagerId = Integer
    type DirectorId = Integer

    {- Define an employee type -}
    data Employee = Engineer Person EngineerId
                   | Manager Person [EngineerId] ManagerId
                   | Director Person [ManagerId] DirectorId
                   deriving Show

    data Person = Person FirstName LastName Gender deriving Show

    data Gender = Male | Female deriving Show

    getFirstName :: Person -> FirstName
    getFirstName (Person firstName lastName gender) = firstName

    getLastName :: Person -> LastName
    getLastName (Person firstName lastName gender) = lastName

    getGender :: Person -> Gender
    getGender (Person firstName lastName gender) = gender

    getEngineerIds :: Employee -> [EngineerId]
    getEngineerIds (Manager person ids managerId) = ids

    getManagerIds :: Employee -> [ManagerId]
    getManagerIds(Director person ids directorId) = ids

    getEmployeeFirstName :: Employee -> FirstName
    getEmployeeFirstName emp =
        case emp of
            Engineer person _ -> getFirstName person
            Manager person _ _ -> getFirstName person
            Director person _ _ -> getFirstName person

    getEmployeeLastName :: Employee -> LastName
    getEmployeeLastName emp =
        case emp of
            Engineer person _ -> getLastName person
            Manager person _ _ -> getLastName person
            Director person _ _ -> getLastName person

Prelude> :load ABC.hs
[1 of 1] Compiling ABC              ( ABC.hs, interpreted )
Ok, modules loaded: ABC.
*ABC> 
*ABC> let person1 = Person "Hari Krishna" "Gurram" Male
*ABC> let person2 = Person "PTR" "Nayan" Female
*ABC> let person3 = Person "Siri valli" "Bem" Female
*ABC> 
*ABC> let engineer1 = Engineer person1 123
*ABC> let engineer2 = Manager person2 [123, 432, 34, 5] 234
*ABC> let engineer3 = Director person3 [234, 543, 678, 987] 345
*ABC> 
*ABC> getEmployeeFirstName engineer1
"Hari Krishna"
*ABC> 
*ABC> getEmployeeLastName engineer1
"Gurram"
*ABC> 
*ABC> getEngineerIds engineer2
[123,432,34,5]
*ABC> 
*ABC> getManagerIds engineer3
[234,543,678,987]


Previous                                                 Next                                                 Home

No comments:

Post a Comment