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

Objective: Learn basic C# Programming Language A certain automobile dealership s

ID: 3852470 • Letter: O

Question

Objective: Learn basic C# Programming Language

A certain automobile dealership sells fifteen different models of automobiles and employs ten salesmen.

A record of sales for each month can be represented by a table, the first row of which contains the

number of sales of each model by salesman 1, the second row contains the number of sales of each

model by salesman 2, and so on. For example, suppose that the sales table for a certain month is the

following:

0 0 2 0 5 6 3 1 3 4 3 3 1 0 2

5 1 9 0 0 2 3 2 2 3 1 4 6 2 1

0 0 0 1 0 0 0 3 1 6 2 1 7 3 2

1 1 1 0 2 2 2 5 2 7 4 2 8 2 1

5 3 2 0 0 0 5 2 1 2 6 6 6 1 4

4 2 1 0 1 1 0 4 4 1 5 5 1 2 1

3 2 5 0 1 2 0 1 3 5 4 3 2 1 2

3 0 7 1 3 5 1 0 6 8 2 2 3 2 1

0 2 6 1 0 2 2 1 1 2 1 5 4 3 0

4 0 2 3 2 1 3 2 3 1 5 4 0 1 0

Figure 1

The automobile models are defined as follows.

Camry, Corolla, Tercel, Protege, Mazda3, Mazda6,Altima, Maxima,

Sentra, Diamante, colt, Mirage, Legacy, Stylus, Impulse.

A Console Based C# program to be written to accomplish the following requirements:

1.The C# program must manage data using a 2-Dimensional array of integers (table in figure 1).

2.The program must be menu driven (See Figure 2)

Your system should be menu driven with the following options.

General Auto Sales

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

1.Add a new sale

2.Sales Per Salesman by model

3.All Sales Per Salesman

4.All Sales For The Company

5.Exit

Figure 2

1.Add a new sale. When this option is selected a screen is popped up requesting the following

information.

Enter Transaction Code :

A transaction code is a 4 character string. For example 0912 is a valid transaction code. The first two

characters represent the salesman code , and the last two characters represent the automobile model

code. Once the transaction code is entered at the above prompt, a verification is required check if the

entered code is a valid one. If the code is invalid then an error message is displayed. If the

entered code is a valid one, the value of the (9,12) location of the table must be incremented by 1.

[Note: In C#, array indices start from 0. Therefore 0, 0 location is a valid location for a 2-D array.

Therefore 0000 is a valid code.

2.Sales per salesman by model. If this option is selected, again, similar transaction code (as described in

1)is entered first. (The verification MUST be done for all options)

Suppose the code 0111 was entered. This is a valid code. This means salesman 01, 11th model. Read

the (01,11) location of the 2-D array and display the following information

Sales Representative Code : 01

Automobile Model : Mirage

Total Sold By This Representative : 4

3. All Sales Per Salesman. This is similar to option 2 except for that all models sold by that representative

is displayed.

As an example , say the code 0809 was entered. This is also a valid code. The following information is

displayed on the screen.

Sales Representative Code : 08

Automobile Model Number Sold

Camry 0

Corolla 2

Tercel 6

Mazda3 1

Marda6 0

Protégé 2

Altima 2

Maxima 1

Sentra 1

Diamante 2

Colt 1

Mirage 5

Legacy 4

Stylus 3

Impulse 0

Total 30

==========

4.Total Sales for the company : A monthly sales report must be produced in the following form:

MODEL

Salesman Ca Cr Te M3 M6 Pro Alt Max Sen Dia Col Mir Leg Sty Imp Total

0 0 0 2 0 5 6 3 1 3 4 3 4 3 1 0 35

1 5 1 9 0 0 2 3 2 2 3 1 3 6 2 1 40

2 0 0 0 1 0 0 0 3 1 6 2 1 7 3 2 26

3 1 1 1 0 2 2 2 5 2 7 4 2 8 2 1 40

4 5 3 2 0 0 0 5 2 1 2 6 6 6 1 4 43

5 4 2 1 0 1 1 0 4 4 1 5 5 1 2 1 32

6 3 2 5 0 1 2 0 1 3 5 4 3 2 1 2 34

7 3 0 7 1 3 5 1 0 6 8 2 2 3 2 1 44

8 0 2 6 1 0 2 2 1 1 2 1 5 4 3 0 30

9 4 0 2 3 2 1 3 2 3 1 5 4 0 1 0 33

-------------------

-----

-----

-----

-----

-----

-----

----------

----

------

-----

------

25

11 35 6 13 21 19 21 27 39 33 35 40 18 12 357

In the actual print out, all the above abbreviated names should be printed as full names as shown in

the type declaration above.

You can hardcode the initial 2-D array to the following. You can hardcode the initial 2-D array to the following.

0 1 0 0 0 0 0 1 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 1 0 0 0 0 0 0 0 0

0 2 0 0 0 0 0 0 0 0 0 0 1 0 0

0 0 0 1 0 0 0 0 0 0 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 0 0 0 0 0 0 0 1 0 0 0 0 0 0

0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

2 0 0 0 0 0 0 0 0 0 0 0 0 0 1

0 0 0 0 0 0 1 0 0 1 0 0 0 0 0

Figure 2 –

Initial 2-D Array

(Note : 0000 is a valid code)

In your program code, you MUST make sure (verification & validation is required) that the correct code is

used to access the table. For example, the user enters the code 1512. This is an invalid code. Your

program must be intelligent enough to detect this and display a proper error message.

For testing purposes you may build your own set of data. You must test your application using the given

scenario.

All output files must have appropriate headings and self documenting information. Identifiers must be

meaningful. The program should be laid out neatly. Block structure should be used to localize

declarations (Avoid Global Variables as much as possible). Whenever possible a piece of information

should be written once; thus numbers and constant declarations should be used rather than explicit

literals. Make all subroutines shorter as possible.

Explanation / Answer

/**
C # program that demonstrates the sales by models
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//create a console project with name salespr
namespace salespr
{
    class Program
    {

        //set number of sales man and item models
        private const int PERSONS = 10;
        private const int ITEMS = 15;

        //array of model names
        static string[] modelNames ={"Camry", "Corolla", "Tercel", "Protege", "Mazda3", "Mazda6",
        "Altima", "Maxima", "Sentra", "Diamante", "colt", "Mirage",
        "Legacy", "Stylus", "Impulse"};

        static void Main(string[] args)
        {

            //sales array
            int[,] sales ={{0, 0, 2, 0, 5, 6, 3, 1, 3, 4, 3, 3, 1, 0, 2 },
                            {5, 1, 9, 0, 0, 2, 3, 2, 2, 3, 1, 4, 6, 2, 1 },
                            {0, 0, 0, 1, 0, 0, 0, 3, 1, 6, 2, 1, 7, 3, 2},
                            {1, 1, 1, 0, 2, 2, 2, 5, 2, 7, 4, 2, 8, 2, 1 },
                            {5, 3, 2, 0,0, 0, 5,2, 1, 2, 6, 6, 6, 1, 4},
                            {4, 2, 1, 0, 1, 1, 0, 4, 4, 1, 5, 5, 1, 2, 1 },
                            {3, 2, 5, 0, 1, 2, 0, 1, 3, 5, 4, 3, 2, 1, 2 },
                            {3, 0, 7, 1, 3, 5, 1, 0, 6, 8, 2, 2, 3, 2, 1 },
                            {0, 2, 6, 1, 0, 2, 2, 1, 1, 2, 1, 5, 4, 3, 0 },
                            {4, 0, 2, 3, 2, 1, 3, 2, 3, 1, 5, 4, 0, 1, 0 }
                            };


            bool repeat = true;
            int choice;
            while (repeat)
            {
                choice = menu();
                switch (choice)
                {
                    case 1:
                        addNewSale(sales);
                        break;
                    case 2:
                        salerPerSalesman(sales);
                        break;
                    case 3:
                        allSalesPerSalesman(sales);
                        break;
                    case 4:
                        totalSales(sales);
                        break;
                    case 5:
                        Environment.Exit(0);
                        break;
                }
            }
        }

        //print total sales of sales man
        public static void totalSales(int[,] sales)
        {

            int totalSum = 0;
            int rowSum = 0;
            int colSum = 0;

            for (int salesman = 0; salesman < PERSONS; salesman++)
            {
                for (int item = 0; item < ITEMS; item++)
                {
                    rowSum += sales[salesman, item];
                    Console.Write("{0,-3}", sales[salesman, item]);
                }

                Console.WriteLine("{0,-3}", rowSum);
                rowSum = 0;
            }

            for (int item = 0; item < ITEMS; item++)          
            {
                for (int salesman = 0; salesman < PERSONS; salesman++)  
                {
                    colSum += sales[salesman,item];                  
                }
                totalSum += colSum;
                Console.Write("{0,-3}", colSum);
                colSum = 0;
            }

            Console.WriteLine("{0}",totalSum);

        }


        //print all sales of the sales to console
        public static void allSalesPerSalesman(int[,] sales)
        {
            Console.WriteLine("Enter Transaction Code : ");
            string code = Console.ReadLine();

            int salesManCode = Convert.ToInt32(code.Substring(0, 2));
            int modelCode = Convert.ToInt32(code.Substring(2));


            if (isValid(salesManCode, modelCode))
                Console.WriteLine("Invalid code");
            else
            {

                for (int i = 0; i < ITEMS; i++)
                {
                    Console.WriteLine("{0} : {1}", modelNames[i], sales[salesManCode, i]);
                }

            }

        }

        //method that add a sale to the sales man
        public static void addNewSale(int[,] sales)
        {
            Console.WriteLine("Enter Transaction Code : ");
            string code = Console.ReadLine();

            int salesManCode = Convert.ToInt32(code.Substring(0, 2));
            int modelCode = Convert.ToInt32(code.Substring(2));


            if (isValid(salesManCode, modelCode))
                Console.WriteLine("Invalid code");
            else
            {
                sales[salesManCode, modelCode] = sales[salesManCode, modelCode] + 1;
            }
        }


        //print the sales per sales man
        public static void salerPerSalesman(int[,] sales)
        {
            Console.WriteLine("Enter Transaction Code : ");
            string code = Console.ReadLine();

            int salesManCode = Convert.ToInt32(code.Substring(0, 2));
            int modelCode = Convert.ToInt32(code.Substring(2));


            if (isValid(salesManCode, modelCode))
                Console.WriteLine("Invalid code");
            else
            {
                Console.WriteLine("Sales Representative Code : {0}", salesManCode);
                Console.WriteLine("Automobile Model : {0}", modelNames[modelCode]);
                Console.WriteLine("Total Sold By This Representative : {0}", sales[salesManCode, modelCode]);
            }
        }

        //Check for validity of code
        public static bool isValid(int salesManCode, int modelCode)
        {
            return (salesManCode < 0 || salesManCode > 10) || (modelCode < 0 || modelCode > 15);
        }

        //Menu method
        public static int menu()
        {

            Console.WriteLine(" 1.Add a new sale ");
            Console.WriteLine("2.Sales Per Salesman by model");
            Console.WriteLine("3.All Sales Per Salesman ");
            Console.WriteLine("4.All Sales For The Company ");
            Console.WriteLine("5.Exit ");
            Console.WriteLine("Enter your choice");
            int choice = Convert.ToInt32(Console.ReadLine());

            return choice;

        }

    }
}


Sample Output:

1.Add a new sale
2.Sales Per Salesman by model
3.All Sales Per Salesman
4.All Sales For The Company
5.Exit
Enter your choice
1
Enter Transaction Code :
0000


1.Add a new sale
2.Sales Per Salesman by model
3.All Sales Per Salesman
4.All Sales For The Company
5.Exit
Enter your choice
4
1 0 2 0 5 6 3 1 3 4 3 3 1 0 2 34
5 1 9 0 0 2 3 2 2 3 1 4 6 2 1 41
0 0 0 1 0 0 0 3 1 6 2 1 7 3 2 26
1 1 1 0 2 2 2 5 2 7 4 2 8 2 1 40
5 3 2 0 0 0 5 2 1 2 6 6 6 1 4 43
4 2 1 0 1 1 0 4 4 1 5 5 1 2 1 32
3 2 5 0 1 2 0 1 3 5 4 3 2 1 2 34
3 0 7 1 3 5 1 0 6 8 2 2 3 2 1 44
0 2 6 1 0 2 2 1 1 2 1 5 4 3 0 30
4 0 2 3 2 1 3 2 3 1 5 4 0 1 0 31
26 11 35 6 14 21 19 21 26 39 33 35 38 17 14 355


1.Add a new sale
2.Sales Per Salesman by model
3.All Sales Per Salesman
4.All Sales For The Company
5.Exit
Enter your choice
1
Enter Transaction Code :
0001


1.Add a new sale
2.Sales Per Salesman by model
3.All Sales Per Salesman
4.All Sales For The Company
5.Exit
Enter your choice
4
1 1 2 0 5 6 3 1 3 4 3 3 1 0 2 35
5 1 9 0 0 2 3 2 2 3 1 4 6 2 1 41
0 0 0 1 0 0 0 3 1 6 2 1 7 3 2 26
1 1 1 0 2 2 2 5 2 7 4 2 8 2 1 40
5 3 2 0 0 0 5 2 1 2 6 6 6 1 4 43
4 2 1 0 1 1 0 4 4 1 5 5 1 2 1 32
3 2 5 0 1 2 0 1 3 5 4 3 2 1 2 34
3 0 7 1 3 5 1 0 6 8 2 2 3 2 1 44
0 2 6 1 0 2 2 1 1 2 1 5 4 3 0 30
4 0 2 3 2 1 3 2 3 1 5 4 0 1 0 31
26 12 35 6 14 21 19 21 26 39 33 35 38 17 14 356


1.Add a new sale
2.Sales Per Salesman by model
3.All Sales Per Salesman
4.All Sales For The Company
5.Exit
Enter your choice
2
Enter Transaction Code :
0000
Sales Representative Code : 0
Automobile Model : Camry
Total Sold By This Representative : 1


1.Add a new sale
2.Sales Per Salesman by model
3.All Sales Per Salesman
4.All Sales For The Company
5.Exit
Enter your choice
3
Enter Transaction Code :
0000
Camry : 1
Corolla : 1
Tercel : 2
Protege : 0
Mazda3 : 5
Mazda6 : 6
Altima : 3
Maxima : 1
Sentra : 3
Diamante : 4
colt : 3
Mirage : 3
Legacy : 1
Stylus : 0
Impulse : 2


1.Add a new sale
2.Sales Per Salesman by model
3.All Sales Per Salesman
4.All Sales For The Company
5.Exit
Enter your choice


5