Ejemplo n.º 1
0
        public void TestMethod1()
        {
            WeightConverter obj = new WeightConverter();

            double expected = 0.1410958478;
            double actual   = obj.GramToOunces(4);
            double delta    = 0.0000058478;

            Assert.AreEqual(expected, actual, delta);

            double expected2 = 198.44664;
            double actual2   = obj.OuncesToGram(7);
            double delta2    = 0.0006;

            Assert.AreEqual(expected2, actual2, delta2);
        }
Ejemplo n.º 2
0
        private static void DoComunicate(TcpClient client)
        {
            NetworkStream stream = client.GetStream();
            StreamReader  reader = new StreamReader(stream);
            StreamWriter  writer = new StreamWriter(stream);

            while (true)
            {
                ClassLibraryMandatory.WeightConverter obj = new WeightConverter();

                string request = reader.ReadLine();

                if (request != null)
                {
                    Console.WriteLine("Request: " + request);

                    string   response = null;
                    string[] myArray  = request.Split(' ');

                    if (request.Split(' ').Length == 2)
                    {
                        if ((myArray[0] == "TOGRAM" || myArray[0] == "TOOUNCE") && myArray[1] != null)
                        {
                            try
                            {
                                double value = double.Parse(myArray[1]);

                                if (myArray[1] == "TOGRAM")
                                {
                                    response = obj.OuncesToGram(value).ToString();
                                }
                                else
                                {
                                    response = obj.GramToOunces(value).ToString();
                                }

                                Console.WriteLine("Responce: " + response);
                                writer.WriteLine(response + "\n ");
                                Console.WriteLine();
                                writer.WriteLine();
                            }
                            catch (Exception)
                            {
                                Console.WriteLine("WRONG INPUT");
                                writer.WriteLine("Please inseart valid number.");
                                Console.WriteLine();
                                writer.WriteLine();
                                writer.Flush();
                                continue;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Available actions: \nTOGRAM [number]\nTOOUNCE [number]");
                            writer.WriteLine("No such action available");
                            Console.WriteLine();
                            writer.WriteLine();
                        }
                    }
                    if (request == "STOP")
                    {
                        break;
                    }
                    else
                    {
                        if (request.Split(' ').Length != 2)
                        {
                            Console.WriteLine("No such action available");
                            Console.WriteLine();
                            writer.WriteLine("No such action available");
                            writer.WriteLine();
                        }
                    }
                    writer.Flush();
                }
            }
            client.Close();
            Console.WriteLine("Client disconnected.\nWaiting...");
        }