Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 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. // Note: I used inheritance because the Turn class have clintsID's (in other words the number of the clint)
  9. // the clerks class had also the clintsID's.
  10.  
  11. namespace Clerks_App
  12. {
  13.     class Clerks :  Turn
  14.     {
  15.         public List<int> clerk = new List<int>();
  16.         public int maxTime { get; set; }
  17.  
  18.         public int sum;
  19.         private Thread thread;
  20.  
  21.         public Clerks()
  22.         {
  23.             sum = 0;
  24.             maxTime = 540;
  25.         }
  26.  
  27.         // Random function between 30-40 in minutes.
  28.         public int RandomTime()
  29.         {
  30.             Random rnd = new Random();
  31.             return rnd.Next(30, 40);
  32.         }
  33.  
  34.         public void ClerkCalling()
  35.         {
  36.             while (clintsNumber != 0)
  37.             {
  38.                 for (int i = 1; i <= clerk.Count; i++)
  39.                 {
  40.                     Thread.Sleep(RandomTime());
  41.                     Console.WriteLine("The clerk number {0} tooks {1} minutes", i, RandomTime());
  42.                     sum += RandomTime();
  43.  
  44.                 }
  45.             }
  46.             Console.WriteLine("Total Time = {0}", sum, clerk.Count);
  47.             IsNeededMoreClerks();
  48.         }
  49.  
  50.         //The clerks calling to the clints, and calculating the totalsum thrugh all the clints
  51.         //after that check if needed more clerks.
  52.         public void ClintTurn()
  53.         {
  54.             for (int i = 1; i <= clints.Count ; i++)
  55.             {
  56.                 Thread.Sleep(RandomTime());
  57.                 Console.WriteLine("The clint number {0} tooks {1} minutes", i*10, RandomTime());
  58.                 sum += RandomTime();
  59.                 clintsNumber--;
  60.             }
  61.         }
  62.  
  63.         //Checking if the input is correct, and adding clerks to the list.
  64.         public void AddClerk()
  65.         {
  66.             try
  67.             {
  68.                 if (clerk.Count == 0)
  69.                 {
  70.                     Console.Write("Enter clerks number: ");
  71.                     int clerkNumber = int.Parse(Console.ReadLine());
  72.                     for (int i = 0; i < clerkNumber; i++)
  73.                     {
  74.                         clerk.Add(i);
  75.                     }
  76.                 }
  77.             }
  78.             catch (Exception ex)
  79.             {
  80.                 Console.WriteLine("Please Try Again: '{0}' \n", ex);
  81.                 Console.WriteLine("-----------------------------------");
  82.                 AddClerk();
  83.             }
  84.            
  85.         }
  86.  
  87.         // calculation how many clerks should be added.
  88.         public int AddNewClerk()
  89.         {
  90.             int addNew = (sum /maxTime) + 1;
  91.             clerk.Add(addNew);
  92.             return (sum/maxTime) + 1;
  93.         }
  94.  
  95.         // Checking if the clerks end by time if not calling other function "AddNewClerk"
  96.         public void IsNeededMoreClerks()
  97.         {
  98.             if (maxTime > sum)
  99.             {
  100.                 Console.WriteLine("Don't needed more Clerks");
  101.             }
  102.             else
  103.             {
  104.                 Console.WriteLine("Must hire {0} more clerks", AddNewClerk());
  105.             }
  106.  
  107.         }
  108.  
  109.         public void Start()
  110.         {
  111.             thread = new Thread(ClintTurn);
  112.             thread.Start();
  113.         }
  114.  
  115.         public void StartClerk()
  116.         {
  117.             thread = new Thread(ClerkCalling);
  118.             thread.Start();
  119.         }
  120.  
  121.         public void Stop()
  122.         {
  123.             thread.Join();
  124.         }
  125.        
  126.  
  127.     }
  128. }
  129. using System;
  130. using System.Collections.Generic;
  131. using System.Linq;
  132. using System.Text;
  133. using System.Threading.Tasks;
  134.  
  135. namespace Clerks_App
  136. {
  137.     class Turn
  138.     {
  139.         public List<int> clints = new List<int>();
  140.         public int clintsNumber { get; set; }
  141.  
  142.        
  143.         // check the input and Adding clints to the list
  144.         public void AddClints()
  145.         {
  146.             try
  147.             {
  148.                 Console.Write("Clints number: ");
  149.                 clintsNumber = int.Parse(Console.ReadLine());
  150.                 for (int i = 0; i < clintsNumber; i++)
  151.                 {
  152.                     clints.Add(i);
  153.                 }
  154.             }
  155.             catch (Exception ex)
  156.             {
  157.                 Console.WriteLine("Please Try Again: " + ex);
  158.                 Console.WriteLine("-----------------------------");
  159.                 AddClints();
  160.             }
  161.            
  162.         }
  163.  
  164.  
  165.  
  166.  
  167.     }
  168. }
  169. using System;
  170. using System.Collections.Generic;
  171. using System.Linq;
  172. using System.Text;
  173. using System.Threading.Tasks;
  174. using System.Threading;
  175.  
  176.  
  177. namespace Clerks_App
  178. {
  179.     class Program
  180.     {
  181.         static void Main(string[] args)
  182.         {
  183.             Clerks c = new Clerks();
  184.             c.AddClerk();
  185.             c.AddClints();
  186.             Console.WriteLine("--------------------------");
  187.             c.StartClerk();
  188.             c.Start();
  189.             Console.WriteLine();
  190.             c.Stop();
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement