In: Computer Science
A company has three salespeople (1 to 3) who sell five different products (1 to 5). Once a day, each saleperson passes in a slip for each type of product sold. Each slip contains the following: a) The salesperson number b) The product number c) The total dollar value of that product sold that day Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. You are required to write a C# program that will read all the information for last month’s sales and summarize the total sales by salesperson and by product. All sales data should be stored in a two-dimensional array sales. After processing all the information for last month, display the results in tabular format, with each column representing a particular salesperson and each row representing a particular product. Cross-total each row to get the total sales of each product for last month. Cross-total each column to get the total sales by salesperson for last month. You tabular output should include these cross-totals to the right of the totaled rows and below the totaled columns.
A sample output is as follows:
Enter sales person number (-1 to end): 1
Enter product number: 100
Enter sales amount: 1000
Invalid input!
Enter sales person number (-1 to end): 1
Enter product number: 1
Enter sales amount: 100
Enter sales person number (-1 to end): 1
Enter product number: 3
Enter sales amount: 200
Enter sales person number (-1 to end): 1
Enter product number: 5
Enter sales amount: 300
Enter sales person number (-1 to end): 2
Enter product number: 2
Enter sales amount: 400
Enter sales person number (-1 to end): 2
Enter product number: 4
Enter sales amount: 500
Enter sales person number (-1 to end): 3
Enter product number: 1
Enter sales amount: 600
Enter sales person number (-1 to end): 3
Enter product number: 2
Enter sales amount: 700
Enter sales person number (-1 to end): 3
Enter product number: 3
Enter sales amount: 800
Enter sales person number (-1 to end): -1
Product Salesperson 1 Salesperson 2 Salesperson 3 Total
1 $100.00 $0.00 $600.00 $700.00
2 $0.00 $400.00 $700.00 $1,100.00
3 $200.00 $0.00 $800.00 $1,000.00
4 $0.00 $500.00 $0.00 $500.00
5 $300.00 $0.00 $0.00 $300.00
Total $600.00 $900.00 $2,100.00
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace Sales
{
class Program
{
static int numOfPerson = 3;
static int numProduct = 5;
static void Main(string[] args)
{
double[,] monthSales = new double[5, 3] { { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
double salesAmount;
int productNumber,personNumber;
while (true)
{
Console.Write("Enter sales person number (-1 to end): ");
personNumber = Convert.ToInt32(Console.ReadLine());
if(personNumber==-1)
break;
Console.Write("Enter product number: ");
productNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter sales amount: ");
salesAmount = Convert.ToInt32(Console.ReadLine());
if (personNumber <=3 && (productNumber >= 1
&& productNumber <= 5))
{
monthSales[productNumber - 1, personNumber - 1] =
salesAmount;
}
else
Console.WriteLine("Invalid input");
}
printSales(monthSales);
}
private static void printSales(double[,] monthSales)
{
int i, j;
double [] personTotalSales=new double[numOfPerson];
Console.WriteLine("\n\n");
Console.WriteLine(String.Format("{0,-5}| {1,-10} | {2,-10} |
{3,-10} | {4,14}","Product","Salesperson 1","Salesperson
2","Salesperson 1","Total"));
for (i = 0; i < numProduct; i++)
{
double sum = monthSales[i, 0] + monthSales[i, 1] + monthSales[i,
2];
Console.WriteLine(String.Format("{0,7}| {1,13} | {2,13} | {3,13} |
{4,13}", (i + 1), monthSales[i, 0].ToString("C", new
CultureInfo("en-US")), monthSales[i, 1].ToString("C", new
CultureInfo("en-US")), monthSales[i, 2].ToString("C", new
CultureInfo("en-US")), sum.ToString("C", new
CultureInfo("en-US"))));
}
Console.WriteLine("----------------------------------------------------------------------");
for (j = 0; j < numOfPerson; j++)
{
personTotalSales[j] = monthSales[0, j] + monthSales[1, j] +
monthSales[2, j] + monthSales[3, j] + monthSales[4, j];
}
Console.WriteLine(String.Format("{0,7}| {1,13} | {2,13} | {3,13} ",
"Total", personTotalSales[0].ToString("C", new
CultureInfo("en-US")), personTotalSales[1].ToString("C", new
CultureInfo("en-US")), personTotalSales[2].ToString("C", new
CultureInfo("en-US"))));
}
}
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.