static void Main(string[] args) { /*The application should output all of the Cereal information that have a serving size that is 1 * cup or more. After you output all those cereals, we also need to inform them which Cereals have 100 * calories or less per serving.*/ string[] lines = File.ReadAllLines("Cereal_Data.txt"); // 0 1 2 3 //lines[0] = "name|manufacturer|calories|cups"; List <Cereal> bowls = new List <Cereal>(); for (int i = 1; i < lines.Length; i++) { string line = lines[i]; //line = "100% Bran | Nabisco | 70|.33" string[] cerealInformation = line.Split('|'); //CerealInformation = {"100% Bran","Nabisco","70","0.33"} string name = cerealInformation[0]; Cereal c = new Cereal(); c.Name = name; c.Manufacturer = cerealInformation[1]; c.Calories = Convert.ToDouble(cerealInformation[2]); c.Cups = Convert.ToDouble(cerealInformation[3]); bowls.Add(c); } Console.WriteLine("List of cereal information for cereals that have 1 or more cups per serving:"); Console.WriteLine(" "); foreach (Cereal cereal in bowls) { if (cereal.Cups >= 1) { Console.WriteLine(cereal); } } Console.WriteLine(" "); Console.WriteLine("List of cereal information for cereals that have 100 or less calories per serving:"); Console.WriteLine(" "); foreach (Cereal cereal in bowls) { if (cereal.Calories <= 100) { Console.WriteLine(cereal); } } }
static void Main(string[] args) { string[] lines = File.ReadAllLines("Cereal_Data.txt"); //line[0] = "name|manufacturer|calories|cups" List <Cereal> bowls = new List <Cereal>(); for (int i = 1; i < lines.Length; i++) { string line = lines[i]; string[] cerealInfo = line.Split('|'); string name = cerealInfo[0]; Cereal c = new Cereal(); c.Name = name; c.Manufacturer = cerealInfo[1]; c.Calories = Convert.ToDouble(cerealInfo[2]); c.Cups = Convert.ToDouble(cerealInfo[3]); //Console.WriteLine(Has a serivng size of 1 or more cups); //I attempted to have this not repeat but it just kept seperating by brands ending up just changing color to green for those with calories less than 100 if (Convert.ToDouble(c.Cups) >= 1) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.Write($"{c.Name}"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); if (Convert.ToDouble(c.Calories) <= 100) { Console.ForegroundColor = ConsoleColor.Green; Console.Write("\t has less than 100 calories"); Console.ForegroundColor = ConsoleColor.White; } else { Console.Write("\t"); // just to match format } Console.Write(c.ToString()); } } }
static void Main(string[] args) { string[] data = File.ReadAllLines("Cereal_Data (1).txt"); List <Cereal> cereals = new List <Cereal>(); for (int i = 1; i < data.Length; i++) { string onedata = data[i]; string[] pieces = onedata.Split("|"); //name|manufacturer|calories|cups // 0 1 2 3 Cereal c = new Cereal(); c.Manufacturer = pieces[1]; c.Name = pieces[0]; c.Calories = Convert.ToDouble(pieces[2]); c.Cups = Convert.ToDouble(pieces[3]); cereals.Add(c); } Console.WriteLine("\nAll cereals with a serving size of 1 cup or more:"); foreach (Cereal cereal in cereals) { if (cereal.Cups >= 1) { Console.WriteLine(cereal); } } Console.WriteLine("\nAll cereals with 100 calories or less per serving:"); foreach (Cereal cereal in cereals) { if (cereal.Calories <= 100) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(cereal); Console.ResetColor(); } } }