Question

Please Write the following program in c# You are to write an application which will create...

Please Write the following program in c#

You are to write an application which will create a company, add agents (their name, ID, and weekly sales) to that company, and printout the details of all agents in the company. The application will contain three classes: Agent.cs, Company.cs, and CompanyTest.cs (this class is already provided).

Flow of the application: The CompanyTest class creates an object of the Company class and reserves space for five agents. At this point if you call the ToString method of the Company object, it will only print NULL since the array which will hold information about agents has not been populated (e.g. five NULLs, as shown in the screen shot below). Then CompanyTest attempts to populate the array with six agents. Since there is space for only FIVE agents, the sixth is not added. Every time an agent is successfully added, the program prints true, else it prints false. After populating the array, details of the agents, such as their ID, name, sales and average, can be printed out as shown below.

The class Agent has the following essential elements:

Instance variables (or auto-implemented properties):

  • a string called agentID                
  • a string called agentName
  • a twodimensional jagged integer array called weeklySales (each row in the multidimensional array represents sales made in a week)
  • a double called average

Two overloaded constructors:

  • one takes parameters: agentID of type string and agentName of type string
  • the other takes parameters: agentID of type string, agentName of type string, and a twodimensional integer jagged array.
  • since the parameters and instance variables have the same names, use the this keyword in the body of the constructors.

Methods:

  • CalcAgentAvg: when called, simply goes through the weeklySales array and calculates the average of the sales. This method will have two for loops (one nested inside the other) that enable us to go through the weeklySales array and calculate the average.
  • PrintWeeklySales: This method helps in creating a string representation of the weeklySales array. It uses two for loops to go through the weeklySales array and builds a string that shows the individual elements of the array.
  • ToString: returns a String which represents the ID, the name, the weekly sales, and the average of a particular agent.

The class Company has the following essential elements:

Instance variables:

  • agentsArray: this is a onedimensional array of type Agent. That is, all its members will be objects of type Agent.

Constructor:

  • one constructor which takes an integer, which represents the maximum number of agents allowed for the company. Using this value, the array instance variable is sized appropriately to hold that many agents.

Methods:

  • AddAgent: takes two strings, agentID and agentName, and a jagged array (this holds sales for the agent being added). If the maximum number of agents has been reached, this method will return false and do nothing else (that is, the agent will not be added). If the maximum number has not been reached, this method will add the agent and return true. The number of agents CANNOT exceed the size of the array, agentsArray.
  • CalcSalesAverage: goes through ONLY the populated elements of agentsArray and calculates average of the sales of EACH agent (by calling the CalcAgentAvg method of the agent object).
  • ToString: goes through the entire agentsArray and calls the ToString method of each agent object in the array. If the element is not populated, this method writes NULL.

The CompanyTest class is below:

public class CompanyTest

{

public static void Main (string[] args)

{

Company aCompany = new Company(5);

Console.WriteLine("COMPANY WITH SPACES FOR 5 AGENTS, NONE POPULATED: \n");

Console.WriteLine(aCompany.ToString());

Console.WriteLine("attempting to populate with 6 agents... \n");

int[][] arr1 = {new int[] {10,20,30,40,50}, new int[] {10, 20} };

Console.WriteLine(aCompany.AddAgent("2601", "Salman Nazir", arr1));

int[][] arr2 = {new int[] {20,20,25,30,30}, new int[] {20, 20}, new int[] {20} };

Console.WriteLine(aCompany.AddAgent("2602", "Graham Peace", arr2));

int[][] arr3 = {new int[] {40,50,60}, new int[] {40, 50}};

   Consol Console.WriteLine(aCompany.AddAgent("2603", "Virginia Kleist", arr3));

int[][] arr int[][] arr4 = {new int[] {75,85,90},new int[] {40, 50}};

int[][] arr5 = {new int[] {90,50,70}, new int[] {40, 50}};

      Console.WriteLine(aCompany.AddAgent("2605", "Oran Alston", arr5));

  int[][] arr6 = {new int[] {40,50,60}, new int[] {40, 50}};

       Console.WriteLine(aCompany.AddAgent("2606", "John Doe", arr6)); //prints false

aCompany.CalcSalesAverage(); //call to method

       Console.WriteLine("\nCOMPANY POPULATED WITH 5 AGENTS: \n");

Console.WriteLine(aCompany.ToString());

Console.ReadLine();

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per requirement submitted above kindly find below solution.

Here new Console Application in C# is created using Visual Studio 2019 with name "AgentsCompanyApplication".This application contains below classes.

Agent.cs :

//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//application namespace
namespace AgentsCompanyApplication
{
class Agent //C# class
{
//auto-implemented properties
public string agentID { get; set; }
public string agentName { get; set; }
//two dimensional jagged array
public int[][] weeklySales { get; set; }
public int average { get; set; }
//constructor
public Agent(string agentID,string agentName)
{
this.agentID = agentID;
this.agentName = agentName;
}
public Agent(string agentID, string agentName,int [][] weeklySales)
{
this.agentID = agentID;
this.agentName = agentName;
this.weeklySales = weeklySales;
}
//methid to calculate average
public void CalcAgentAvg()
{
//variable used to store sum
int sum = 0;
  
//using for loop
//this will be used for number of rows
for (int i = 0; i < weeklySales.GetLength(0); i++)
{
//this for loop is used for number of columns
for (int j = 0; j < weeklySales[i].Length; j++)
{
sum = sum + weeklySales[i][j];
}
}
//calculate and assign the average
this.average = sum/weeklySales.Length;
}
//function to print PrintWeeklySales()
public string PrintWeeklySales()
{
//variable used to concatenate string
string str = "";
//using for loop
//this will be used for number of rows
for (int i = 0; i < weeklySales.GetLength(0); i++)
{
//this for loop is used for number of columns
for (int j = 0; j < weeklySales[i].Length; j++)
{
str = str + weeklySales[i][j]+" ";//concatenate each element
}
str +=",";//used for new line
}
return str;
}
//ToString() method
public string ToString()
{
//return weekly sales
return this.agentID + " " + this.agentName + " " + PrintWeeklySales() + " " + this.average;
}
}
}

**********************************

Company.cs :

//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//application namespace
namespace AgentsCompanyApplication
{
class Company //C# class
{
//Instance variables
public Agent [] agentsArray { get; set; }
public int i=0;
//constructor
public Company(int num)
{
agentsArray = new Agent[num];//set size of the array agentsArray
}
//method to add agent
public bool AddAgent(string agentID,string agentName,int[][] weeklySales)
{
bool flag;//used to return bool type
//checking size of the array
if(i<agentsArray.Length)
{
//if size is not reached maximum number than add agent
agentsArray[i] = new Agent(agentID, agentName, weeklySales);
i++;//increment value of i
flag = true;
}
else
{
flag = false;//set flag
}
return flag;//return flag
}
//methd to calculate sales average
public void CalcSalesAverage()
{
for (int i = 0; i < agentsArray.Length; i++)
{
//call method to get the sales of each agent
agentsArray[i].CalcAgentAvg();
}
}
//method to return string
public string ToString()
{
string returnString = "";//declaring variable
for (int i = 0; i < agentsArray.Length; i++)
{
if(agentsArray[i]==null)
{
returnString = "NULL";//set as NULL
}
else
{
//call method to print each agent details
returnString+=agentsArray[i].ToString()+Environment.NewLine;
}

}
return returnString;//return string
}


}
}

********************************

CompanyTest.cs :

//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//application namespace
namespace AgentsCompanyApplication
{
class CompanyTest //C# class
{
//entry point of application , Main() method
static void Main(string[] args)
{
//creating array of company class objects
Company aCompany = new Company(5);
//message
Console.WriteLine("COMPANY WITH SPACES FOR 5 AGENTS, NONE POPULATED: \n");
//method to return details of agent
Console.WriteLine(aCompany.ToString());
//when trying to populate with 6 agents
Console.WriteLine("attempting to populate with 6 agents... \n");
//declaring and initilizing array which is jagged arrat of sales
int[][] arr1 = { new int[] { 10, 20, 30, 40, 50 }, new int[] { 10, 20 } };
//adding agent in the company
Console.WriteLine(aCompany.AddAgent("2601", "Salman Nazir", arr1));
//declaring and initilizing array which is jagged arrat of sales for agent 2
int[][] arr2 = { new int[] { 20, 20, 25, 30, 30 }, new int[] { 20, 20 }, new int[] { 20 } };
//adding agent in the company
Console.WriteLine(aCompany.AddAgent("2602", "Graham Peace", arr2));
//declaring and initilizing array which is jagged arrat of sales for agent 3
int[][] arr3 = { new int[] { 40, 50, 60 }, new int[] { 40, 50 } };
//adding agent in the company
Console.WriteLine(aCompany.AddAgent("2603", "Virginia Kleist", arr3));
//declaring and initilizing array which is jagged arrat of sales for agent 4
int[][] arr4 = { new int[] { 75, 85, 90 }, new int[] { 40, 50 } };
//declaring and initilizing array which is jagged arrat of sales for agent 5
int[][] arr5 = { new int[] { 90, 50, 70 }, new int[] { 40, 50 } };
//adding agent in the company
Console.WriteLine(aCompany.AddAgent("2605", "Oran Alston", arr5));
//declaring and initilizing array which is jagged arrat of sales for agent 6
int[][] arr6 = { new int[] { 40, 50, 60 }, new int[] { 40, 50 } };
//adding agent in the company
Console.WriteLine(aCompany.AddAgent("2606", "John Doe", arr6)); //prints false
//method to calculate average sales
aCompany.CalcSalesAverage(); //call to method
//message
Console.WriteLine("\nCOMPANY POPULATED WITH 5 AGENTS: \n");
//method tp print details
Console.WriteLine(aCompany.ToString());
//used to hold the screen
Console.ReadLine();
}
}
}

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

Output :Run application using F5 and will get the screen as shown below

Screen 1:

NOTE :PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
Please Write the following program in c# You are to write an application which will create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account i...

    You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account in a bank. The class must have the following instance variables: a String called accountID                       a String called accountName a two-dimensional integer array called deposits (each row represents deposits made in a week). At this point it should not be given any initial value. Each...

  • Your goal is to create an ‘Array’ class that is able to hold multiple integer values....

    Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...

  • Given below is a partially completed C program, which you will use as a start to...

    Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count - @; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; return count; Task 1: (10 pts) Show the design of the function compareAndCount() by writing a pseudocode. To...

  • PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Given...

    PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; } return count;...

  • PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint:...

    PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint: the number array in task 2 is work for task 3 in figure 1.   Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i...

  • Using C# Create an application class named LetterDemo that instantiates objects of two classes named Letter...

    Using C# Create an application class named LetterDemo that instantiates objects of two classes named Letter and CertifiedLetter and that demonstrates all their methods. The classes are used by a company to keep track of letters they mail to clients. The Letter class includes auto-implemented properties for the Name of the recipient and the Date mailed (stored as strings). Next, include a ToString() method that overrides the Object class’s ToString() method and returns a string that contains the name of...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT