Esempio n. 1
0
        /// <summary>
        /// Read from the keyboard the country with the annual production of electricity.
        /// </summary>
        /// <returns> An array with all the countries and their annual production. </returns>
        public static Country[] ReadFromKeyboard()
        {
            var length    = ReadInteger("Enter the number of countries:");
            var countries = new Country[length];

            for (int i = 0; i < countries.Length; i++)
            {
                countries[i] = new Country();

                var name = ReadString("Country name:");
                countries[i].Name = name;

                var           number      = ReadLong("Production of electricity:");
                NaturalNumber electricity = new NaturalNumber(number);
                countries[i].MonthlyProductionElectricalCurrent = electricity;
            }
            return(countries);
        }
Esempio n. 2
0
        /// <summary>
        /// Read line by line a text file, every line represents a country with the annual production of electricity.
        /// </summary>
        /// <returns> An array with all the countries and their annual production. </returns>
        public static Country[] ReadFromFile()
        {
            int numberOfLines;

            Country[] countries = null;

            try
            {
                using (StreamReader sr = new StreamReader(@"C:\Users\USER\Desktop\Work\PUModule2JavaIntro\CurentElectric-homework\Country.txt"))
                {
                    var line = sr.ReadLine();
                    numberOfLines = int.Parse(line);

                    countries = new Country[numberOfLines];

                    var i = 0;
                    while ((line = sr.ReadLine()) != null)
                    {
                        //bool isParsable = int.TryParse(line, out numberOfLines);
                        //if (!isParsable) Console.WriteLine("Please enter a number equal with the number of countries!");
                        //Console.WriteLine($"number of lines = {numberOfLines}");
                        var categories        = line.Split(",", StringSplitOptions.RemoveEmptyEntries);
                        var name              = categories[0];
                        var monthlyProduction = (long)int.Parse(categories[1]);

                        NaturalNumber electricity = new NaturalNumber(monthlyProduction);
                        countries[i]      = new Country();
                        countries[i].Name = name;
                        countries[i].MonthlyProductionElectricalCurrent = electricity;

                        i++;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            return(countries);
        }
Esempio n. 3
0
 public Country(string name, long monthlyProduction)
 {
     this.name = name;
     this.monthlyProductionElectricalCurrent = new NaturalNumber(monthlyProduction);
 }