I have this program,all i am stuck with is how to check if my file League.bin exits and if it is exists need to de-serialized into ArrayList and if it does exist user should initialize 4 teams,which i tried in FootballMethod(),plus i tried to do in the main top check for a existing file.And after deserialize in to League.bin.The program is complaining for foreach() in main.Something went wrong with my logic.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace PremierLeague
{
    [Serializable]
    class FootballTeam
    {
        private string name;
        private int gamesPlayed;
        private int goalsFor;
        private int goalsAgainst;
        private int points;
        public FootballTeam(string name)
        {
            this.name = name;
        }
        public FootballTeam(string name, int gamesPlayed, int goalsFor, int goalsAgainst, int points)
        {
            this.name = name;
            this.gamesPlayed = gamesPlayed;
            this.goalsFor = goalsFor;
            this.goalsAgainst = goalsAgainst;
            this.points = points;
        }

        public FootballTeam()
        {

            Console.WriteLine("Enter 4 teams names:");
            string names = Console.ReadLine();
            ArrayList teamNames = new ArrayList();
            teamNames.Add(new FootballTeam(names));
        }
        public void Display()
        {
            Console.WriteLine("{0,20} {1,10} {2,10} {3,10} {4,10}",
                name, gamesPlayed, goalsFor, goalsAgainst, points);
        }
        public void DeductPoints(int numPoints)
        {
            this.points -= numPoints;
        }
        public void UpdateOnResult(int goalsFor, int goalsAgainst)
        {
            gamesPlayed++;
            this.goalsFor += goalsFor;
            this.goalsAgainst += goalsAgainst;
            if (goalsFor > goalsAgainst)
                points += 3;
            else if (goalsFor == goalsAgainst)
                points++;
        }
        public string Name
        {
            get
            {
                return name;
            }
        }
        public int GamesPlayed
        {
            get
            {
                return gamesPlayed;
            }
        }
        public int GoalsFor
        {
            get
            {
                return goalsFor;
            }
        }
        public int GoalsAgainst
        {
            get
            {
                return goalsAgainst;
            }
        }
        public int Points
        {
            get
            {
                return points;
            }
        }
    }
}

Here is the main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace PremierLeague
{
    class Program
    {
        static ArrayList table = new ArrayList();
        private static BinaryFormatter bformatter;
        static void Main(string[] args)
        {
            int option;
            do
            {
                option = ShowMenu();
                switch (option)
                {
                    case 1:
                        DisplayTable();
                        break;
                    case 2:
                        EnterResult();
                        break;
                    case 3:
                        DeductPoints();
                        break;
                    case 4:
                        break;
                }
            } while (option != 4);
        }
        //ShowMenu method
        static int ShowMenu()
        {
            if (File.Exists("League.bin"))
            {
                Console.WriteLine("file exists");
                bformatter = new BinaryFormatter();
                FileStream footballFile = new FileStream("League.bin", FileMode.Open, FileAccess.Read);
                ArrayList leagueInfo = bformatter.Deserialize(footballFile) as ArrayList;
                Console.WriteLine("Retrieved List Contents");
                foreach (FootballTeam li in footballFile)
                    Console.WriteLine("{0}  {1} {2} {3} {4}", li.Name, li.GamesPlayed,li.GoalsFor,li.GoalsAgainst,li.Points);
                Console.WriteLine("\n\n");
                Console.ReadLine();

            }
            else
            {
                FootballTeam();

            }

            Console.WriteLine("\n\t1. Display table");
            Console.WriteLine("\t2. Enter result");
            Console.WriteLine("\t3. Deduct points");
            Console.WriteLine("\t4. Quit");
            Console.Write("\n Option ? ");
            int option = int.Parse(Console.ReadLine());
            return option;
        }

        private static void FootballTeam()
        {
            throw new NotImplementedException();
        }//end of ShowMenu method
        //DisplayTable method
        static void DisplayTable()
        {
            Console.WriteLine("\n");
            Console.WriteLine("{0,20} {1,15} {2,10} {3,10} {4,10}",
            "Team", "Games Played", "Goals For", "Against", "Points");
            Console.WriteLine("{0,20} {1,15} {2,10} {3,10} {4,10}",
            "----", "------------", "---------", "-------", "------");
            foreach (FootballTeam team in table)
                team.Display();
        }
        static void EnterResult()
        {
            Console.Write("\nEnter result - Home team : ");
            string homeTeam = Console.ReadLine();
            Console.Write("Enter result - Home score : ");
            int homeScore = int.Parse(Console.ReadLine());
            Console.Write("\nEnter result: Away team : ");
            string awayTeam = Console.ReadLine();
            Console.Write("Enter result: Away score : ");
            int awayScore = int.Parse(Console.ReadLine());
            foreach (FootballTeam team in table)
            {
                if (team.Name == homeTeam)
                {
                    team.UpdateOnResult(homeScore, awayScore);
                }
                if (team.Name == awayTeam)
                {
                    team.UpdateOnResult(awayScore, homeScore);
                }
            }
        }
        static void DeductPoints()
        {
            string name;
            int points;
            Console.Write("\nEnter team name: ");
            name = Console.ReadLine();
            Console.Write("Enter points to deduct: ");
            points = int.Parse(Console.ReadLine());
            foreach (FootballTeam team in table)
            {
                if (team.Name == name)
                {
                    team.DeductPoints(points);
                    break;
                }
            }
        }//end of DeductPoints method
    }
}

Edited 1 Year Ago by PulsarScript

In the foreach, you're trying to use the FileStream,footballFile, as a collection. I think you meant to use leagueInfo.

On a side note arraylist is not the best choice for this as it stores each object as an object not the type you want. I would suggest a List<FootballTeam> instead.

Edited 1 Year Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.