Example #1
0
        static void Main(string[] args)
        {
            bool      firstRun    = true;
            SuperBowl mySuperBowl = new SuperBowl();

            while (firstRun == true && mySuperBowl.welcome() == false)
            {
                mySuperBowl.getText(@"Super_Bowl_Project.csv");
                mySuperBowl.writeFile(mySuperBowl.generateFile());
                Console.ReadKey();
                Console.WriteLine("Press any Key to Continue....");
                firstRun = false;
            }
        }
        public string yearManager(SuperBowl thing)
        {
            string year = "";

            for (int i = 0; i < a; i++)
            {
                year = Convert.ToString(thing.date.Split("-")[2]);
                if (Convert.ToInt32(year) >= 66)
                {
                    year = "19" + year;
                }
                else if (Convert.ToInt32(year) <= 66)
                {
                    year = "20" + year;
                }
            }
            a++;
            return(year);
        }
        static void Writer3(ref StreamWriter write, ref List <SuperBowl> listOfSuperBowls)
        {
            var groupQuery = from superBowl in listOfSuperBowls
                             group superBowl by superBowl.State into stateGroup
                             orderby stateGroup.Count() descending
                             select new { aGroup = stateGroup.Key, Count = stateGroup.Count() };
            var Taker = groupQuery.Take(1);

            foreach (var superBowl in Taker)
            {
                write.Write($"The state that has hosted the most superbowls is {superBowl.aGroup}. They have hosted {superBowl.Count} superbowls.\n");
            }

            //This is crude, how to do using LINQ
            foreach (var SuperBowl in listOfSuperBowls)
            {
                if (SuperBowl.State == "Florida")
                {
                    write.Write(SuperBowl.HighestState() + "\n");
                }
            }
        }
Example #4
0
        //Method helps assign values to values list
        public static SuperBowl FromCsv(string csvLine)
        {
            string[]  values    = csvLine.Split(',');
            SuperBowl superBowl = new SuperBowl();

            superBowl.Date            = Convert.ToDateTime(values[0]);
            superBowl.romanNumeral    = values[1];
            superBowl.Attendance      = Convert.ToInt32(values[2]);
            superBowl.winningQB       = values[3];
            superBowl.winningCoach    = values[4];
            superBowl.winningTeam     = values[5];
            superBowl.winningPts      = Convert.ToInt32(values[6]);
            superBowl.losingQB        = values[7];
            superBowl.losingCoach     = values[8];
            superBowl.losingTeam      = values[9];
            superBowl.losingPts       = Convert.ToInt32(values[10]);
            superBowl.MVP             = values[11];
            superBowl.pointDifference = Convert.ToInt32(values[6]) - Convert.ToInt32(values[10]);
            superBowl.hostStadium     = values[12];
            superBowl.hostCity        = values[13];
            superBowl.hostState       = values[14];
            return(superBowl);
        }
        //Method that writes to filePath2(includes all queries)
        static void ReadWriteFiles(string adjustedFilePath, string filePath2)
        {
            //Create list of SuperBowl objects
            List <SuperBowl> values = File.ReadAllLines(adjustedFilePath)
                                      .Skip(1)
                                      .Select(v => SuperBowl.FromCsv(v))
                                      .ToList();
            //Enable writing to the text file
            StreamWriter sw = new StreamWriter(filePath2);

            //Generate a list of all super bowl winners
            sw.WriteLine("                                                        SUPER BOWL WINNERS                                                              ");
            sw.WriteLine("----Winning Team------------Year-----Winning Quarterback--------------Winning Coach-------Super Bowl MVP------------------Point Spread--");
            var superBowlWinners =
                from superBowl in values
                where superBowl.Date != null
                select superBowl;

            foreach (SuperBowl superBowl in superBowlWinners)
            {
                string output = String.Format("    {0,-23} {1,-8} {2,-32} {3,-19} {4,-31} {5,-17}",
                                              superBowl.winningTeam, superBowl.Date.Year, superBowl.winningQB,
                                              superBowl.winningCoach, superBowl.MVP, superBowl.pointDifference);
                sw.WriteLine(output);
            }

            //Generate a list of the top five attended super bowl’s
            sw.WriteLine("");
            sw.WriteLine("                                      TOP FIVE ATTENDED SUPER BOWLS                                            ");
            sw.WriteLine("----Year-----Winning Team------------Losing Team-------------Host City--------Host State------Host Stadium-----");
            var topFiveAttended =
                (from superBowl in values
                 orderby superBowl.Attendance descending
                 select superBowl).Take(5);

            foreach (SuperBowl superBowl in topFiveAttended)
            {
                string output = String.Format("    {0,-8} {1,-23} {2,-23} {3,-16} {4,-15} {5,-17}",
                                              superBowl.Date.Year, superBowl.winningTeam, superBowl.losingTeam,
                                              superBowl.hostCity, superBowl.hostState, superBowl.hostStadium);
                sw.WriteLine(output);
            }

            //Output the state that hosted the  most super bowls
            sw.WriteLine("");
            sw.WriteLine("        THE STATE THAT HOSTED THE MOST SUPER BOWLS       ");
            sw.WriteLine("------Host City--------Host State-------Host Stadium-------");

            var stateMostHosted = values.GroupBy(superBowl => superBowl.hostState)
                                  .Select(group => new { hostState = group.Key, Count = group.Count() })
                                  .OrderByDescending(superBowl => superBowl.Count);
            var item = stateMostHosted.First();

            var mostFrequent      = item.hostState;
            var mostFrequentCount = item.Count;

            foreach (SuperBowl superBowl in values)
            {
                if (superBowl.hostState.Equals(mostFrequent))
                {
                    string output = String.Format("      {0,-16} {1,-16} {2,-17}",
                                                  superBowl.hostCity, superBowl.hostState, superBowl.hostStadium);
                    sw.WriteLine(output);
                }
            }

            //Generate a list of players who won MVP more than once
            sw.WriteLine("");
            sw.WriteLine("        PLAYERS WHO WON THE SUPER BOWL MVP TITLE MORE THAN ONCE          ");
            sw.WriteLine("------Super Bowl MVP--------Winning Team--------------Losing Team--------");

            var moreThanOnceMVP = from superBowl in values
                                  group superBowl by superBowl.MVP into MVPGroups
                                  orderby MVPGroups.Count() descending
                                  select MVPGroups;

            foreach (var groupMVP in moreThanOnceMVP)
            {
                if (groupMVP.Count() > 1)
                {
                    foreach (var superBowl in groupMVP)
                    {
                        string output = String.Format("      {0,-21} {1,-25} {2,-25}",
                                                      superBowl.MVP, superBowl.winningTeam, superBowl.losingTeam);
                        sw.WriteLine(output);
                    }
                }
            }

            //Which coach lost the most super bowls?
            sw.WriteLine("");
            sw.WriteLine("THE COACH THAT LOST THE MOST SUPER BOWLS");

            var coachLostMost = values.GroupBy(superBowl => superBowl.losingCoach)
                                .Select(group => new { losingCoach = group.Key, Count = group.Count() })
                                .OrderByDescending(superBowl => superBowl.Count);
            var clm                = coachLostMost.First();
            var mostLosses         = clm.losingCoach;
            var mostFrequentLosses = clm.Count;

            foreach (SuperBowl superBowl in values)
            {
                if (superBowl.losingCoach.Equals(mostLosses))
                {
                    sw.WriteLine("  " + superBowl.losingCoach + "  ");
                    break;
                }
            }

            //Which coach won the most super bowls?
            sw.WriteLine("");
            sw.WriteLine("THE COACH THAT WON THE MOST SUPER BOWLS");

            var coachWonMost = values.GroupBy(superBowl => superBowl.winningCoach)
                               .Select(group => new { winningCoach = group.Key, Count = group.Count() })
                               .OrderByDescending(superBowl => superBowl.Count);
            var cwm              = coachWonMost.First();
            var mostWins         = cwm.winningCoach;
            var mostFrequentWins = cwm.Count;

            foreach (SuperBowl superBowl in values)
            {
                if (superBowl.winningCoach.Equals(mostWins))
                {
                    sw.WriteLine("  " + superBowl.winningCoach + "  ");
                    break;
                }
            }

            //Which team(s) won the most super bowls?
            sw.WriteLine("");
            sw.WriteLine("THE TEAM(s) THAT WON THE MOST SUPER BOWLS");

            var teamWonMost = values.GroupBy(superBowl => superBowl.winningTeam)
                              .Select(group => new { winningTeam = group.Key, Count = group.Count() })
                              .OrderByDescending(superBowl => superBowl.Count);
            var twm               = teamWonMost.FirstOrDefault();
            var tMostWins         = twm.winningTeam;
            var tMostFrequentWins = twm.Count;

            foreach (SuperBowl superBowl in values)
            {
                if (superBowl.winningTeam.Equals(tMostWins))
                {
                    sw.WriteLine("  " + superBowl.winningTeam + "  ");
                    break;
                }
            }

            //Which team(s) lost the most super bowls?
            sw.WriteLine("");
            sw.WriteLine("THE TEAM(s) THAT LOST THE MOST SUPER BOWLS");

            var teamLostMost = values.GroupBy(superBowl => superBowl.losingTeam)
                               .Select(group => new { losingTeam = group.Key, Count = group.Count() })
                               .OrderByDescending(superBowl => superBowl.Count);
            var tlm                 = teamLostMost.FirstOrDefault();
            var tMostLosses         = tlm.losingTeam;
            var tMostFrequentLosses = tlm.Count;

            foreach (SuperBowl superBowl in values)
            {
                if (superBowl.losingTeam.Equals(tMostLosses))
                {
                    sw.WriteLine("  " + superBowl.losingTeam + "  ");
                    break;
                }
            }

            //Which Super bowl had the greatest point difference?
            sw.WriteLine("");
            sw.WriteLine("THE SUPER BOWL THAT HAD THE GREATEST POINT DIFFERENCE");

            var greatestPointDifference = values.GroupBy(superBowl => superBowl.pointDifference)
                                          .Select(group => new { pointDifference = group.Key, Count = group.Count() })
                                          .OrderBy(superBowl => superBowl.pointDifference);
            var gpd = greatestPointDifference.Last();
            var greatestDifference   = gpd.pointDifference;
            var pointDifferenceCount = gpd.Count;

            foreach (SuperBowl superBowl in values)
            {
                if (superBowl.pointDifference.Equals(greatestDifference))
                {
                    sw.WriteLine("Super Bowl " + superBowl.romanNumeral + " had the greatest point differnce of " + superBowl.pointDifference + " in " + superBowl.Date.Year + ".");
                }
            }

            //What is the average attendance of all super bowls?
            sw.WriteLine("");
            sw.WriteLine("AVERAGE ATTENDANCE OF ALL SUPER BOWLS 1967-2017");
            double AvgAttendance = values.Average(superBowl => superBowl.Attendance);

            sw.WriteLine("The average attendance at Super Bowls between 1967 to 2017 is " + AvgAttendance.ToString("n0") + " attendees.");



            sw.Close();
        }
        static void Main(string[] args)
        {
            string filePath;

            Console.WriteLine("Enter the file path for 'Super_Bowl_Project.csv': ");
            filePath = Console.ReadLine();

            if (File.Exists(filePath))
            {
                List <SuperBowl> superBowlList = new List <SuperBowl>();

                FileStream   CSVFile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                StreamReader reader  = new StreamReader(CSVFile);

                var parentDirectory = Directory.GetParent(filePath);

                Console.WriteLine("Enter desired name for new .txt file:");
                string fileName = Console.ReadLine();

                string newFilePath = parentDirectory + "/" + fileName + ".txt";


                FileStream   txtFile = new FileStream(newFilePath, FileMode.Append, FileAccess.Write);
                StreamWriter writer  = new StreamWriter(txtFile);

                string row = reader.ReadLine();

                while (!reader.EndOfStream)
                {
                    row = reader.ReadLine();
                    string[] aSuperBowl = row.Split(',');

                    SuperBowl theBigGame = new SuperBowl(aSuperBowl[0], aSuperBowl[1], Convert.ToInt32(aSuperBowl[2]), aSuperBowl[3], aSuperBowl[4], aSuperBowl[5], Convert.ToInt32(aSuperBowl[6]),
                                                         aSuperBowl[7], aSuperBowl[8], aSuperBowl[9], Convert.ToInt32(aSuperBowl[10]), aSuperBowl[11], aSuperBowl[12], aSuperBowl[13], aSuperBowl[14]);
                    superBowlList.Add(theBigGame);
                }

                //SB WINNER OUTPUT

                writer.WriteLine("Super Bowl Winners:");
                string sbNumber  = "SB";
                string sbWinner  = "Winner";
                string sbLoser   = "Loser";
                string sbYear    = "Year";
                string sbQB      = "QB";
                string sbCoach   = "Coach";
                string sbMVP     = "MVP";
                string sbPtDiff  = "Pt Diff";
                string sbCity    = "City";
                string sbState   = "State";
                string sbStadium = "Stadium";

                writer.WriteLine("{0,-8}{1,-21}{2,-5}{3,-27}{4,-16}{5,-26}{6,-7}", sbNumber, sbWinner, sbYear, sbQB, sbCoach, sbMVP, sbPtDiff);
                writer.WriteLine(new string('-', 110));
                foreach (SuperBowl winner in superBowlList)
                {
                    double ptDifference = winner.winnerPts - winner.loserPts;
                    writer.WriteLine($"{winner.sb,-8}{winner.winner,-20} '{winner.date.Substring(winner.date.Length - 2),-3} {winner.winnerQB,-26} {winner.winnerCoach,-15} {winner.MVP,-25} {ptDifference,-7}");
                }


                //TOP 5 ATTENDED SBs


                var top5AttendedSBsQ = (from superBowl in superBowlList
                                        orderby superBowl.attendance descending
                                        select superBowl).Take(5);

                writer.WriteLine("\nTop 5 attended super bowls:");
                writer.WriteLine("{0,-5}{1,-20}{2,-20}{3,-10}{4,-11}{5,-16}", sbYear, sbWinner, sbLoser, sbCity, sbState, sbStadium);
                writer.WriteLine(new string('-', 81));

                foreach (SuperBowl superBowl in top5AttendedSBsQ)
                {
                    writer.WriteLine("'{0,-4}{1,-20}{2,-20}{3,-10}{4,-11}{5,-16}", superBowl.date.Substring(superBowl.date.Length - 2), superBowl.winner, superBowl.loser, superBowl.city, superBowl.state, superBowl.stadium);
                }

                // STATE/CITY/STADIUM THAT HOSTED THE MOST SUPER BOWLS

                var mostHostState = superBowlList
                                    .GroupBy(state => state.state)
                                    .OrderByDescending(group => group.Count())
                                    .First().Key;

                var mostHostCity = superBowlList
                                   .GroupBy(city => city.city)
                                   .OrderByDescending(group => group.Count())
                                   .First().Key;

                var mostHostStadium = superBowlList
                                      .GroupBy(stadium => stadium.stadium)
                                      .OrderByDescending(group => group.Count())
                                      .First().Key;

                writer.WriteLine("\nState that hosted the most super bowls:\n\t{0}\n" +
                                 "City that hosted the most super bowls:\n\t{1}\n" +
                                 "Stadium that hosted the most super bowls:\n\t{2}",
                                 mostHostState, mostHostCity, mostHostStadium);

                // PLAYERS WHO WON MVP MORE THAN ONCE

                writer.WriteLine("\nPlayers who've won MVP multiple times:");
                var mvpGroup = from superBowl in superBowlList
                               group superBowl by superBowl.MVP into mvps
                               where mvps.Count() > 1
                               orderby mvps.Key
                               select mvps;

                /*
                 * var multiMVP = from mvp in mvpGroup
                 *             where mvpGroup.Count() > 1
                 *             select mvp;
                 */
                foreach (var player in mvpGroup)
                {
                    writer.WriteLine(player.Key);
                    foreach (var mvp in player)
                    {
                        writer.WriteLine("\tWon with {0} and beat {1}", mvp.winner, mvp.loser);
                    }
                }

                //1    //WHICH COACH LOST THE MOST AMOUNT OF SUPER BOWLSSS?

                var mostLossesCoach = superBowlList
                                      .GroupBy(coach => coach.loserCoach)
                                      .OrderByDescending(coach => coach.Count())
                                      .First().Key;

                writer.WriteLine("\nWhich coach lost the most super bowls?\n\t{0}", mostLossesCoach);

                //2    //WHICH COACH WON THE MOST AMOUNT OF SUPER BOWLSSS?

                var mostWinsCoach = superBowlList
                                    .GroupBy(coach => coach.winnerCoach)
                                    .OrderByDescending(coach => coach.Count())
                                    .First().Key;

                writer.WriteLine("\nWhich coach won the most super bowls?\n\t{0}", mostWinsCoach);

                //3    //WHICH TEAMS WON THE MOST SUPERBOWLS

                var mostWinsTeam = superBowlList
                                   .GroupBy(team => team.winner)
                                   .OrderByDescending(team => team.Count())
                                   .First().Key;

                writer.WriteLine("\nWhich team has won the most super bowls?\n\t{0}", mostWinsTeam);

                //4    //WHICH TEAM LOST THE MOST SUPER BOWLS

                var mostLossesTeam = superBowlList
                                     .GroupBy(team => team.loser)
                                     .OrderByDescending(team => team.Count())
                                     .First().Key;

                writer.WriteLine("\nWhich team has lost the most super bowls?\n\t{0}", mostLossesTeam);

                //5    //WHICH SUPER BOWL HAD THE GREATEST POINT DIFFERENCE

                var ptDiffQ = from superBowl in superBowlList
                              orderby(superBowl.winnerPts - superBowl.loserPts) descending
                              select superBowl;

                writer.WriteLine("\nWhich super bowl had the greatest point difference?\n\tSuper bowl {0}", ptDiffQ.First().sb);

                //6    //WHAT IS THE AVE ATTENDANCE OF ALL THE SBs

                int totalAttendance = 0;
                foreach (SuperBowl superBowl in superBowlList)
                {
                    totalAttendance = totalAttendance + superBowl.attendance;
                }
                int avgAttendance = totalAttendance / superBowlList.Count();

                writer.WriteLine("\nWhat is the average attendance of all the super bowls?\n\t{0} people", avgAttendance);

                CSVFile.Close();
                reader.Close();
                txtFile.Close();
            }
            else
            {
                Console.WriteLine("I can't seem to find that file :(\nPlease locate the file and restart the program !");
            }
            Console.WriteLine("Check directory of 'Super_Bowl_Project.csv' for the new file !\n\nPress 'Enter' to quit");
            Console.ReadLine();
        }
