Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Home_Task
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.WriteLine("Enter num of Clients");
  15.             int ClientNum = int.Parse(Console.ReadLine());
  16.             Console.WriteLine("Please Enter Max num of Clerks you can effort");
  17.             int maxClercks = int.Parse(Console.ReadLine());
  18.             for (int i = 1; i <= maxClercks; i++)  //checks 1 to Max clerks
  19.             {
  20.                 Console.WriteLine("Checking for {0} Clerks", i);
  21.                 Thread.Sleep(1000);
  22.                 ClientsQueue clientqueue = new ClientsQueue(ClientNum);
  23.                 Clerck[] clercks = Clerck.CreateClercks(i, i * 540);
  24.                 Thread.Sleep(2000);
  25.                 if (clercks[0].Total > 0)
  26.                 {
  27.                     Console.WriteLine("Calculation Finished");
  28.                     Console.WriteLine("Number of Needed Clerks for {0} Clients is: {1}", ClientNum, i);
  29.                     break;
  30.                 }
  31.                 if ((clercks[0].Total < 0) && (i == maxClercks))
  32.                 {
  33.                     Console.WriteLine("Calculation Finished");
  34.                     Console.WriteLine("You can't have enough clerks to serve all customers");
  35.                     break;
  36.                 }
  37.             }
  38.             Console.ReadLine();
  39.         }
  40.     }
  41. }
  42.  
  43.  
  44. // Customer class
  45.  
  46. using System;
  47. using System.Collections.Generic;
  48. using System.Linq;
  49. using System.Text;
  50. using System.Threading.Tasks;
  51.  
  52. namespace Home_Task
  53. {
  54.     public class Customer
  55.     {
  56.         public int GetTreatTime()
  57.         {
  58.             Random rnd = new Random();
  59.             return rnd.Next(30, 40);
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65. // ClientQueue
  66.  
  67. using System;
  68. using System.Collections;
  69. using System.Collections.Generic;
  70. using System.Linq;
  71. using System.Text;
  72. using System.Threading.Tasks;
  73.  
  74. namespace Home_Task
  75. {
  76.     public class ClientsQueue
  77.     {
  78.         private static Queue<Customer> queue;
  79.         public ClientsQueue(int clientNum)//create queue with clients
  80.         {
  81.             queue = new Queue<Customer>();
  82.  
  83.             for (int i = 0; i < clientNum; i++)
  84.             {
  85.                 queue.Enqueue(new Customer());
  86.             }
  87.         }
  88.         public static Customer GetClientFromQueue()//remove client from queue
  89.         {
  90.             lock (queue)
  91.             {
  92.                 if (queue.Count() > 0)
  93.                 {
  94.                     return queue.Dequeue();
  95.                 }
  96.                 return null;
  97.             }
  98.         }
  99.     }
  100. }
  101.  
  102. //clerck class
  103.  
  104. using System;
  105. using System.Collections.Generic;
  106. using System.Linq;
  107. using System.Text;
  108. using System.Threading;
  109. using System.Threading.Tasks;
  110.  
  111.  
  112. namespace Home_Task
  113. {
  114.     public class Clerck
  115.     {
  116.         private Thread takeCustomer;
  117.         public int daily;
  118.         private static int _total;
  119.         public int Total
  120.         {
  121.             get { return _total; }
  122.         }
  123.         public static Clerck[] CreateClercks(int numOfClerrcks, int totalDay)
  124.         {
  125.             _total = totalDay;
  126.             Clerck[] clercks = new Clerck[numOfClerrcks];
  127.             for (int i = 0; i < numOfClerrcks; i++)
  128.             {
  129.                 clercks[i] = new Clerck();
  130.             }
  131.             return clercks;
  132.         }
  133.  
  134.         public Clerck()
  135.         {
  136.             this.daily = 540;
  137.             takeCustomer = new Thread(clerckJob);
  138.             takeCustomer.Start();
  139.         }
  140.  
  141.         public void clerckJob()
  142.         {
  143.             Customer client = ClientsQueue.GetClientFromQueue();
  144.             while ((this.daily > 0) && (client != null)) //checks if day not finished and if left any clients in queue
  145.             {
  146.                 int clientTime = client.GetTreatTime();
  147.                 this.daily -= clientTime; //subtract time of a single queue from one clerk
  148.                 _total -= clientTime; //substract time of a single queue from all clerks
  149.                 Console.WriteLine(this.daily);
  150.                 Thread.Sleep(100);
  151.                 client = ClientsQueue.GetClientFromQueue();
  152.  
  153.             }
  154.             takeCustomer.Join();
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement