Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RMcGee
Created December 9, 2013 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RMcGee/7882450 to your computer and use it in GitHub Desktop.
Save RMcGee/7882450 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlayerCreator
{
class Program
{
static void Main(string[] args)
{
const Int32 MAXPLAYERS = 23;
Int32 playerCount = 0;
Player[] players = new Player[MAXPLAYERS];
char menuItem;
Int32 number, goals, assists;
string firstName, lastName;
Console.WriteLine("Welcome to the player system...\n");
menuItem = GetMenuItem();
while (menuItem != 'X')
{
ProcessMenuItem(menuItem, number, firstName, lastName, goals, assists, ref playerCount, MAXPLAYERS);
menuItem = GetMenuItem();
}
Console.WriteLine("\nThank you, goodbye");
Console.ReadLine();
}
static char GetMenuItem()
{
char menuItem;
DisplayMenu();
menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
while (menuItem != 'C'
&& menuItem != 'L' && menuItem != 'R' && menuItem != 'U' && menuItem != 'D' && menuItem != 'X')
{
Console.WriteLine("\nError - Invalid menu item\n");
DisplayMenu();
menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
}
return menuItem;
}
static void DisplayMenu()
{
Console.WriteLine("Please pick an item:");
Console.WriteLine("C - Create Player");
Console.WriteLine("R - Retrieve Player");
Console.WriteLine("U - Update Player");
Console.WriteLine("D - Delete Player");
Console.WriteLine("L - List Player(s)");
Console.WriteLine("X - Exit");
}
static void ProcessMenuItem(Char menuItem, Int32 number, String firstName, String lastName,
Int32 goals, Int32 assists, ref Int32 playerCount, Int32 MAXPLAYERS)
{
switch (menuItem)
{
case 'C':
ProcessCreate(number, firstName, lastName, goals, assists, ref playerCount, MAXPLAYERS);
break;
case 'L':
ProcessList(number, firstName, lastName, goals, assists, playerCount);
break;
/*case 'R':
ProcessRetrieve(number, firstName, lastName, goals, assists, playerCount);
break;*/
/*case 'U':
ProcessUpdate(number, firstName, lastName, goals, assists, playerCount);
break;*/
/*case 'D':
ProcessDelete(number, firstName, lastName, goals, assists, ref playerCount);
break;*/
}
}
static void ProcessCreate(Int32 number, String firstName, String lastName,
Int32 goals, Int32 assists, ref Int32 playerCount, Int32 MAXPLAYERS)
{
String playerFirstName, playerLastName;
Int32 playerNumber, playerGoals, playerAssists;
Player[] players = new Player[MAXPLAYERS];
if (playerCount < MAXPLAYERS)
{
Console.WriteLine("Create Player - please enter the player's number");
playerNumber = Int32.Parse(Console.ReadLine());
if(GetPlayerIndex(number, players, playerCount) == -1)
{
Console.WriteLine("Create Player - please enter the player's first name");
playerFirstName = Console.ReadLine();
Console.WriteLine("Create Player - please enter the player's last name");
playerLastName = Console.ReadLine();
Console.WriteLine("Create Player - please enter the player's goals");
playerGoals = Int32.Parse(Console.ReadLine());
Console.WriteLine("Create Player - please enter the player's assists");
playerAssists = Int32.Parse(Console.ReadLine());
InsertPlayer(number, firstName, playerLastName, goals, assists, players, ref playerCount);
Player player = new Player(playerNumber, playerFirstName, playerLastName, playerGoals, playerAssists);
Console.WriteLine("Number - " + player.Number + " First Name - " + player.FirstName + " Last Name - " + player.LastName + " Goals - " + player.Goals + "Assists - " + player.Assists);
Console.ReadLine();
}
else
Console.WriteLine("\nCreate Player: the player roster is already full");
}
else
Console.WriteLine("\nCreate Player: the player roster is already full");
}
static void ProcessList(Int32 number, String firstName, String lastName,
Int32 goals, Int32 assists, Int32 playerCount)
{
const Int32 MAXPLAYERS = 23;
Player[] players = new Player[MAXPLAYERS];
if (playerCount > 0)
{
for (Int32 player = 0; player < playerCount; player++)
{
Console.WriteLine(players[player].Number, players[player].FirstName, players[player].LastName,
players[player].Goals, players[player].Assists);
}
}
else
Console.WriteLine("\nList Players: the roster is empty\n");
}
static Int32 InsertPlayer(Int32 number, String firstName, String lastName, Int32 goals,
Int32 assists, Player[] players, ref Int32 playerCount)
{
Int32 insertIndex, shiftCount;
insertIndex = GetInsertIndex(number, players, playerCount);
for (shiftCount = playerCount; shiftCount > insertIndex; shiftCount--)
players[shiftCount] = players[shiftCount - 1];
players[insertIndex] = new Player(number, firstName, lastName, goals, assists);
playerCount++;
return insertIndex;
}
static Int32 GetInsertIndex(Int32 number, Player[] players,
Int32 playerCount)
{
Int32 index = 0;
bool found = false;
while (index < playerCount && found == false)
if (players[index].Number > number)
found = true;
else
index++;
return index;
}
static Int32 GetPlayerIndex(Int32 number,
Player[] players, Int32 playerCount)
{
Int32 index = 0;
bool found = false;
while (index < playerCount && found == false)
if (players[index].Number == number)
found = true;
else
index++;
if (found == false)
index = -1;
return index;
}
}
}
----------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlayerCreator
{
class Player
{
//Variables
private Int32 _number;
private Int32 _goals;
private Int32 _assists;
private String _firstName;
private String _lastName;
//Constructor
public Player(Int32 number, String firstName, String lastName, Int32 goals, Int32 assists)
{
Number = number;
Goals = goals;
Assists = assists;
FirstName = firstName;
LastName = lastName;
}
//Accessors
public Int32 Number
{
get
{
return _number;
}
set
{
/*if (value <= 0)
throw new Exception("Number must be greater than zero");
else*/
_number = value;
}
}
public Int32 Goals
{
get
{
return _goals;
}
set
{
/*if (value < 0)
throw new Exception("Number must be positive");
else*/
_goals = value;
}
}
public Int32 Assists
{
get
{
return _assists;
}
set
{
/*if (value <= 0)
throw new Exception("Number must be greater than zero");
else*/
_assists = value;
}
}
public String FirstName
{
get
{
return _firstName;
}
set
{
/*if (value == "")
throw new Exception("First Name cannot be empty");
else*/
_firstName = value;
}
}
public String LastName
{
get
{
return _lastName;
}
set
{
/*if (value == "")
throw new Exception("Last Name cannot be empty");
else*/
_lastName = value;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment