Question

In: Computer Science

Create a class of DSA with two pointer objects of Employee and Customer. These objects will...

Create a class of DSA with two pointer objects of Employee and Customer. These objects will represent the head pointer of corresponding linkedlists.

Add new data member functions searchCustomer & searchEmployee, to search for customers and employees separately in DSA.

Solutions

Expert Solution

There is a small confusion if your question, you had not specified in which language you need the code, so I had done in cpp, comment below, if you required in another languale, I will modify the answer based on that.

Code :

#include <iostream>
#include <string>
using namespace std;

class Employee
{
    public:
        string name;
        int id;
        Employee *next;
        Employee(const string &newName, int a)
        {
            name = newName;
            id = a;
            next = 0;
        }
        void print() const
        {
            cout << "Name: " << name << ", id: " << id << endl;
        }

        ~Employee()
        {
            cout << "Destructor of Employee" << endl;
        }
};
Employee *head = NULL;

class Customer
{
    public:
        string name;
        int id;
        Customer *next;
        Customer(const string &newName, int a)
        {
            name = newName;
            id = a;
            next = 0;
        }
        void print() const
        {
            cout << "Customer Name: " << name << ", Customer id: " << id << endl;
        }

        ~Customer()
        {
            cout << "Destructor of Customer" << endl;
        }
};
Customer *headOfCustomer = NULL;

bool InsertEmployee(const string &n, int a)
{
    Employee *previous, *current, *newEmployee;
    newEmployee = new Employee(n, a);
    if (!newEmployee)
        return false;

    if (head)
    {
        previous = head;
        current = head->next;
        while (current)
        {
            previous = current;
            current = current->next;
        }
        previous->next = newEmployee;
    }
    else
    {
        head = newEmployee;
    }

    return true;
}
bool InsertCustomer(const string &n, int a)
{
    Customer *previous, *current, *newCustomer;
    newCustomer = new Customer(n, a);
    if (!newCustomer)
        return false;

    if (headOfCustomer)
    {
        previous = headOfCustomer;
        current = headOfCustomer->next;
        while (current)
        {
            previous = current;
            current = current->next;
        }
        previous->next = newCustomer;
    }
    else
    {
        headOfCustomer = newCustomer;
    }

    return true;
}

void printEmployee()
{
    Employee *tempPtr;
    if (head)
    {
        tempPtr = head;
        while (tempPtr)
        {
            tempPtr->print();
            tempPtr = tempPtr->next;
        }
    }
    else
    {
        cout << "The list is empty" << endl;
    }
}

void printCustomer()
{
    Customer *tempPtr;
    if (headOfCustomer)
    {
        tempPtr = headOfCustomer;
        while (tempPtr)
        {
            tempPtr->print();
            tempPtr = tempPtr->next;
        }
    }
    else
    {
        cout << "The list is empty" << endl;
    }
}

void searchEmployee(int id)
{
    Employee *tempPtr;
    if (head)
    {
        tempPtr = head;
        while (tempPtr)
        {
            if(tempPtr->id == id)
            {
                cout<<"The employee found";
                return;
            }
            tempPtr = tempPtr->next;
        }
        cout<<"Employee not found in the linked list";
    }
    else
    {
        cout << "The list is empty" << endl;
    }
}
void searchCustomer(int id)
{
    Customer *tempPtr;
    if (headOfCustomer)
    {
        tempPtr = headOfCustomer;
        while (tempPtr)
        {
            if(tempPtr->id == id)
            {
                cout<<"The Customer found";
                return;
            }
            tempPtr = tempPtr->next;
        }
        cout<<"Customer not found in the linked list";
    }
    else
    {
        cout << "The list is empty" << endl;
    }
}
int main()
{

    int option = 1;
    string name;
    int id;
    // InsertEmployee("Employee 1", 100);
    // InsertEmployee("Employee 2", 200);
    // InsertEmployee("Employee 3", 300);
    // printEmployee();
    while(option != 0 )
    {
        cout<<"\n0. Exit";
        cout<<"\n1. Add Employee";
        cout<<"\n2. Seach Employee";
        cout<<"\n3. Display all Employee";
        cout<<"\n4. Add Customer";
        cout<<"\n5. Search Customer";
        cout<<"\n6. Display all Customer";
        cout<<"\nEnter your option : ";
        cin>>option;
        switch(option)
        {
            case 1: // Add employee
                    cout<<"Enter employee name : ";
                    cin>>name;
                    cout<<"Enter employee ID : ";
                    cin>>id;
                    if(InsertEmployee(name,id))
                    {
                        cout<<"Successfully inserted";
                    }
                    break;
            case 2: // Search employee
                    cout<<"Enter employee ID to search : ";
                    cin>>id;
                    searchEmployee(id);
                    break;
            case 3: // Display all  employee
                    printEmployee();
                    break;
            case 4: // Add customer
                    cout<<"Enter customer name : ";
                    cin>>name;
                    cout<<"Enter customer ID : ";
                    cin>>id;
                    if(InsertCustomer(name,id))
                    {
                        cout<<"Successfully inserted";
                    }
                    break;
            case 5: // Search customer
                    cout<<"Enter customer ID to search : ";
                    cin>>id;
                    searchCustomer(id);
                    break;
            case 6: // Display all  customer
                    printCustomer();
                    break;
        }
    }

    return 0;
}

Screenshot of a part of output :


Related Solutions

java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs....
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside...
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside of the ServerClass class, that: 1. Takes the IP 2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128) 3. Returns the Server object with the larger sum Example: server_one = ServerClass("127.0.0.1") server_two = ServerClass("192.168.0.1") result = biggest_ip_sum(server_one, server_two) print(result) Hint Modify get_server_ip in the previous problem. -------------------- Please use this code to start class ServerClass: """ Server class for...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
Create a windows application that contains two TextBox objects and two Button objects. One of the...
Create a windows application that contains two TextBox objects and two Button objects. One of the TextBox objects and one of the buttons are initially invisible. The first textbox should be used to input a password. The textbox should be masked to some character of your choice so that the characters entered by the user are not seen on the screen. When the user clicks the first button, the second TextBox object and button object should be displayed with a...
Make Animal an abstract class. Create a Kennel Class Create 2-3 Dog objects Create 2-3 Cat...
Make Animal an abstract class. Create a Kennel Class Create 2-3 Dog objects Create 2-3 Cat objects Put your Dog and Cat objects in an Array of Animals Loop over your Animals and print the animal with Species, Age, and the status of the appropriate vaccines. Using the code below: public class Animal { //Declaring instance variables private int age; private boolean RabiesVaccinationStatus; private String name; private String ownerName; //Zero argumented constructor public Animal() { } //Parameterized constructor public Animal(int...
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Part I: The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class.
  Part I:   The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class. * Update the function display() to be a virtual function     * Add function, double weeklyEarning() to be a pure virtual function. This will make Employee an abstract class, so your main will not be able to create any Employee objects. Part II: The Derived Classes Add an weeklyEarning() function to each...
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT