Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Home_Task
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter num of Clients");
- int ClientNum = int.Parse(Console.ReadLine());
- Console.WriteLine("Please Enter Max num of Clerks you can effort");
- int maxClercks = int.Parse(Console.ReadLine());
- for (int i = 1; i <= maxClercks; i++) //checks 1 to Max clerks
- {
- Console.WriteLine("Checking for {0} Clerks", i);
- Thread.Sleep(1000);
- ClientsQueue clientqueue = new ClientsQueue(ClientNum);
- Clerck[] clercks = Clerck.CreateClercks(i, i * 540);
- Thread.Sleep(2000);
- if (clercks[0].Total > 0)
- {
- Console.WriteLine("Calculation Finished");
- Console.WriteLine("Number of Needed Clerks for {0} Clients is: {1}", ClientNum, i);
- break;
- }
- if ((clercks[0].Total < 0) && (i == maxClercks))
- {
- Console.WriteLine("Calculation Finished");
- Console.WriteLine("You can't have enough clerks to serve all customers");
- break;
- }
- }
- Console.ReadLine();
- }
- }
- }
- // Customer class
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Home_Task
- {
- public class Customer
- {
- public int GetTreatTime()
- {
- Random rnd = new Random();
- return rnd.Next(30, 40);
- }
- }
- }
- // ClientQueue
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Home_Task
- {
- public class ClientsQueue
- {
- private static Queue<Customer> queue;
- public ClientsQueue(int clientNum)//create queue with clients
- {
- queue = new Queue<Customer>();
- for (int i = 0; i < clientNum; i++)
- {
- queue.Enqueue(new Customer());
- }
- }
- public static Customer GetClientFromQueue()//remove client from queue
- {
- lock (queue)
- {
- if (queue.Count() > 0)
- {
- return queue.Dequeue();
- }
- return null;
- }
- }
- }
- }
- //clerck class
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Home_Task
- {
- public class Clerck
- {
- private Thread takeCustomer;
- public int daily;
- private static int _total;
- public int Total
- {
- get { return _total; }
- }
- public static Clerck[] CreateClercks(int numOfClerrcks, int totalDay)
- {
- _total = totalDay;
- Clerck[] clercks = new Clerck[numOfClerrcks];
- for (int i = 0; i < numOfClerrcks; i++)
- {
- clercks[i] = new Clerck();
- }
- return clercks;
- }
- public Clerck()
- {
- this.daily = 540;
- takeCustomer = new Thread(clerckJob);
- takeCustomer.Start();
- }
- public void clerckJob()
- {
- Customer client = ClientsQueue.GetClientFromQueue();
- while ((this.daily > 0) && (client != null)) //checks if day not finished and if left any clients in queue
- {
- int clientTime = client.GetTreatTime();
- this.daily -= clientTime; //subtract time of a single queue from one clerk
- _total -= clientTime; //substract time of a single queue from all clerks
- Console.WriteLine(this.daily);
- Thread.Sleep(100);
- client = ClientsQueue.GetClientFromQueue();
- }
- takeCustomer.Join();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement