Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
8.9 kB
1
Indexable
Never
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;

namespace ConsoleApp3
{
    //struktura po koqto rabotim
    struct ClientInfo
    {
        public string fullName;
        public int dayOfReg;
        public int monthOfReg;
        public int yearOfReg;

        public int numberOfPurchases;
        public float totalPriceOfPurchases;
        public int rating;
    }

    class Program
    {
        static ClientInfo ReadClientInfo() //suzdavame po struktura koqto priema stoinosti
        {//suzdavame obekt po struktura
            ClientInfo client = new ClientInfo();
            Console.Write("Please the full name of the client: ");
            client.fullName = ReadAndLimitString(40);

            client.dayOfReg = ReadNumberWithinRange(1, 31, "Please enter the day of registration: ");
            client.monthOfReg = ReadNumberWithinRange(1, 12, "Please enter the month of registration: ");
            client.yearOfReg = ReadNumberWithinRange(1900, 2021, "Please enter the year of registration: ");



            client.numberOfPurchases = ReadNumberWithinRange(1, 9999, "Please enter the number of purchases: ");

            Console.Write("Enter the total price of purchases: ");
            client.totalPriceOfPurchases = float.Parse(Console.ReadLine());

            if (client.numberOfPurchases < 100)
            {
                client.rating = 1;
            }
            else if (client.numberOfPurchases < 300)
            {
                client.rating = 2;
            }
            else if (client.numberOfPurchases < 500)
            {
                client.rating = 3;
            }
            else if (client.numberOfPurchases < 1000)
            {
                client.rating = 4;
            }
            else if (client.numberOfPurchases < 10000)
            {
                client.rating = 5;
            }
           
            return client;
        }
        static int ReadNumberWithinRange(int minRange, int maxRange, string inputMsg) //funkciq za range na chislo
        {
            int n = 0;
            do
            {
                Console.Write(inputMsg);//izvejda msg koeto e podadeno kato argument
                n = Int32.Parse(Console.ReadLine());
                if (n < minRange || n > maxRange)
                {
                    Console.WriteLine("The input should be between " + minRange +" and " + maxRange);
                }
            } while (n < minRange || n > maxRange); // dokato n e izvun range
            return n;
        }
        static void PrintClientInfo(ClientInfo client)  //suzdavame funkciq koqto izkarva vsichki stroinosti na client
        {
            StringBuilder result = new StringBuilder();

            result.Append(client.fullName);
            result.Append(", ");

            result.Append(client.numberOfPurchases);
            result.Append(", ");
            //izvajda do vtori znak sled zapetaq
            result.Append(client.totalPriceOfPurchases.ToString("0.00"));
            result.Append(", ");

            result.Append(client.dayOfReg);
            result.Append('.');
            result.Append(client.monthOfReg);
            result.Append('.');
            result.Append(client.yearOfReg);
            result.Append(", ");
            //* za vsqko chislo ot rating
            result.Append('*', client.rating);

            Console.WriteLine(result);
        }
        
        static void PrintAllClientByName(List<ClientInfo> clients) //printva po azbuchen red imenata na clientite s argument list
        {
            Console.WriteLine("Print all clients by name");
            //sortira po azbuchen red
            clients.Sort(delegate (ClientInfo lhs, ClientInfo rhs)
            {
                return String.Compare(lhs.fullName, rhs.fullName);
            });
            //vseki client ot veche sortiraniq list go izkarva
            foreach (ClientInfo client in clients)
            {
                PrintClientInfo(client);
            }
            Console.WriteLine();
        }
        static void PrintClientsWithRating2(List<ClientInfo> clients) //printva s rating 2
        {
            Console.WriteLine("Printing all clints with rating 2");
            List<ClientInfo> clientsRating2 = GetClientsByRating(clients, 2); //lista e pulen sus sortirani s rating 2
            //sortira po totalprice
            clientsRating2.Sort(delegate (ClientInfo lhs, ClientInfo rhs)
            {
                if (rhs.totalPriceOfPurchases == lhs.totalPriceOfPurchases)
                {
                    return lhs.fullName.CompareTo(rhs.fullName);
                }
                return rhs.totalPriceOfPurchases.CompareTo(lhs.totalPriceOfPurchases);
            });
            //printva ot sortiraniq list
            foreach (ClientInfo client in clientsRating2)
            {
                PrintClientInfo(client);
            }
            Console.WriteLine();
        }
        static void PrintClientNumbersByDateAndRating(List<ClientInfo> clients,int rating) //za godina i rating sortirane
        {

            Console.WriteLine("Print clients number by date and rating");
            //suzdava se nov list s argumenti klient i rating
            List<ClientInfo> clientsByRating = GetClientsByRating(clients,rating);

            //dictionary stoinostite vurvqt ruka za ruka
            // podavame purvata stoinost, izvlichame i vtorata
            Dictionary<int, int> clientsNumberByYear = new Dictionary<int, int>();

            foreach (ClientInfo client in clientsByRating)
            {
                int clientsCount = 0;
                //true if the Dictionary<TKey,TValue> contains an element with the specified key;
                if (clientsNumberByYear.TryGetValue(client.yearOfReg, out clientsCount))
                {
                    clientsNumberByYear.Remove(client.yearOfReg);
                    //kum godinata dobavqme 1
                    clientsNumberByYear.Add(client.yearOfReg, clientsCount + 1);
                }
                else
                {
                    clientsNumberByYear.Add(client.yearOfReg, 1);
                }
            }

            foreach (KeyValuePair<int, int> entry in clientsNumberByYear)
            {
                //entry key - godina
                //entry value - broi
                // do something with entry.Value or entry.Key
                Console.WriteLine(entry.Key + " - " + entry.Value);
            }
        }

        static int ReadNumberOfClients() //za nachalo da dobavish broi klienti
        {
            return ReadNumberWithinRange(1, 5000, "Please enter a number of clients: ");
        }
        static List<ClientInfo> GetClientsByRating(List<ClientInfo> clients, int rating) //sortirane po rating
        {
            List<ClientInfo> clientsByRating = new List<ClientInfo>();//suzdavane na nov list 

            foreach (ClientInfo client in clients)
            {
                if (client.rating == 2)
                {//dobavqne v noviq list
                    clientsByRating.Add(client);
                }
            }
            return clientsByRating;
        }
        static string ReadAndLimitString(int Limit) //limit na stringove
        {
            string result;
            do
            {
                result = Console.ReadLine();

                if (result.Length > Limit)
                {
                    Console.WriteLine("Input should be under " + Limit + " characters");
                }
            } while (result.Length > Limit); //dokato e poveche ot limit shte te kara da go pravish
            return result;
        }

        static void Main(string[] args)
        {
            //suzdavane na nov list ot klienti
            List<ClientInfo> clients = new List<ClientInfo>();
            int numberOfClients = ReadNumberOfClients();
            Console.WriteLine();

            //za i po malko ot broq na klientite
            for (int i = 0; i < numberOfClients; i++)
            {
                ClientInfo client = ReadClientInfo();//prochita infoto koeto vuvejdame
                clients.Add(client);//dobavq v lista
                Console.WriteLine();
            }

            PrintAllClientByName(clients); //izpulnqva funckiqta za clients
            PrintClientsWithRating2(clients); //izpulnqva funckiqta za clients
            int rating = ReadNumberWithinRange(1, 5, "Please enter a number of rating: ");//rating e ravno na funkciqta
            PrintClientNumbersByDateAndRating(clients, rating); //funkciqta e izvinaka za tova chislo

            Console.ReadKey();
        }
    }
}