Example #7
0
        static void Main(string[] args)
        {
            //Reading from a file
            //Declaring variables
            FileStream   input;
            StreamReader read;
            string       primingValue;

            string[] sbData;
            string   csvPATH = "";
            string   txtPATH = "";

            //Program Introduction
            Console.WriteLine("Welcome to the Superbowl CSV file reader.");
            Console.WriteLine("This Program will read the data from the CSV file and write it to a TXT file.\n");

            //User defines the PATH to the csv
            Console.WriteLine("Where do you want to read the csv file from?\n");
            Console.WriteLine("for example:\nC:\\Users\\Jake\\Documents\\College\\SENG Semester 2\\Advanced Programming\\Projects\\Project 2\\Project_Two\\Super_Bowl_Project.csv\n");
            Console.WriteLine("Input the PATH to the CSV File:");
            csvPATH = @Console.ReadLine();
            Console.Clear();

            //User defines the PATH to the txt
            Console.WriteLine("Where do you want to read the csv file from?\n");
            Console.WriteLine("for example:\nC:\\Users\\Jake\\Documents\\College\\SENG Semester 2\\Advanced Programming\\Projects\\Project 2\\Project_Two\\Super_Bowl_Project.txt\n");
            Console.WriteLine("Input the PATH to the TXT File:");
            txtPATH = @Console.ReadLine();
            Console.Clear();

            try
            {
                //declaring a file stream to write to the text file with the superbowl data
                FileStream   newfile = new FileStream(txtPATH, FileMode.Create, FileAccess.Write);
                StreamWriter outfile = new StreamWriter(newfile);

                input        = new FileStream(csvPATH, FileMode.Open, FileAccess.Read);
                read         = new StreamReader(input);
                primingValue = read.ReadLine();
                List <SuperBowl> sbDataList = new List <SuperBowl>(); //declaring superbowl data list

                //Establishing a looping structure to read in all the superbowl data
                while (!read.EndOfStream)
                {
                    sbData = read.ReadLine().Split(',');  //reads in each element between each comma and splits the comma off
                    SuperBowl sb = new SuperBowl(sbData); //each element of the list will be called sb
                    sbDataList.Add(sb);                   //adds a superbowl to the list
                    //Console.WriteLine(sbDataList[sbDataList.Count - 1]); //used to insure that the data is populating the list
                }

                read.Dispose();
                input.Dispose();

                //Below outputs the list of the winning teams
                Console.WriteLine("            List of Winning Teams");
                outfile.WriteLine("            List of Winning Teams");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                //invoking the print winner function in a foreach
                foreach (SuperBowl sb in sbDataList)
                {
                    Console.WriteLine(sb.printWinner());
                }

                foreach (SuperBowl sb in sbDataList)
                {
                    outfile.WriteLine(sb.printWinner());
                }

                //Below outputs the list of the top attended superbowl counts
                Console.WriteLine("          Top 5 Attended Superbowls");
                outfile.WriteLine("          Top 5 Attended Superbowls");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                var attendanceQuery = (from sb in sbDataList orderby sb.Attendance descending select sb).ToList <SuperBowl>().Take(5); //selects 5 superbowls by top attendance from the superbowl data list and pust them into an attendance query list

                //For each element in the attendance query write the date, winning team, losing team, city, state, and stadium
                //This code was influenced off of LeAnn Simonson and Sarah Fox
                attendanceQuery.ToList <SuperBowl>().ForEach(x => Console.WriteLine($"1. The date the team won: {x.Date}\n2. The winning team: {x.winningTeam}\n3. The losing team: {x.losingTeam}\n" +
                                                                                    $"4. The city: {x.City}\n5. The state: {x.State}\n6. The stadium: {x.Stadium}\n7. The attendance: {x.Attendance}\n"));

                attendanceQuery.ToList <SuperBowl>().ForEach(x => outfile.WriteLine($"1. The date the team won: {x.Date}\n2. The winning team: {x.winningTeam}\n3. The losing team: {x.losingTeam}\n" +
                                                                                    $"4. The city: {x.City}\n5. The state: {x.State}\n6. The stadium: {x.Stadium}\n7. The attedance: {x.Attendance}\n"));

                //Below outputs the state that hosted the most superbowls
                //This section and the MVP section contain adapted code originally from LeAnn Simonson
                Console.WriteLine("    State That Hosted The Most Superbowls");
                outfile.WriteLine("    State That Hosted The Most Superbowls");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                var hostQueryCount = (from sb in sbDataList                                                            //defining a query count variable that is used to make a descending list
                                      group sb by sb.State into nestedQuery                                            //creates a group of state data and puts it into a nested query
                                      orderby nestedQuery.Count() descending                                           //orders the list
                                      select nestedQuery).First().Count();                                             //takes the count of the first element in nestedQuery (the state that hosted the most superbowls)

                var hostCount = (from sb in sbDataList                                                                 //defining a host count variable that considers each superbowl(sb) in the superbowl data list
                                 group sb by sb.State into hostGroup                                                   //creates a group of state data and puts it into host group
                                 where hostGroup.Count() == hostQueryCount                                             //adds a condition to the list where the only element it contains can be equal to the most hosted count
                                 select hostGroup).Take(1);                                                            //takes the one element in the list

                foreach (var sb in hostCount)                                                                          //iterates through the list (even though it's just one element)
                {
                    Console.WriteLine($"{sb.Key} hosted the most superbowls. They hosted {sb.Count()} superbowls.\n"); //writes the state and count to the terminal
                    outfile.WriteLine($"{sb.Key} hosted the most superbowls. They hosted {sb.Count()} superbowls.\n"); //writes the state and count to the file
                    foreach (var info in sb)                                                                           //iterates through the info attached to the sb element and outputs the city, state, stadium, and date
                    {
                        Console.WriteLine($"1. City: {info.City}");                                                    //using methods to pull data from a superbowl
                        outfile.WriteLine($"1. City: {info.City}");
                        Console.WriteLine($"2. State: {info.State}");
                        outfile.WriteLine($"2. State: {info.State}");
                        Console.WriteLine($"3. Stadium: {info.Stadium}");
                        outfile.WriteLine($"3. Stadium: {info.Stadium}");
                        Console.WriteLine($"4. Date: {info.Date}\n");
                        outfile.WriteLine($"4. Date: {info.Date}\n");
                    }
                }

                //Below outputs players that won MVP for than once as well as their team and the team they beat
                Console.WriteLine("List of Players That Won Mvp More Than 1 Time");
                outfile.WriteLine("List of Players That Won Mvp More Than 1 Time");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                var MVPCount = from sb in sbDataList            //defining an MVPCount variable that considers each superbowl(sb) in the superbowl data list
                               group sb by sb.MVP into MVPGroup //creates a group of MVP data to be counted
                               where MVPGroup.Count() > 1       //only adds the MVP to the group if the player was MVP more than once
                               orderby MVPGroup.Key             //orders the list
                               select MVPGroup;                 //returns the ordered list of MVPs in a list format

                foreach (var sb in MVPCount)
                {
                    Console.WriteLine($"{sb.Key} won MVP {sb.Count()} times\n");         //writes the MVP and amount of times they were MVP
                    outfile.WriteLine($"{sb.Key} won MVP {sb.Count()} times\n");
                    foreach (var info in sb)                                             //iterates through the info attached to the sb element(MVP) and outputs the winning team, team they beat, and the date the superbowl took place.
                    {
                        Console.WriteLine($"1. Their winning team: {info.winningTeam}"); //using methods to pull data from a superbowl
                        outfile.WriteLine($"1. Their winning team: {info.winningTeam}");
                        Console.WriteLine($"2. The team they beat: {info.losingTeam}");
                        outfile.WriteLine($"2. The team they beat: {info.losingTeam}");
                        Console.WriteLine($"3. Superbowl took place: {info.Date}\n");
                        outfile.WriteLine($"3. Superbowl took place: {info.Date}\n");
                    }
                    Console.WriteLine("\n");
                    outfile.WriteLine("\n");
                }

                //Below outputs the coach that lost the most superbowls
                Console.WriteLine("     Coach That Lost The Most Superbowls  ");
                outfile.WriteLine("     Coach That Lost The Most Superbowls  ");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                //Defining a query count variable that is used to make a descending list to determine highest loss count.
                //It creates a group of losing coach data and puts it into a nested query, orders the list, and takes the
                //count of the first element in nestedQuery (the coach that lost the most superbowls).
                var highestLossCount = (from sb in sbDataList
                                        group sb by sb.losingCoach
                                        into nestedQuery
                                        orderby nestedQuery.Count() descending
                                        select nestedQuery).First().Count();

                //defining a losing coach query that is designed to take the coach that lost the most by using a condition that
                //was found in the nested query. From there, the coaches that lost the most are selected and added to an array
                var losingCoachQuery = (from sb in sbDataList
                                        group sb by sb.losingCoach
                                        into losingCoachGroup
                                        where losingCoachGroup.Count() == highestLossCount
                                        select losingCoachGroup.Key).ToArray();

                for (var x = 0; x < losingCoachQuery.Length; x++)
                {
                    Console.WriteLine($"- {losingCoachQuery[x]} lost the most superbowls\n");
                    outfile.WriteLine($"- {losingCoachQuery[x]} lost the most superbowls\n");
                }
                Console.WriteLine("\n");
                outfile.WriteLine("\n");

                //Below outputs the coach that won the most superbowls
                //////
                Console.WriteLine("     Coach That Won The Most Superbowls  ");
                outfile.WriteLine("     Coach That Won The Most Superbowls  ");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                //The same process as the most losses coach section is used for this query manipulation.
                var highestWinCount = (from sb in sbDataList
                                       group sb by sb.winningCoach
                                       into nestedQuery
                                       orderby nestedQuery.Count() descending
                                       select nestedQuery).First().Count();

                var winningCoachQuery = (from sb in sbDataList
                                         group sb by sb.winningCoach
                                         into winningCoachGroup
                                         where winningCoachGroup.Count() == highestWinCount
                                         select winningCoachGroup.Key).ToArray();

                for (var x = 0; x < winningCoachQuery.Length; x++)
                {
                    Console.WriteLine($"- {winningCoachQuery[x]} won the most superbowls\n");
                    outfile.WriteLine($"- {winningCoachQuery[x]} won the most superbowls\n");
                }
                Console.WriteLine("\n");
                outfile.WriteLine("\n");

                //Below outputs the team that won the most superbowls
                //////
                Console.WriteLine("      Team That Won The Most Superbowls");
                outfile.WriteLine("      Team That Won The Most Superbowls");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                //The same process as the most losses coach section is used for this query manipulation.
                var teamHighestWinCount = (from sb in sbDataList
                                           group sb by sb.winningTeam
                                           into nestedQuery
                                           orderby nestedQuery.Count() descending
                                           select nestedQuery).First().Count();

                var winningTeamQuery = (from sb in sbDataList
                                        group sb by sb.winningTeam
                                        into winningTeamGroup
                                        where winningTeamGroup.Count() == teamHighestWinCount
                                        select winningTeamGroup.Key).ToArray();

                for (var x = 0; x < winningTeamQuery.Length; x++)
                {
                    Console.WriteLine($"- {winningTeamQuery[x]} won the most superbowls\n");
                    outfile.WriteLine($"- {winningTeamQuery[x]} won the most superbowls\n");
                }
                Console.WriteLine("\n");
                outfile.WriteLine("\n");

                //Below outputs the team that lost the most superbowls
                //////
                Console.WriteLine("     Team That Lost The Most Superbowls");
                outfile.WriteLine("     Team That Lost The Most Superbowls");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                //The same process as the most losses coach section is used for this query manipulation.
                var teamHighestLossCount = (from sb in sbDataList
                                            group sb by sb.losingTeam
                                            into nestedQuery
                                            orderby nestedQuery.Count() descending
                                            select nestedQuery).First().Count();

                var losingTeamQuery = (from sb in sbDataList
                                       group sb by sb.losingTeam
                                       into losingTeamGroup
                                       where losingTeamGroup.Count() == teamHighestLossCount
                                       select losingTeamGroup.Key).ToArray();

                for (var x = 0; x < losingTeamQuery.Length; x++)
                {
                    Console.WriteLine($"- {losingTeamQuery[x]} lost the most superbowls\n");
                    outfile.WriteLine($"- {losingTeamQuery[x]} lost the most superbowls\n");
                }
                Console.WriteLine("\n");
                outfile.WriteLine("\n");

                //Below outputs the superbowl that had the greatest point difference
                Console.WriteLine("  Superbowl with Greatest Point Difference");
                outfile.WriteLine("  Superbowl with Greatest Point Difference");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                //declaring an empty list to place the point differences
                var pointDifferences = new List <int>();

                //for each superbowl element in the superbowl data list, add the point differences associated with the superbowl to the list
                foreach (var sb in sbDataList)
                {
                    pointDifferences.Add(sb.pointDifference());
                }

                //delcaring a greatest point difference variable and making it equal to the max point difference in the point differences list
                var greatestPointDifference = pointDifferences.Max();

                //for each superbowl element in the superbowl data list, write to the file and
                //console only if the point difference is equal to the greatest point difference
                foreach (var sb in sbDataList)
                {
                    if (sb.pointDifference() == greatestPointDifference)
                    {
                        Console.WriteLine($"- Superbowl {sb.SB} has the greatest point difference of {sb.pointDifference()}");
                        outfile.WriteLine($"- Superbowl {sb.SB} has the greatest point difference of {sb.pointDifference()}");
                    }
                }
                Console.WriteLine("\n");
                outfile.WriteLine("\n");

                //Below outputs the average attendance
                Console.WriteLine("     Average Attendance of All Superbowls");
                outfile.WriteLine("     Average Attendance of All Superbowls");
                Console.WriteLine("---------------------------------------------\n");
                outfile.WriteLine("---------------------------------------------\n");

                //declaring integers to invoke an attendance function
                int totalAttendance = 0;
                int avgAttendance;
                int count = 0;

                //adds the attendance onto the total attendance and increases the
                //count by one for each superbowl element in superbowl data list
                foreach (var sb in sbDataList)
                {
                    totalAttendance = totalAttendance + sb.Attendance;
                    count           = count + 1;
                }
                //simple average attendance function
                avgAttendance = totalAttendance / count;

                Console.WriteLine($"Average Attendance: {avgAttendance}");
                outfile.WriteLine($"Average Attendance: {avgAttendance}");

                Console.WriteLine("\n");
                outfile.WriteLine("\n");

                //End message and file closing
                outfile.Close(); //closes the outfile (this fixes data stream issues as well)
                Console.WriteLine("The data displayed above has been written to the TXT file");
            } //end of try

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Example #8
0
        static void Main(string[] args)
        {
            /**Your application should allow the end user to pass a file path for output
             * or guide them through generating the file.
             **/

            string filePath  = Directory.GetCurrentDirectory();
            string backOne   = Directory.GetParent(filePath).ToString();
            string backTwo   = Directory.GetParent(backOne).ToString();
            string backThree = Directory.GetParent(backTwo).ToString();
            string SBData    = @"E:\P2\Project_Two\Super_Bowl_Project.csv";

            Console.WriteLine(backThree);

            if (Directory.Exists(backThree))
            {
                Console.WriteLine("It exists.");
                if (File.Exists(SBData))
                {
                    Console.WriteLine("It exists.");
                    List <SuperBowl> sbList = new List <SuperBowl>();

                    FileStream   csvFile = new FileStream(SBData, FileMode.Open, FileAccess.Read);
                    StreamReader reader  = new StreamReader(csvFile);

                    var newDirectory = Directory.GetParent(backThree);
                    Console.WriteLine("This will print out a .txt file containing data on Super Bowls.");
                    Console.WriteLine("Please enter a name for the new .txt file.");
                    string newTxtFile = Console.ReadLine();

                    string newFilePath = newDirectory + "/" + newTxtFile + ".txt";

                    FileStream   txtFile = new FileStream(newFilePath, FileMode.Append, FileAccess.Write);
                    StreamWriter writer  = new StreamWriter(txtFile);

                    string row = reader.ReadLine();

                    while (!reader.EndOfStream)
                    {
                        row = reader.ReadLine();
                        string[]  SBDataTwo = row.Split(",");
                        SuperBowl SBDTwo    = new SuperBowl()
                        {
                            Date        = SBDataTwo[0],
                            SB          = SBDataTwo[1],
                            Attendance  = Convert.ToInt32(SBDataTwo[2]),
                            QBWinner    = SBDataTwo[3],
                            CoachWinner = SBDataTwo[4],
                            Winner      = SBDataTwo[5],
                            WinnerPts   = Convert.ToInt32(SBDataTwo[6]),
                            QBLoser     = SBDataTwo[7],
                            CoachLoser  = SBDataTwo[8],
                            Loser       = SBDataTwo[9],
                            LoserPts    = Convert.ToInt32(SBDataTwo[10]),
                            Mvp         = SBDataTwo[11],
                            Stadium     = SBDataTwo[12],
                            City        = SBDataTwo[13],
                            State       = SBDataTwo[14]
                        };
                        sbList.Add(SBDTwo);
                    }

                    writer.WriteLine("Super Bowl Winners:");
                    writer.WriteLine("/n");
                    string sbNum         = "SB";
                    string sbWinner      = "Winner";
                    string sbLoser       = "Loser";
                    string sbYear        = "Year";
                    string sbQuarterBack = "Quarter Back";
                    string sbCoachOne    = "Coach";
                    string sbMVP         = "Most Valuable Player";
                    string sbPointDif    = "Point Difference";
                    string sbCity        = "City";
                    string sbState       = "State";
                    string sbStadium     = "Stadium";

                    writer.WriteLine("{0,-8}{1,-21}{2,-5}{3,-27}{4,-16}{5,-26}{6,-7}", sbNum, sbWinner, sbYear, sbQuarterBack, sbCoachOne, sbMVP, sbPointDif);
                    writer.WriteLine(new string('-', 110));
                    foreach (SuperBowl winner in sbList)
                    {
                        double ptDifference = winner.WinnerPts - winner.LoserPts;
                        writer.WriteLine($"{winner.SB,-8}{winner.Winner,-20} '{winner.Date.Substring(winner.Date.Length - 2),-3} {winner.QBWinner,-26} {winner.CoachWinner,-15} {winner.Mvp,-25} {ptDifference,-7}");
                    }
                    var topAttendance = (from superBowl in sbList
                                         orderby superBowl.Attendance descending
                                         select superBowl).Take(5);

                    writer.WriteLine("");
                    writer.WriteLine("Top 5 attended super bowls:");
                    writer.WriteLine("{0,-5}{1,-20}{2,-20}{3,-10}{4,-11}{5,-16}", sbYear, sbWinner, sbLoser, sbCity, sbState, sbStadium);
                    writer.WriteLine(new string('-', 81));

                    foreach (SuperBowl superBowl in topAttendance)
                    {
                        writer.WriteLine("'{0,-4}{1,-20}{2,-20}{3,-10}{4,-11}{5,-16}", superBowl.Date.Substring(superBowl.Date.Length - 2), superBowl.Winner, superBowl.Loser, superBowl.City, superBowl.State, superBowl.Stadium);
                    }

                    var HostingState = sbList
                                       .GroupBy(state => state.State)
                                       .OrderByDescending(group => group.Count())
                                       .First().Key;

                    var HostingCity = sbList
                                      .GroupBy(city => city.City)
                                      .OrderByDescending(group => group.Count())
                                      .First().Key;

                    var HostingStadium = sbList
                                         .GroupBy(stadium => stadium.Stadium)
                                         .OrderByDescending(group => group.Count())
                                         .First().Key;

                    writer.WriteLine("");
                    writer.WriteLine("The state that hosted the most super bowl games:\n\t{0}\n" +
                                     "The city that hosted the most super bowl games:\n\t{1}\n" +
                                     "The stadium that hosted the most super bowl games:\n\t{2}",
                                     HostingState, HostingCity, HostingStadium);


                    writer.WriteLine("");
                    writer.WriteLine("These are the players who have won MVP multiple times: ");
                    var mvpGroup = from superBowl in sbList
                                   group superBowl by superBowl.Mvp into mvps
                                   where mvps.Count() > 1
                                   orderby mvps.Key
                                   select mvps;

                    foreach (var player in mvpGroup)
                    {
                        writer.WriteLine(player.Key);
                        foreach (var mvp in player)
                        {
                            writer.WriteLine("\t {0} V.S. {1}", mvp.Winner, mvp.Loser);
                        }
                    }


                    var coachLosses = sbList
                                      .GroupBy(coach => coach.CoachLoser)
                                      .OrderByDescending(coach => coach.Count())
                                      .First().Key;

                    writer.WriteLine("");
                    writer.WriteLine("Which coach lost the most super bowls?\n\t{0}", coachLosses);


                    var coachWins = sbList
                                    .GroupBy(coach => coach.CoachWinner)
                                    .OrderByDescending(coach => coach.Count())
                                    .First().Key;

                    writer.WriteLine("");
                    writer.WriteLine("Which coach won the most super bowls?\n\t{0}", coachWins);


                    var teamWins = sbList
                                   .GroupBy(team => team.Winner)
                                   .OrderByDescending(team => team.Count())
                                   .First().Key;

                    writer.WriteLine("");
                    writer.WriteLine("Which team has won the most super bowls?\n\t{0}", teamWins);


                    var teamLosses = sbList
                                     .GroupBy(team => team.Loser)
                                     .OrderByDescending(team => team.Count())
                                     .First().Key;

                    writer.WriteLine("");
                    writer.WriteLine("Which team lost the most super bowls?\n\t{0}", teamLosses);


                    var ptDiffQ = from superBowl in sbList
                                  orderby(superBowl.WinnerPts - superBowl.LoserPts) descending
                                  select superBowl;

                    writer.WriteLine("");
                    writer.WriteLine("Which super bowl had the greatest point difference?\n\tSuper bowl {0}", ptDiffQ.First().SB);

                    int maxAttendance = 0;
                    foreach (SuperBowl superBowl in sbList)
                    {
                        maxAttendance = maxAttendance + superBowl.Attendance;
                    }
                    int averageAttendance = maxAttendance / sbList.Count();

                    writer.WriteLine("");
                    writer.WriteLine("What is the average attendance of all the super bowls? \n\t{0} people?", averageAttendance);

                    csvFile.Close();
                    reader.Close();
                    txtFile.Close();
                }
            }
            else
            {
                Console.WriteLine("Something went wrong with the program...");
                Console.WriteLine("Please contact tech support at 426-5678 (IAM-LOST).");
                Console.WriteLine("This program will now close. Have a nice day.");
            }
        }