示例#1
0
        public static void Main(string[] args)
        {
            Request r = new Request();


            string path = "/Users/noufalrasheed/Desktop/Data.csv";

            int[] InputRequestOutput = new int[4];
            InputRequestOutput = Request.InputRequest();

            int[] readRecordOutput = new int[4];
            readRecordOutput = Request.readRecord(InputRequestOutput[0].ToString(), path, InputRequestOutput[1], InputRequestOutput[2], InputRequestOutput[3]);
            Console.WriteLine("\n\n result");
            string ZipAddress = readRecordOutput[0].ToString();
            int    Acounter   = readRecordOutput[1];
            int    Fcounter   = readRecordOutput[2];
            int    Pcounter   = readRecordOutput[3];

            // build the graph only when help is needed from nearest zipCodes
            if (Acounter != 0 || Fcounter != 0 || Pcounter != 0)
            {
                Graph g = new Graph();
                // read the Nodes(Vertices) from the file and add them to the graph
                using (StreamReader sr = new StreamReader("/Users/noufalrasheed/Desktop/Node.csv"))
                {
                    string line = sr.ReadLine();

                    while (line != null)
                    {
                        string[] strCols = line.Split(',');
                        line = sr.ReadLine();
                        g.AddVertex(strCols[0]);
                    }
                }

                // read the Edges from the file and add them to the graph
                using (StreamReader sr = new StreamReader("/Users/noufalrasheed/Desktop/Edge.csv"))
                {
                    string line = sr.ReadLine();
                    //incase if you want to ignore the header
                    while (line != null)
                    {
                        string[] strCols = line.Split(',');
                        line = sr.ReadLine();
                        string cost  = strCols[2].Trim();
                        int    cost1 = Int32.Parse(cost);
                        g.AddEdge(strCols[0], strCols[1], cost1, true);
                        //(src,dst,cost,bi-direction)
                    }
                }
                // create object for Dijkstra Class
                var dijkstra = new Dijkstra();
                // method to apply Dijkstra Algorithm which takes (graph,src) as arguments.

                var x = dijkstra.ApplyDijkstra(g, ZipAddress);
                // order the output in ascending order based on the smallest cost from src
                var dict = x.OrderBy(i => i.Value).ToDictionary(i => i.Key, i => i.Value.ToString());
                //printing the results
                foreach (var a in dict)
                {
                    Console.WriteLine("ZipCode: {0}\t Cost: {1}", a.Key, a.Value);
                }
                ZipCode temp;
                //create object from InfoRepository class
                var InfoRep = new InfoRepository();
                // read each zipCode information(how many vehicle for each type) and store them under zipCode name
                using (StreamReader sr = new StreamReader("/Users/noufalrasheed/Desktop/Data.csv"))
                {
                    string line = sr.ReadLine();
                    while (line != null)
                    {
                        string[] strCols = line.Split(',');
                        line = sr.ReadLine();
                        temp = new ZipCode(strCols[0]);
                        temp.Infos.Add(new VehiclesInfo("Ambulance", Convert.ToInt32(strCols[1])));
                        temp.Infos.Add(new VehiclesInfo("FireTruck", Convert.ToInt32(strCols[2])));
                        temp.Infos.Add(new VehiclesInfo("PoliceCar", Convert.ToInt32(strCols[3])));
                        InfoRep.Add(temp);
                    }
                }

                // convert the dictionary to 2D-array
                string[,] stringArray2d = new string[2, dict.Count];
                int ii = 0;
                foreach (KeyValuePair <string, string> item in dict)
                {
                    stringArray2d[0, ii] = item.Key;
                    stringArray2d[1, ii] = item.Value;
                    ii++;
                }
                // variable used to store the txt results
                string txt1;
                // help class used to process insufficient vehicles
                Help         h  = new Help();
                StreamWriter sw = File.AppendText("/Users/noufalrasheed/Desktop/Request1.txt");
                // call getHelp for each type in case the return value was greater than 0
                if (Acounter != 0)
                {
                    sw.WriteLine("Getting help from nearest ZipCodes\n");
                    txt1 = Help.getHelp(Acounter, stringArray2d, "Ambulance", InfoRep);
                    sw.WriteLine(txt1);
                }
                txt1 = "";

                if (Fcounter > 0)
                {
                    sw.WriteLine("Getting help from nearest ZipCodes\n");
                    txt1 = Help.getHelp(Fcounter, stringArray2d, "FireTruck", InfoRep);
                    sw.WriteLine(txt1);
                }
                txt1 = "";

                if (Pcounter != 0)
                {
                    sw.WriteLine("Getting help from nearest ZipCodes\n");
                    txt1 = Help.getHelp(Pcounter, stringArray2d, "PoliceCar", InfoRep);
                    sw.WriteLine(txt1);
                }
                sw.WriteLine("\n\nThank you for using our system!");
                sw.Close();
            }
        }
