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;
- // Note: I used inheritance because the Turn class have clintsID's (in other words the number of the clint)
- // the clerks class had also the clintsID's.
- namespace Clerks_App
- {
- class Clerks : Turn
- {
- public List<int> clerk = new List<int>();
- public int maxTime { get; set; }
- public int sum;
- private Thread thread;
- public Clerks()
- {
- sum = 0;
- maxTime = 540;
- }
- // Random function between 30-40 in minutes.
- public int RandomTime()
- {
- Random rnd = new Random();
- return rnd.Next(30, 40);
- }
- public void ClerkCalling()
- {
- while (clintsNumber != 0)
- {
- for (int i = 1; i <= clerk.Count; i++)
- {
- Thread.Sleep(RandomTime());
- Console.WriteLine("The clerk number {0} tooks {1} minutes", i, RandomTime());
- sum += RandomTime();
- }
- }
- Console.WriteLine("Total Time = {0}", sum, clerk.Count);
- IsNeededMoreClerks();
- }
- //The clerks calling to the clints, and calculating the totalsum thrugh all the clints
- //after that check if needed more clerks.
- public void ClintTurn()
- {
- for (int i = 1; i <= clints.Count ; i++)
- {
- Thread.Sleep(RandomTime());
- Console.WriteLine("The clint number {0} tooks {1} minutes", i*10, RandomTime());
- sum += RandomTime();
- clintsNumber--;
- }
- }
- //Checking if the input is correct, and adding clerks to the list.
- public void AddClerk()
- {
- try
- {
- if (clerk.Count == 0)
- {
- Console.Write("Enter clerks number: ");
- int clerkNumber = int.Parse(Console.ReadLine());
- for (int i = 0; i < clerkNumber; i++)
- {
- clerk.Add(i);
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Please Try Again: '{0}' \n", ex);
- Console.WriteLine("-----------------------------------");
- AddClerk();
- }
- }
- // calculation how many clerks should be added.
- public int AddNewClerk()
- {
- int addNew = (sum /maxTime) + 1;
- clerk.Add(addNew);
- return (sum/maxTime) + 1;
- }
- // Checking if the clerks end by time if not calling other function "AddNewClerk"
- public void IsNeededMoreClerks()
- {
- if (maxTime > sum)
- {
- Console.WriteLine("Don't needed more Clerks");
- }
- else
- {
- Console.WriteLine("Must hire {0} more clerks", AddNewClerk());
- }
- }
- public void Start()
- {
- thread = new Thread(ClintTurn);
- thread.Start();
- }
- public void StartClerk()
- {
- thread = new Thread(ClerkCalling);
- thread.Start();
- }
- public void Stop()
- {
- thread.Join();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Clerks_App
- {
- class Turn
- {
- public List<int> clints = new List<int>();
- public int clintsNumber { get; set; }
- // check the input and Adding clints to the list
- public void AddClints()
- {
- try
- {
- Console.Write("Clints number: ");
- clintsNumber = int.Parse(Console.ReadLine());
- for (int i = 0; i < clintsNumber; i++)
- {
- clints.Add(i);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Please Try Again: " + ex);
- Console.WriteLine("-----------------------------");
- AddClints();
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace Clerks_App
- {
- class Program
- {
- static void Main(string[] args)
- {
- Clerks c = new Clerks();
- c.AddClerk();
- c.AddClints();
- Console.WriteLine("--------------------------");
- c.StartClerk();
- c.Start();
- Console.WriteLine();
- c.Stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement