SHARE
TWEET

Untitled

a guest May 23rd, 2019 81 in 4 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TieusMethod
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //Read Orders
  12.             Console.Write("How many orders will there be?\nn=");
  13.             int numberOfOrders = int.Parse(Console.ReadLine());
  14.             List<Order> Orders = new List<Order>();
  15.             TakeOrders(numberOfOrders, Orders);
  16.             Kitchen.NextOrder = Orders.OrderBy(o => o.PlaceTime).ThenByDescending(o => o.CookTime).First();
  17.             ReciteOrders(Orders);
  18.  
  19.             //Initialize
  20.             Kitchen.IsCooking = false;
  21.  
  22.  
  23.  
  24.             ////Kitchen Cycle
  25.             while (Orders.Count > Kitchen.OrdersServed.Count)
  26.             {
  27.                 //Kitchen Registers Orders
  28.                 var ordersPlacedNow = Orders.Where(o => o.PlaceTime == Kitchen.KitchenTime).ToList();
  29.                 if (ordersPlacedNow != null)
  30.                 {
  31.                     foreach (var order in ordersPlacedNow)
  32.                     {
  33.                         Kitchen.OrdersInQueue.Add(order);
  34.                         Console.WriteLine($"Added Order of Time:{order.PlaceTime} to Queue");
  35.  
  36.                         Kitchen.UpdateNextOrder();
  37.                     }
  38.                 }
  39.  
  40.                 //Make sure something is cooking
  41.                 if (Kitchen.OrderInProgress == null)
  42.                 {
  43.                     Kitchen.UpdateNextOrder();
  44.                     Kitchen.OrderInProgress = Kitchen.NextOrder;
  45.                     Kitchen.OrdersInQueue.Remove(Kitchen.NextOrder);
  46.  
  47.                     Console.WriteLine($"Order done.");
  48.  
  49.                     Kitchen.UpdateNextOrder();
  50.                 }
  51.  
  52.                 //Swap Orders if done
  53.                 if (Kitchen.OrderInProgress.CookTime == 0)
  54.                 {
  55.                     Kitchen.OrderInProgress.IsDone = true;
  56.                     Kitchen.OrdersServed.Add(Kitchen.OrderInProgress);
  57.                     Console.WriteLine("Added finished order to OrdersServed");
  58.                     Kitchen.OrdersInQueue.Remove(Kitchen.OrderInProgress);
  59.                 }
  60.                 Kitchen.UpdateNextOrder();
  61.                 if (Kitchen.OrderInProgress.IsDone && Kitchen.NextOrder != null)
  62.                 {
  63.                     Kitchen.OrderInProgress = Kitchen.NextOrder;
  64.                     Kitchen.UpdateNextOrder();
  65.                 }
  66.  
  67.                 //Tick the timers
  68.                 Console.WriteLine("Cycle end");
  69.                 foreach (var order in Orders.Where(o => o.PlaceTime <= Kitchen.KitchenTime && !o.IsDone))
  70.                 {
  71.                     order.WaitTime++;
  72.                     Console.WriteLine("Order placed at "+order.PlaceTime+ " has waited" + order.WaitTime);
  73.                 }
  74.                     Kitchen.KitchenTime++;
  75.                 Console.WriteLine("Kitchen Time:" + Kitchen.KitchenTime);
  76.                 if (Kitchen.OrderInProgress != null)
  77.                 {
  78.                     Kitchen.OrderInProgress.CookTime--;
  79.                     Console.WriteLine("Cooking order still has "+ Kitchen.OrderInProgress.CookTime +" minutes to cook.");
  80.                 }
  81.                 Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~");
  82.                 Console.ReadKey();
  83.             }
  84.  
  85.             #region Old Logic Remnants
  86.             //    if (!Kitchen.IsCooking)
  87.             //    {
  88.             //        var order = Orders.Single(o => o.PlaceTime == NextOrder);
  89.             //        order.IsDone = true;
  90.             //        orderOfCooking[orderOfCookingIndex] = order.PlaceTime;
  91.             //        orderOfCookingIndex++;
  92.             //        Kitchen.IsCooking = true;
  93.             //        Timer = order.CookTime;
  94.             //    }
  95.             //    NextOrder = Tick(Orders, TimeElasped);
  96.             //    Kitchen.IsCooking = Timer == 0 ? false : true;
  97.             //}
  98.             #endregion
  99.  
  100.             AverageWaitTime(numberOfOrders, Orders);
  101.  
  102.         }
  103.  
  104.         private static void AverageWaitTime(int numberOfCustomers, List<Order> Orders)
  105.         {
  106.             int avrgWaitTime = 0;
  107.             foreach (var order in Orders)
  108.                 avrgWaitTime += order.WaitTime;
  109.              avrgWaitTime/= numberOfCustomers;
  110.  
  111.             Console.WriteLine("The average wait time on orders was:\t" + avrgWaitTime + " minutes.");
  112.         }
  113.  
  114.         private static void ReciteOrders(List<Order> Orders)
  115.         {
  116.             foreach (var order in Orders)
  117.             {
  118.                 Console.WriteLine($"\nOrder placed at time: {order.PlaceTime} cooks in: {order.CookTime} minutes.");
  119.             }
  120.         }
  121.  
  122.         private static void TakeOrders(int numberOfCustomers, List<Order> Orders)
  123.         {
  124.             for (int i = 0; i < numberOfCustomers; i++)
  125.             {
  126.                 Console.Write("What time was Order #" + i + " placed at?");
  127.                 int placeTime = int.Parse(Console.ReadLine());
  128.                 Console.Write("How much time will it take to cook Order #" + i + "?");
  129.                 int cookTime = int.Parse(Console.ReadLine());
  130.                 Orders.Add(new Order(placeTime, cookTime));
  131.             }
  132.         }
  133.     }
  134.     public class Order
  135.     {
  136.         public int CookTime, PlaceTime;
  137.         public bool IsDone = false;
  138.         public int WaitTime = 0;
  139.  
  140.         public Order(int place, int cook)
  141.         {
  142.             CookTime = cook;
  143.             PlaceTime = place;
  144.         }
  145.     }
  146.  
  147.     public static class Kitchen
  148.     {
  149.         public static Order OrderInProgress;
  150.         public static int KitchenTime = 0;
  151.         public static List<Order> OrdersInQueue = new List<Order>();
  152.         public static List<Order> OrdersServed = new List<Order>();
  153.         public static bool IsCooking=false;
  154.         public static Order NextOrder;
  155.  
  156.         public static void UpdateNextOrder()
  157.         {
  158.             if (OrdersInQueue.Any())
  159.             {
  160.                 int minimumWaitTimeAverage = 0;
  161.                 foreach (var order in OrdersInQueue)
  162.                     minimumWaitTimeAverage += order.WaitTime + NextOrder.CookTime;
  163.                 minimumWaitTimeAverage /= OrdersInQueue.Count;
  164.                
  165.  
  166.                 foreach (var prospectiveNextOrder in OrdersInQueue)
  167.                 {
  168.                     int waitTimeSum = 0;
  169.                     foreach (var order in OrdersInQueue)
  170.                     {
  171.                         waitTimeSum += order.WaitTime + prospectiveNextOrder.CookTime;
  172.                     }
  173.                     int propsectiveOrderWaitTimeAverage = waitTimeSum / OrdersInQueue.Count;
  174.                     NextOrder = propsectiveOrderWaitTimeAverage < minimumWaitTimeAverage ? prospectiveNextOrder : NextOrder;
  175.                 }
  176.             }
  177.         }
  178.     }
  179. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top