Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,231 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,854 people online right now. Registration is fast and FREE... Join Now!




Searching an Employees Created using Accessor Methods

 
Reply to this topicStart new topic

Searching an Employees Created using Accessor Methods

bravos
12 Mar, 2007 - 07:29 AM
Post #1

New D.I.C Head
*

Joined: 11 Oct, 2006
Posts: 14


My Contributions
I have created 5 employees with attributes. The user will enter an employee identification number. After that I need to have that number checked against the employees I've created to see if it exists. I can't figure this out and would appreciate some help.

CODE
using System;
using System.Collections.Generic;
using System.Text;

namespace Assign_4_Employee_Info
{
    public class EmployeeCreator
    {
            //Member Variables
            private string employeeIdentificationNumber;
            private string firstName;
            private string middleInitial;
            private string lastName;
            private string address;
            private string city;
            private string state;
            private string zipCode;
            private double baseSalaryLevel;
            static public int numberOfInstances;

            //Property of the baseSalaryLevel data field
            public double baseSalary
            {
                get
                {
                    return baseSalaryLevel;
                }
                set
                {
                    baseSalaryLevel = value;
                }
            }

            public string employeeNumber
            {
                get
                {
                    return employeeIdentificationNumber;
                }
                set
                {
                    employeeIdentificationNumber = value;
                }
            }


            //Mutator Method
            public void SetFirstName(string fName)
            {
                firstName = fName;
            }

            //Mutator Method
            public void SetMiddleInitial(string mInitial)
            {
                middleInitial = mInitial;
            }

            //Mutator Method
            public void SetLastName(string lName)
            {
                lastName = lName;
            }

            //Mutator Method
            public void SetAddress(string eAddress)
            {
                address = eAddress;
            }

            //Mutator Method
            public void SetCity(string eCity)
            {
                city = eCity;
            }

            //Mutator Method
            public void SetState(string eState)
            {
                state = eState;
            }

            //Mutator Method
            public void SetZipCode(string eZipCode)
            {
                zipCode = eZipCode;
            }

            //Mutator Method
            //public void SetIdentificationNumber(string eIdentification)
            //{
            //    employeeIdentificationNumber = eIdentification;
            //}

            //Accessor Method
            public string GetIdentificationNumber()
            {              
                return employeeIdentificationNumber;              
            }

            //Accessor Method
            public string GetFirstName()
            {
                return firstName;
            }

            //Accessor Method
            public string GetMiddleInitial()
            {
                return middleInitial;
            }

            //Accessor Method
            public string GetLastName()
            {
                return lastName;
            }

            //Accessor Method
            public string GetAddress()
            {
                return address;
            }
            //Accessor Method
            public string GetCity()
            {
                return city;
            }

            //Accessor Method
            public string GetState()
            {
                return state;
            }

            //Accessor Method
            public string GetZipCode()
            {
                return zipCode;
            }

            //Default Constructor
            public EmployeeCreator()
            { }

            //Overload Constructor
            public EmployeeCreator(string idNumber, double salary)
            {
                employeeIdentificationNumber = idNumber;
                baseSalaryLevel = salary;
            }

            //Overload Constructor
            //public EmployeeCreator(int idNumber, string fName, string mInitial, string lName)
            //{
            //    employeeIdentificationNumber = idNumber;
            //    firstName = fName;
            //    middleInitial = mInitial;
            //    lastName = lName;
            //}

      }
}


CODE
using System;
using System.Collections.Generic;
using System.Text;

namespace Assign_4_Employee_Info
{
    public class EmployeeMain
    {
        //Static Method
        static void DisplayOriginalValues(EmployeeCreator Employee, int number)
        {
            string dIdentificationNumber;
            string dName, dFirstName, dMiddleInitial, dLastName, dAddress,
                dCity, dState, dZipCode, dLocation;
            double dBaseSalary = 0;

            dIdentificationNumber = Employee.GetIdentificationNumber();
            dFirstName = Employee.GetFirstName();
            dMiddleInitial = Employee.GetMiddleInitial();
            dLastName = Employee.GetLastName();
            dAddress = Employee.GetAddress();
            dCity = Employee.GetCity();
            dState = Employee.GetState();
            dZipCode = Employee.GetZipCode();
            dBaseSalary = Employee.baseSalary;

            dName = dFirstName + " " + dMiddleInitial + ". " + dLastName;
            dLocation = dCity + ", " + dState + " " + dZipCode;

            Console.WriteLine("Employee Number <{0}>:  {1}  ==> {2}", number, dIdentificationNumber, dName);
          
        }

        static void CheckIdentificationNumber(EmployeeCreator Employee, int number)
        {
            Console.Write("Enter the Employee Number ");
            string eNumber = Console.ReadLine();            
            string employeeNum = Employee.employeeNumber;
            
            for (int i = 0; i <= number; i++)
            {                
                if (eNumber == employeeNum)
                {
                    Console.WriteLine("You Selected Employee =====> {0} {1} {2}", Employee.GetFirstName(), Employee.GetMiddleInitial(), Employee.GetLastName());
                    Console.WriteLine("Residing at ===============> {0}", Employee.GetAddress());
                    Console.WriteLine("                             {0} {1} {2}", Employee.GetCity(), Employee.GetState(), Employee.GetZipCode());
                    Console.WriteLine("With a Base Salary of =====> {0}", Employee.baseSalary);
                }
                else
                {
                    Console.WriteLine("There are No Employees with this Idenfication Number");
                }
            }            
        }
      
        static void Main(string[] args)
        {
            string mFirstName;
            string mMiddleInitial;
            string mLastName;
            string mAddress;
            string mCity;
            string mState;
            string mZipCode;
            //string eNumber;
            int iInst;

            EmployeeCreator Employee01 = new EmployeeCreator("10001", 50000);
            Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            mFirstName = "Fred";
            Employee01.SetFirstName(mFirstName);
            mMiddleInitial = "R";
            Employee01.SetMiddleInitial(mMiddleInitial);
            mLastName = "Flintstone";
            Employee01.SetLastName(mLastName);
            mAddress = "1 Cobble Stone Square";
            Employee01.SetAddress(mAddress);
            mCity = "Bedrock";
            Employee01.SetCity(mCity);
            mState = "CO";
            Employee01.SetState(mState);
            mZipCode = "00001-0001";
            Employee01.SetZipCode(mZipCode);
            iInst = Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            DisplayOriginalValues(Employee01, iInst);

            EmployeeCreator Employee02 = new EmployeeCreator("10021", 48500);
            //Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            mFirstName = "LeRoy";
            Employee02.SetFirstName(mFirstName);
            mMiddleInitial = "P";
            Employee02.SetMiddleInitial(mMiddleInitial);
            mLastName = "Thompson";
            Employee02.SetLastName(mLastName);
            mAddress = "12 N. Congress Ave.";
            Employee02.SetAddress(mAddress);
            mCity = "Greensboro";
            Employee02.SetCity(mCity);
            mState = "NC";
            Employee02.SetState(mState);
            mZipCode = "10001-1001";
            Employee02.SetZipCode(mZipCode);
            iInst = Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            DisplayOriginalValues(Employee02, iInst);

            EmployeeCreator Employee03 = new EmployeeCreator("10031", 45500);
            //Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            mFirstName = "George";
            Employee03.SetFirstName(mFirstName);
            mMiddleInitial = "B";
            Employee03.SetMiddleInitial(mMiddleInitial);
            mLastName = "Pennington";
            Employee03.SetLastName(mLastName);
            mAddress = "121 W. Vermont St.";
            Employee03.SetAddress(mAddress);
            mCity = "Columbus";
            Employee03.SetCity(mCity);
            mState = "OH";
            Employee03.SetState(mState);
            mZipCode = "10021-1021";
            Employee03.SetZipCode(mZipCode);
            iInst = Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            DisplayOriginalValues(Employee03, iInst);

            EmployeeCreator Employee04 = new EmployeeCreator("10041", 43500);
            //Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            mFirstName = "Larry";
            Employee04.SetFirstName(mFirstName);
            mMiddleInitial = "J";
            Employee04.SetMiddleInitial(mMiddleInitial);
            mLastName = "Workstone";
            Employee04.SetLastName(mLastName);
            mAddress = "1212 E. Sawgrass Dr.";
            Employee04.SetAddress(mAddress);
            mCity = "Seatle";
            Employee04.SetCity(mCity);
            mState = "WA";
            Employee04.SetState(mState);
            mZipCode = "12101-1211";
            Employee04.SetZipCode(mZipCode);
            iInst = Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            DisplayOriginalValues(Employee04, iInst);

            EmployeeCreator Employee05 = new EmployeeCreator("10051", 40500);
            //Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            mFirstName = "Elaine";
            Employee05.SetFirstName(mFirstName);
            mMiddleInitial = "C";
            Employee05.SetMiddleInitial(mMiddleInitial);
            mLastName = "Washington";
            Employee05.SetLastName(mLastName);
            mAddress = "1421 S. Cranberry Ave.";
            Employee05.SetAddress(mAddress);
            mCity = "Boston";
            Employee05.SetCity(mCity);
            mState = "MA";
            Employee05.SetState(mState);
            mZipCode = "10031-1031";
            Employee05.SetZipCode(mZipCode);
            iInst = Assign_4_Employee_Info.EmployeeCreator.numberOfInstances++;
            DisplayOriginalValues(Employee05, iInst);

            EmployeeCreator EmployeeTemp = new EmployeeCreator();  
            CheckIdentificationNumber(EmployeeTemp, iInst);            
        }
    }
}

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Searching An Employees Created Using Accessor Methods
12 Mar, 2007 - 10:40 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
CODE


static void CheckIdentificationNumber(EmployeeCreator Employee, int number)
        {
            Console.Write("Enter the Employee Number ");
            string eNumber = Console.ReadLine();            
            string employeeNum = Employee.employeeNumber;
            
            for (int i = 0; i <= number; i++)
            {                
                if (eNumber == employeeNum)
                {
                    Console.WriteLine("You Selected Employee =====> {0} {1} {2}", Employee.GetFirstName(), Employee.GetMiddleInitial(), Employee.GetLastName());
                    Console.WriteLine("Residing at ===============> {0}", Employee.GetAddress());
                    Console.WriteLine("                             {0} {1} {2}", Employee.GetCity(), Employee.GetState(), Employee.GetZipCode());
                    Console.WriteLine("With a Base Salary of =====> {0}", Employee.baseSalary);
                }
                else
                {
                    Console.WriteLine("There are No Employees with this Idenfication Number");
                }
            }            




well your problem is that you are sending number of instances of employee but sending only one of them [that too uninitialized one!!!].
so you are comparing same instance for number of times and as it is not initialized to any value it does not give you expected output.

Solution is to make array of EmployeeCreator Objects in main and initailize them one by one. Then send this array to the above function.
your static variable called "noOfInstances" is public so no need to send it.

now in above function you have an array of objects from which you can traverse and find the thing you want.

and one suggestion, if you have accessor and mutator methods with you then please use them for data manipuation, don't make that data public it removes the perpose of OOP.

hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:23PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month