Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your job is to finish the partially constructed Agent class. More specifically,

ID: 3738546 • Letter: Y

Question

Your job is to finish the partially constructed Agent class. More specifically, you need to write the following three Agent methods

public bool AddClient(Client c)

public Client FindClient(string clientNameValue)

public bool RemoveClient(string clientNameValue)

The AddClient() method adds a new client object to the agent's list and dictionary (provided that it is not in there already). It returns true if the insertion takes place and false otherwise.

The FindClient() method must use the dictionary to locate a client whose name is provided in the call. If the client is found it returns a reference to the corresponding Client object. Otherwise, it returns null.

The RemoveClient() method deletes an existing client from the agent's list and dictionary. If the given client is not associated to the agent (not found in her group) then the method returns false.

    class Agent

    {

        //(fields) class members

        private string agentName;

        private List<Client> clientList;

        private Dictionary<string, Client> clientDictionary;

        //Property (only access to agent's name is used)

        //--------------------------------------------------------------------

        //NOTE: The company has decided that all direct traffic to clientList,

        //and clientDictionary should be blocked. That is why they do not

        //have associated Properties and do appear in the Constructors.

        //--------------------------------------------------------------------

        public string AgentName

        {

            get { return agentName; }

            set { agentName = value; }

        }

        //zero-arguments constructor

        public Agent()

        {

            agentName = "na";

            clientList = new List<Client>();

            clientDictionary = new Dictionary<string, Client>();

        }

        //one-argument constructor (see note above)

        public Agent(string agentNameValue)

        {

            agentName = agentNameValue;

            clientList = new List<Client>();

            clientDictionary = new Dictionary<string, Client>();

        }

        //Methods

        public override string ToString()

      {

            StringBuilder result = new StringBuilder("Agent Name: " + AgentName);

            foreach (Client c in clientList)

            {

                result.Append(" " + c);

            }

            return result.ToString();

        }

       

        public bool AddClient(Client c)

        {

        }

        public Client FindClient(string clientNameValue)

        {

        }

        public bool RemoveClient(string clientNameValue)

        {

        }

    }

Explanation / Answer

import java.util.ArrayList;

import java.util.Dictionary;

import java.util.Enumeration;

import java.util.Hashtable;

import java.util.Iterator;

import java.util.List;

public class Agent {

public static void main(String[] args)

{

Agent agent = new Agent("agent");

Client client1 = new Client("client1", "USA");

System.out.println( "Adding client1 :: "+agent.addClient(client1));

Client client2 = new Client("client2", "Australia");

System.out.println( "Adding client2 :: "+agent.addClient(client2));

System.out.println(agent);

System.out.println("Finding client3 : "+agent.findClient("client3"));

System.out.println("Adding client1 : "+agent.addClient(client1));

System.out.println(agent);

System.out.println("Remove Cient :: "+agent.removeClient("client1"));

System.out.println(agent);

Client client3 = new Client("client3", "China");

System.out.println( "Adding client3 :: "+agent.addClient(client3));

System.out.println(agent);

}

//(fields) class members

private String agentName;

private List<Client> clientList;

private Dictionary<String, Client> clientDictionary;

//zero-arguments constructor

/**

* @return the agentName

*/

public String getAgentName() {

return agentName;

}

/**

* @param agentName the agentName to set

*/

public void setAgentName(String agentName) {

this.agentName = agentName;

}

public Agent()

{

this.agentName = "na";

this.clientList = new ArrayList<Client>();

this.clientDictionary = new Hashtable<String, Client>();

}

//one-argument constructor (see note above)

public Agent(String agentNameValue)

{

this.agentName = agentNameValue;

this.clientList = new ArrayList<Client>();

this.clientDictionary = new Hashtable<String, Client>();

}

public String toString()

{

StringBuilder result = new StringBuilder("Agent Name: " + this.agentName);

Enumeration<Client> clients = clientDictionary.elements();

while(clients.hasMoreElements())

{

result.append(" "+clients.nextElement() );

}

return result.toString();

}

public boolean addClient(Client client)

{

boolean isAddedCleint=false;

if(this.clientDictionary.get(client.getName()) == null)

{

isAddedCleint = true;

clientDictionary.put(client.getName(), client);

}

return isAddedCleint;

}

public Client findClient(String clientNameValue)

{

Client client = null;

if(this.clientDictionary.get(clientNameValue) != null)

{

client = clientDictionary.get(client.getName());

}

return client;

}

public boolean removeClient(String clientNameValue)

{

boolean isRemoved = false;

if(this.clientDictionary.get(clientNameValue) != null)

{

clientDictionary.remove(clientNameValue);

isRemoved = true;

}

return isRemoved;

}

}

class Client

{

private String name;

private String location;

public Client(String name,String location)

{

this.name= name;

this.location=location;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the location

*/

public String getLocation() {

return location;

}

/**

* @param location the location to set

*/

public void setLocation(String location) {

this.location = location;

}

public String toString()

{

return "Client Name : "+name + " Location :: "+location;

}

}

Output:

=================================

Adding client1 :: true

Adding client2 :: true

Agent Name: agent

Client Name : client2 Location :: Australia

Client Name : client1 Location :: USA

Finding client3 : null

Adding client1 : false

Agent Name: agent

Client Name : client2 Location :: Australia

Client Name : client1 Location :: USA

Remove Cient :: true

Agent Name: agent

Client Name : client2 Location :: Australia

Adding client3 :: true

Agent Name: agent

Client Name : client2 Location :: Australia

Client Name : client3 Location :: China