Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a class Player and I want to use a method that creates a list of Players. I wrote some code but something is wrong and i don't know what. The only record if the list that I am able to print out at the Console is the last one. What am I doing wrong?

Below is the code that i wrote...

File Player.cs

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise10dot6
{
    class Player
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private double balance;
        private bool isActive;
        private string password;

        //public string Password
        //{
        //    get { return password; }
        //    set { password = value; }
        //}

        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; }
        }


        public double Balance
        {
            get { return balance; }
            set { balance = value; }
        }

        public Player()
        {
            // A constructor to initialize a new player given a name, balance and password. The player should be initialized as inactive.
            this.name = "Iraklis Markelis";
            this.balance = 0.9;
            this.password = "*******";
            this.isActive = false;
        }

        public List<Player> Players()
        {
            bool finish = false;
            List<Player> ps = new List<Player>();

            while (!finish)
            {
                Console.Write("Type player's name: ");
                this.name = Console.ReadLine();
                Console.Write("Type player's balance: ");
                this.balance = double.Parse(Console.ReadLine());
                Console.Write("Give a password: ");
                this.password = Console.ReadLine();
                this.isActive = false;
                ps.Add(this);
                Console.Write("Do you want to continue? (0/1): ");
                finish = Convert.ToBoolean(Convert.ToInt16(Console.ReadLine()));
            }
            return ps;
        }

        public string PlayerInfo()
        {
            // A method to return a string that shows the public fields of a player separated by tabs.
            return "Name: " + this.name + "\tBalance: " + this.balance.ToString() + "\tIs active: " + this.isActive.ToString();
        }

        public bool PasswordAccepted(string password)
        {
            // A method to check a string for being a valid password, returning a bool.
            if (this.password == password)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void ShowPlayers(List<Player> players)
        {
            foreach (Player player in players)
            {
                Console.WriteLine(player.PlayerInfo());
            }
        }

        public Player FindPlayer(Player[] players, string nameToSearch)
        {
            foreach (Player player in players)
            {
                if (nameToSearch == player.name)
                {
                    return player;
                }
            }
            return null;
        }

        public string Login(string name, string password, Player[] players, out Player p)
        {
            p = FindPlayer(players, name);
            if (p == null)
            {
                return "Player not found.";
            }
            else
            {
                if (p.isActive == true)
                {
                    return "Player is active.";
                }
                else if (p.password == password)
                {
                    p.isActive = true;
                    return "OK";
                }
                else
                {
                    return "Wrong password.";
                }
            }
        }

        public string Logout(Player[] players, string name)
        {
            foreach (Player player in players)
            {
                if (player.name == name)
                {
                    if(player.isActive == true)
                    {
                        return "OK";
                    }
                    else
                    {
                        return "Logout failure. Player not logged in";
                    }
                }
                else
                {
                    return "Logout failure. Player not found.";
                }
            }

            return null;
        }
    }
}


File Program.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise10dot6
{
    class Program
    {
        static void Main(string[] args)
        {
            Player p = new Player();
            List<Player> myPlayers = new List<Player>();
            myPlayers = p.Players();
            p.ShowPlayers(myPlayers);
            Console.ReadLine();
        }
    }
}


Thank you very much for your help...
Posted
Updated 26-Jul-15 2:58am
v2

1 solution

I'm going to be brutal here, but hopefully not upset you too much. Then I'll try to explain some of what you should have done! :laugh:

That's really bad. It misses the point of object orientation completely, and not only breaks every rule, it also shows that you don't understand what an instance is, or what you should do with it.

So, let's start again.

Your Player class should not know how to talk to the user: that's the job of the code that creates it. So move the Console code out of Player, and into Program. By all means create a method which reads input from the user - but that shouldn't be in Player, because Player only knows about players, not users.

And Player shouldn't know about other Players either - in the same way that four people playing a computer war game on the internet don't have to know each other outside the game, your Player class should be concerned with itself, not others - so trying to work with the list of players from within the Player class is a bad idea. I'd suggest you create a PlayerList class which looks through all the players it knows about instead (that way, you can later have two teams - each of which know about their own players, but not the opposition)

And somewhere, (not inside Player, but possibly inside PlayerList) you need to create a new Player instance for each Player you want to work with.

Go back, read your course notes again, and see if you can fit any of this into what they say - because the code you have is never going to work!
I'd start small: create a player and fill in his info. Then when that works, create a list of Players, and add each one. Then ... you get the idea - work in small steps instead of trying to throw it all together and then hope it works! :laugh:
Any specific questions, feel free to ask - but read your course notes first!
 
Share this answer
 
Comments
markelis 24-Jul-15 7:52am    
If I understood right i'll just have to create a new class PlayerList and put the code of Players in there...

Thank you very much. I definitely missed it there. Thanks for your help...

P.S. Don't worry. I'm just trying to learn. Thanks anyway for being kind. Greetings from Greece.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900