Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Share Trading Company

If we have a share trading company in which every customer can buy shares of different companies with different numbers of shares, we have to go with jagged arrays. In the code presented below, I've asked the number of customers to be place in memory (if memory is available to store those records, of course); then we'll send them to the database in a batch.

In this problem, every customer can buy the shares of different companies whose names are different lengths. The number of companies associated with every customer can also differ.

Therefore, to optimize the use of available memory, we cannot go with multi-dimensional arrays because we don't have the fixed number of elements associated with each customer, and we want to allocate the memory for every associated element to the customer when the data is being inputted.

namespace ShareTrade
using System ;
class ShareTrading

  class Trading
   public static void Main()
     Console.Write("How many customers record you want to have in memeory : ") ;
     int NoOfCustomers = Console.ReadLine().ToInt32() ;		

     string[][] CustomerRecords ;
     // To store the name of companies shares purchased of, by customer
     int[][] AllShares ; // To store the number of shares obtained by every customer
     string[] CustomerNames ; // To store the name of customers

     CustomerRecords = new string[NoOfCustomers][] ;
     AllShares = new int[NoOfCustomers][] ;
     CustomerNames = new string[NoOfCustomers] ;

     for(int CustomerNo = 0 ; CustomerNo < NoOfCustomers ; CustomerNo++)
      // Getting the cutomers name
      Console.Write("Enter Your Name (Customer Number :: {0}) : ",CustomerNo + 1) ;
      CustomerNames[CustomerNo] = Console.ReadLine() ;

      // Welcome Message
      Console.WriteLine("Welcome {0} !!!",CustomerNames[CustomerNo]) ;

      //Asking about the number of companies from which customer is going to buy shares
      Console.Write("How many different companies shares you want : ") ;
      int NoOfCompanies = Console.ReadLine().ToInt32() ;

      CustomerRecords[CustomerNo] = new string[NoOfCompanies] ;
      AllShares[CustomerNo] = new int[NoOfCompanies] ;

      for(int CompanyCount = 0 ; CompanyCount < NoOfCompanies ; CompanyCount++)
        // Asking about the company names cutomers wants to buy from
        Console.Write("Enter Company Name {0} : ",CompanyCount + 1) ;
        CustomerRecords[CustomerNo][CompanyCount] = Console.ReadLine() ;

        // Asking about the number of shares to buy
        Console.Write("Enter number of shares to buy of {0}: 
                      ",CustomerRecords[CustomerNo][CompanyCount]) ;
        AllShares[CustomerNo][CompanyCount] = Console.ReadLine().ToInt32() ;


     // Sending data to store in database, just portraying
     SendToDatabase(ref CustomerRecords, ref AllShares, 
                    ref CustomerNames, NoOfCustomers) ;    

     Console.Write("Press any key to finish ...") ; 
     Console.Read() ;

static void SendToDatabase(ref string[][] Records, 
                           ref int[][] Shares, 
                           ref string[] Names, 
                           int CustomerCount)
  //Sending Data to the Database

  Console.WriteLine() ;
  Console.WriteLine("Establishing connection to database ....") ;

  int ColNo = 0; // Column Number
  for(int RowNo = 0 ; RowNo < CustomerCount ; RowNo++)
   Console.WriteLine() ;
   Console.WriteLine("\nCustomer Name : {0}",Names[RowNo]) ;
   ColNo = 0 ;
   foreach(string record in Records[RowNo])
     Console.Write("Company Name : {0}",record) ;
     Console.WriteLine("\tShares Purchased : {0}",Shares[RowNo][ColNo++]) ;
  Console.WriteLine() ;
  Console.WriteLine("Data has been stored in appropriate database records !!") ;



In this solution, we use a method named "SendToDatabase", which shows that we are sending the data stored in memory to the database in a batch, as we are passing the jagged arrays as reference parameters to the method. We can enhance this application according to our application needs; this example is just to show the concept of jagged arrays as the best solution to real-life problems.

The application designed is a self-explanatory console application that shows only the optimized part. The database connectivity and visual interface are not part of our topic, and have been portrayed by "SendToDatabase" methods and console messages, which can be replaced by required components.

  • + Share This
  • 🔖 Save To Your Account