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; } } }
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(); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)