示例#2
0
        public static string getHelp(int theCounter, string[,] stringArray2d, string type, InfoRepository InfoRep)
        {
            string txt  = "";
            int    c    = 1;
            bool   flag = true;
            Random rr   = new Random();
            //int Fcounter = 20; // send by user to ask for help from the nearest zipCodes and it is the main counter to specify how many zipcode needed
            //theCounter = 3;
            int tempCounter = 0;          // counter used to calculate how many vehicle has been used from adjecent zipcodes
            int constant    = theCounter; // constant needed to manage the loop
            int Pcounter1   = theCounter;
            int temp2       = 0;          // counter to hold how many vehicle to use from the last zipcode in case if last zipcode has more vehicles than needed

            while (flag && theCounter > 0)
            {
                string zip_code = stringArray2d[0, c];
                string cost     = stringArray2d[1, c];
                var    tempF    = InfoRep.getQuantity(zip_code, type);
                Console.WriteLine(zip_code + " has total:" + tempF + type);
                txt        += (zip_code + " has total:" + tempF + type + "\n");
                theCounter  = theCounter - tempF;
                tempCounter = tempCounter + tempF;
                if (Pcounter1 < tempF)
                {
                    while (Pcounter1 > 0)
                    {
                        txt += (type + "#" + Pcounter1 + "\t Your VehicalID  is:  " + zip_code + "." + rr.Next(1, 100) + "\n");
                        Console.WriteLine(type + "#" + Pcounter1 + "\t Your VehicalID  is:  " + zip_code + "." + rr.Next(1, 100) + "\n");
                        //Console.WriteLine(Pcounter1);
                        Pcounter1--;
                    }
                }
                //Console.WriteLine("temp cnt" + tempCounter);
                if (tempCounter > constant)
                {
                    temp2 = tempCounter - tempF;
                    int temp3 = tempCounter - constant;
                    int temp4 = temp2 - temp3 - 1;
                    while (temp4 >= 1 && Pcounter1 > 0)
                    {
                        txt += (type + "#" + Pcounter1 + " \t Your VehicalID  is:  " + zip_code + "." + rr.Next(1, 100) + "\n");
                        Console.WriteLine(type + "#" + Pcounter1 + " \t Your VehicalID  is:  " + zip_code + "." + rr.Next(1, 100) + "\n");
                        //Console.WriteLine(Pcounter1);//output
                        Pcounter1--;
                        temp4--;
                    }
                }
                while (tempCounter <= constant && tempF > 0)
                {
                    txt += (type + "#" + Pcounter1 + " \t Your VehicalID  is:  " + zip_code + "." + rr.Next(1, 100) + "\n");
                    Console.WriteLine(type + "#" + Pcounter1 + " \t Your VehicalID  is:  " + zip_code + "." + rr.Next(1, 100) + "\n");
                    //Console.WriteLine(Pcounter1);//output
                    Pcounter1--;
                    tempF--;
                }

                if (theCounter > 0)
                {
                    c++;
                }
                if (theCounter == 0)
                {
                    //sw.Close();
                    flag = false;
                }
            }
            return(txt);
        }