static void Main(string[] args) { 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|0.33" string[] cerealInformation = line.Split('|'); // 0 1 2 3 //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(c); } Console.WriteLine("\nAll cereals that have 1 cup or more for the serving size"); foreach (Cereal cereal in bowls) { if (cereal.Cups >= 1) { Console.WriteLine(cereal); } } Console.WriteLine("\nAll cereals that have 100 calories or fewer per serving"); foreach (Cereal cereal in bowls) { if (cereal.Calories <= 100) { Console.WriteLine(cereal); } } }
static void Main(string[] args) { List <Cereal> bowls = new List <Cereal>(); string[] lines = File.ReadAllLines("Cereal_Data.txt"); //[0] = "name|manufacturer|calories|cups" -- for loop //[1] = "100% Bran|Nabisco|70|0.33" //Read in all data from the dile and make Cereal objects and add those to ur bowls collection for (int i = 1; i < lines.Length; i++) { string line = lines[i]; //Line = "100% Bran|Nabisco|70|0.33" string[] pieces = line.Split('|'); // 0 1 2 3 //pieces = {"100% Bran", "Nabisco", "70", "0.33"} Cereal c = new Cereal(); c.Name = pieces[0]; c.Manufacturer = pieces[1]; c.Calories = Convert.ToDouble(pieces[2]); //must convert string to double c.Cups = Convert.ToDouble(pieces[3]); //must convert string to double bowls.Add(c); } Console.WriteLine("Cereals with a serving size greater than or equal to 1:"); //Go through bowls and output desired cereals foreach (Cereal bowl in bowls) { if (bowl.Cups >= 1) { Console.WriteLine(bowl); } } Console.WriteLine("\nCereals that have 100 calories or less per serving"); foreach (Cereal bowl in bowls) { if (bowl.Calories <= 100) { Console.WriteLine(bowl); } } }
static void Main(string[] args) { //We want to read in a data set of Cereal information and help answer a few questions for the user. //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"); List <Cereal> bowls = new List <Cereal>(); for (int i = 1; i < lines.Length; i++) { string[] breaks = lines[i].Split("|"); Cereal C = new Cereal(); C.Name = breaks[0]; C.Manufacturer = breaks[1]; C.Calories = Convert.ToDouble(breaks[2]); C.Cups = Convert.ToDouble(breaks[3]); bowls.Add(C); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("The following cereals have a serving size that is 1 cup or more:"); Console.ForegroundColor = ConsoleColor.White; foreach (Cereal cereal in bowls) { if (cereal.Cups >= 1) { Console.WriteLine(cereal.ToString()); } } Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("The following cereals have 100 calories or less per serving:"); Console.ForegroundColor = ConsoleColor.White; foreach (Cereal cereal in bowls) { if (cereal.Calories <= 100) { Console.WriteLine(cereal.ToString()); } } }
static void Main(string[] args) { //100% on exam looking at file and reading it in. To get file: Right click project, add existing item. Filter all files, select. Properties, change to copy always string[] lines = File.ReadAllLines("Cereal_Data.txt"); // 0 1 2 3 //lines[0] = "name|manufacturer|calories|cups"; //anytime you have a collection: dictionary, array, etc you need a loop to iterate through //would do a foreach except we aren't processing anything List <Cereal> bowls = new List <Cereal>(); for (int i = 1; i < lines.Length; i++) { string line = lines[i]; string[] cerealpieces = line.Split('|'); string name = cerealpieces[0]; Cereal c = new Cereal(); c.Name = name; c.Manufacturer = cerealpieces[1]; c.Calories = Convert.ToDouble(cerealpieces[2]); c.Cups = Convert.ToDouble(cerealpieces[3]); bowls.Add(c); // Console.WriteLine(c.Name); if (c.Cups >= 1) { Console.WriteLine("Cereal Information:"); Console.WriteLine(c.Name); Console.WriteLine(c.Manufacturer); Console.WriteLine(c.Calories); Console.WriteLine(c.Cups); Console.WriteLine("\n"); } else { } } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("These cereals have no more than 100 calories"); Console.ForegroundColor = ConsoleColor.White; for (int i = 1; i < lines.Length; i++) { string line = lines[i]; string[] cerealpieces = line.Split('|'); string name = cerealpieces[0]; Cereal c = new Cereal(); c.Name = name; c.Manufacturer = cerealpieces[1]; c.Calories = Convert.ToDouble(cerealpieces[2]); c.Cups = Convert.ToDouble(cerealpieces[3]); bowls.Add(c); // Console.WriteLine(c.Name); if (c.Calories <= 100) { Console.WriteLine("Cereal Information:"); Console.WriteLine(c.Name); Console.WriteLine(c.Manufacturer); Console.WriteLine(c.Calories); Console.WriteLine(c.Cups); Console.WriteLine("\n"); } else { } } }