Beispiel #1
0
 /// <summary>
 /// Prints out a single action string
 /// </summary>
 /// <param name="actionString">Action String to print out</param>
 /// <param name="finalState">Specifies the Final Time or State in the Elevator System</param>
 public static void PrintValidActionString(string actionString, int finalState)
 {
     //Check if the Action String is valid before printing
     if (BuildingUtilityClass.IsValidActionString(actionString, finalState))
     {
         Console.WriteLine(actionString);
     }
     else
     {
         Console.Error.WriteLine("No Solution");
     }
 }
Beispiel #2
0
        /// <summary>
        /// Prints out all action strings in the ValidActionStringsList Collection
        /// </summary>
        /// <param name="validActionStringsList">Specifies a Collection of valid action strings</param>
        /// <param name="finalState">Specifies the Final Time or State in the Elevator System</param>
        public static void PrintValidActionStrings(ArrayList validActionStringsList, int finalState)
        {
            if (validActionStringsList.Count == 0)
            {
                throw new Exception("No Solution");
            }

            foreach (string actionString in validActionStringsList)
            {
                //Check if the Action String is valid before printing
                if (BuildingUtilityClass.IsValidActionString(actionString, finalState))
                {
                    Console.WriteLine(actionString);
                }
                else
                {
                    Console.Error.WriteLine("No Solution");
                }
            }
        }
        /// <summary>
        /// Recursive method to build action string
        /// </summary>
        /// <param name="treeNode">Current Node to check in the Tree</param>
        private void FindActionPathToFinalFloorAtFinalState(TreeNode treeNode)
        {
            foreach (TreeNode tn in treeNode.Nodes)
            {
                //Get the current elevator object specified by the current node in the tree
                Elevator currentElevatorNode = (Elevator)tn.Tag;

                //If the current elevator doesnt belongs to the Final Floor at State T,
                //continue to search the tree
                if (!IsCurrentElevatorAFinalNode(currentElevatorNode))
                {
                    FindActionPathToFinalFloorAtFinalState(tn);
                }
                else
                {
                    //If the current elevator does belong to the Final foor at State T,
                    //create an Action string by retrieving all Parent Node values
                    TreeNode currentNode  = tn.Parent;
                    string   actionString = String.Empty;

                    while (currentNode.Text != "RootNode")
                    {
                        actionString += currentNode.Text;
                        currentNode   = currentNode.Parent;
                    }

                    //Reverse the ActionString since it was build from Final Node to Root Node
                    actionString = BuildingUtilityClass.ReverseString(actionString);

                    //Add the action string to an ActionString list of valid actions
                    if (!ActionStringList.Contains(actionString))
                    {
                        if (actionString != String.Empty)
                        {
                            ActionStringList.Add(actionString);
                        }
                    }
                }
            }
        }
        //Main Entry Point of the Elevator System Console Application
        static void Main(string[] args)
        {
            try
            {
                ArrayList validActionStringsList = new ArrayList();

                ParseCommandLineArgs(args);

                //The Command to Invoke the Program must be called InvokeProgram
                if (InvokeProgramCommand.Equals("InvokeProgram"))
                {
                    /*Create an object encasulating elevator states representative of the
                     * correctly formatted Input File*/
                    ElevatorState elevatorStateCollection = new ElevatorState(ElevatorSystemFilePath);

                    //Get a list of valid action strings
                    validActionStringsList = elevatorStateCollection.FindValidActionStrings(StartingElevator, FinalFloor, FinalTime);

                    /*If there were valid action strings that were found print them out
                     * Otherwise, throw an exception since no input matched the Command Line arguments*/
                    if (validActionStringsList != null)
                    {
                        BuildingUtilityClass.PrintValidActionStrings(validActionStringsList, FinalTime);
                    }
                    else
                    {
                        throw new Exception("No Solution");
                    }
                }
                else
                {
                    throw new Exception("No Solution");
                }
            }
            catch (Exception ex)
            {
                //If there is no solution, print out "No Solution" to stderr or Console.Error and nothing to Console.WriteLine
                Console.Error.WriteLine("No Solution");
            }
        }