Exemplo n.º 1
0
        static void Main(string[] args)
        {
            List <PhoneAcc> accounts = new List <PhoneAcc>(); // List for storing the final accounts to be displayed
            IDList          idList   = new IDList();          // Class for storing ID list retrieved from the web service

            // Start timing
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            // Retrieve the list of account IDs from the web service
            idList = getIDListFromWeb(null);

            if (env == (int)Env.Debugging)
            {
                Console.WriteLine("Retrieved ID list: " + idList.ToString());
            }

            // Process the retrieved accounts and store valid accounts
            processPhoneAcc(idList, ref accounts, numRes);

            // While the retrieved list has a token for retrieving even more phone accounts
            // Keep retrieving and processing accounts
            while (!String.IsNullOrEmpty(idList.token))
            {
                // Retrieve the list of account IDs from the web service
                idList = getIDListFromWeb(idList.token);

                if (env == (int)Env.Debugging)
                {
                    Console.WriteLine("Retrieved ID list: " + idList.ToString());
                }

                // Process the retrieved accounts and store valid accounts
                processPhoneAcc(idList, ref accounts, numRes);
            }

            // Sort the collected accounts based on name
            AccNameCompare nameCompare = new AccNameCompare();

            accounts.Sort(nameCompare);

            // Stop timing
            stopWatch.Stop();

            long time = stopWatch.ElapsedMilliseconds;

            // Print out the result
            Console.WriteLine();
            Console.WriteLine("Time elapsed: {0} ms", time);
            Console.WriteLine();
            Console.WriteLine(accounts.Count + " results sorted by name: ");
            for (int i = 0; i < accounts.Count; i++)
            {
                Console.WriteLine(accounts[i].ToString());
            }


            // Wait for return key to pause the console
            Console.ReadLine();
        } // end of Main()
Exemplo n.º 2
0
        /// <summary>
        /// Process phone accounts retrieved from web service
        /// Store phone accounts with valid US phone numbers and youngest age to the account list
        /// </summary>
        /// <param name="idList">The list of account IDs retrieved from web service</param>
        /// <param name="accounts">The list of phone accounts to collect</param>
        /// <param name="numRes">The number of account to collect</param>
        private static void processPhoneAcc(IDList idList, ref List <PhoneAcc> accounts, int numRes)
        {
            // Iterate through all the retrieved IDs
            // Using each ID, retrieve the account from the web service
            // Add [numRes] accounts with valid number to the account list
            // If the number of accounts retrieved from web service is less than [numRes]
            // Add all the retrieved accounts with valid number
            int maxAgeIndex = 0; // track the ID of the account the max age among the collecte accounts

            for (int i = 0; i < idList.result.Count; i++)
            {
                string json = null;

                using (WebClient webClient = new WebClient())
                {
                    // Retrieve the account using ID
                    try
                    {
                        // URL: endpoint + detail route + retrieved id of account
                        json = webClient.DownloadString(ENDPOINT_URL + DETAIL_ROUTE + idList.result[i]);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }

                PhoneAcc acc = JsonConvert.DeserializeObject <PhoneAcc>(json);

                if (env == (int)Env.Debugging)
                {
                    Console.WriteLine("Retrieved account: " + acc.ToString());
                }

                if (acc.hasValidNumber())
                {
                    // If not enough results have been collected
                    if (accounts.Count < numRes)
                    {
                        if (env == (int)Env.Debugging)
                        {
                            Console.WriteLine("Adding account with ID: " + acc.id);
                        }
                        accounts.Add(acc);

                        // Update the index of the account with max age
                        if (acc.age >= accounts[maxAgeIndex].age)
                        {
                            // The index of the new acc is count - 1
                            maxAgeIndex = accounts.Count - 1;
                        }
                    }
                    // If enough results have been collected
                    else
                    {
                        // Compare the retrieved acc to the collected acc with max age
                        if (acc.age < accounts[maxAgeIndex].age)
                        {
                            if (env == (int)Env.Debugging)
                            {
                                Console.WriteLine("Replacing account " + accounts[maxAgeIndex].id + " with account " + acc.id);
                            }
                            // Replace the acc with max age with the retrieved acc
                            accounts[maxAgeIndex] = acc;

                            // Update the index of the account with max age
                            maxAgeIndex = getMaxAgeIndex(accounts);
                        }
                    }
                }
            }
        }