Visual C# 8.20 (Total Sales)

This Solution is for the “Total Sales” of chapter 8 in the textbook; Visual c# How to program 6th edition by Deitel.

//Date: 6/2/2017
/*Description:
Program reads all the information input by the user for a month’s sales and summarize the total sales by salesperson and by product
All totals are stored in rectangular array “sales” and display the results in tabular format, with each column representing
a particular salesperson and each row representing a particular product as well as the total sales of each product
lastly, displays the total sales for each salesperson for the month.
*/
using System;

class Program
{
static void Main(string[] args)
{
decimal[,] salesarray = new decimal[5, 3];
string exit;

//loops user prompts
do
{

Console.Write(” Enter Salesperson ID: “);
int spID = int.Parse(Console.ReadLine());

 

Console.Write(” Enter the product ID: “);
int prodID = int.Parse(Console.ReadLine());

 

Console.Write(” Enter the Sale amount: “);
decimal sale = decimal.Parse(Console.ReadLine());

//reads user input into sales array
salesarray[prodID – 1, spID – 1] = sale;

Console.WriteLine(” Press < enter > to continue or input < Ctrl > z to end.”);
exit = Console.ReadLine();

} while (exit != null);

//prints titles
Console.WriteLine();
Console.WriteLine(“Prod. ID Salesperson 1 Salesperson 2 Salesperson 3 Total Product Sales”);
Console.WriteLine();

 

decimal[] totalsarray = new decimal[4];

//nested for statement prints Sales array data
for (int row = 0; row < 5; ++row)
{
//prints product ID number
Console.Write($”{(row + 1),8}”);

//initializes varable for product totals
decimal prodtotal = 0;

for (int column = 0; column < 3; ++column)
{
//prints sales array data
Console.Write($”{salesarray[row, column],16:C}”);

//calculates total amount of sales for each product ID
prodtotal += salesarray[row, column];

 

//reads each salespersons product total into totalsarray
totalsarray[column] += salesarray[row, column];
}
//prints the total sales for each product ID
Console.WriteLine($” {(prodtotal),24:C} “);
}

Console.Write(” Total:”);

for (int column = 0; column < 3; ++column)
{
//prints each salespersons total sales
Console.Write($”{(totalsarray[column]),16:C}”);
}
Console.WriteLine();
Console.WriteLine();
}

}

 

Program Output:
Enter Salesperson ID: 1
Enter the product ID: 1
Enter the Sale amount: 50.48
Press < enter > to continue or input < Ctrl > z to end.

Enter Salesperson ID: 1
Enter the product ID: 4
Enter the Sale amount: 300.79
Press < enter > to continue or input < Ctrl > z to end.

Enter Salesperson ID: 2
Enter the product ID: 1
Enter the Sale amount: 49.95
Press < enter > to continue or input < Ctrl > z to end.

Enter Salesperson ID: 2
Enter the product ID: 2
Enter the Sale amount: 100
Press < enter > to continue or input < Ctrl > z to end.

Enter Salesperson ID: 2
Enter the product ID: 5
Enter the Sale amount: 700.96
Press < enter > to continue or input < Ctrl > z to end.

Enter Salesperson ID: 3
Enter the product ID: 2
Enter the Sale amount: 99.99
Press < enter > to continue or input < Ctrl > z to end.

Enter Salesperson ID: 3
Enter the product ID: 4
Enter the Sale amount: 300.79
Press < enter > to continue or input < Ctrl > z to end.
^Z

Prod. ID Salesperson 1 Salesperson 2 Salesperson 3 Total Product Sales

1 $50.48 $49.95 $0.00 $100.43
2 $0.00 $100.00 $99.99 $199.99
3 $0.00 $0.00 $0.00 $0.00
4 $300.79 $0.00 $300.79 $601.58
5 $0.00 $700.96 $0.00 $700.96
Total: $351.27 $850.91 $400.78

Press any key to continue . . .

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s