using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Windows.Forms;
namespace ABCGolf
{
public partial class Form1 : Form, ITeam
{
// Global Variables for all of Form1 to access
string playeraddTeam = "";
List myTeams = new List(10);
string TeamText = "";
Team myteam = new Team();
private const int NOTFOUND = 99999;
public Form1()
{
InitializeComponent();
} // End Form1 constructor
private void btnClearTeam_Click(object sender, EventArgs e)
{
txtTeamName.Clear();
txtTeamRank.Clear();
} // End btnClearTeam_Click method
private void btnAddUpdateTeam_Click(object sender, EventArgs e)
{
try
{
if (txtTeamRank.Text != "" && txtTeamName.Text != "")
{
int teamrnk = Convert.ToInt16(txtTeamRank.Text);
Team myteam = new Team(txtTeamName.Text, teamrnk);
TeamText = txtTeamName.Text;
int theTruth = GetTheTruth(myTeams, myteam.TeamName);
if (theTruth == 1)
{
int Location = TeamGetIndex(myTeams, txtTeamName.Text);
myTeams.RemoveAt(Location);
cbUpdateTeam.Items.Remove(txtTeamName.Text);
}
if (myTeams.Count < 10)
{
myTeams.Add(myteam);
cbUpdateTeam.Items.Add(txtTeamName.Text);
cbViewTeam.Items.Add(txtTeamName.Text);
cbPlayerTeams.Items.Add(txtTeamName.Text);
}
else
{
MessageBox.Show("You must remove a team before entering a another team.");
}
txtTeamName.Clear();
txtTeamRank.Clear();
}
else
{
MessageBox.Show("You must enter a Value for both fields");
}
}
catch (Exception v)
{
MessageBox.Show(v.Message);
}
} // End btnAddUpdateTeam_Click method
public int TeamGetIndex(List myTeams, string p)
{
// Local Method Variables
int Index = 0;
foreach (Team I in myTeams)
{
if (I.TeamName == p)
{
return Index;
}
Index++;
}
// Team not found
Index = NOTFOUND;
return Index;
} // End TeamGetIndex method
public int PlayerGetIndex(List myPlayers, string p)
{
int Index = 0;
foreach (Golfer I in myPlayers)
{
if (I.LastName == p)
{
return Index;
}
Index++;
}
Index = NOTFOUND;
return Index;
} // End PlayerGetIndex method
private int GetTheTruth(List myTeams, string p)
{
foreach (Team I in myTeams)
{
if (I.TeamName == p)
{
return 1;
}
}
return 0;
} // End GetTheTruth method
private void cbUpdateTeam_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string name = cbUpdateTeam.SelectedItem.ToString();
Team theTeam = (from x in myTeams where x.TeamName == name select x).FirstOrDefault();
txtTeamName.Text = theTeam.TeamName;
TeamText = txtTeamName.Text;
txtTeamRank.Text = theTeam.TeamRank.ToString();
}
catch (Exception a)
{
MessageBox.Show(a.Message);
}
} // End cbUpdateTeam_SelectedIndexChanged method
private void btnDeleteTeam_Click(object sender, EventArgs e)
{
try
{
int Location2 = TeamGetIndex(myTeams, TeamText);
myTeams.RemoveAt(Location2);
cbUpdateTeam.Items.Remove(TeamText);
cbViewTeam.Items.Remove(TeamText);
cbPlayerTeams.Items.Remove(TeamText);
txtTeamName.Clear();
txtTeamRank.Clear();
cbUpdateTeam.Text = "";
}
catch (Exception m)
{
MessageBox.Show(m.Message);
}
} // End btnDeleteTeam_Click_1 method
private void cbViewTeam_SelectedIndexChanged(object sender, EventArgs e)
{
// Local Method Variables
Team gridteam = new Team();
try
{
gridteam = (from x in myTeams where x.TeamName == cbViewTeam.Text select x).FirstOrDefault();
if (gridteam.Players.Count != 0)
{
int counter = 0;
if (dataGridView1.Rows.Count > 1)
{
// Remove in decending order as rows are reindexed with each remove
dataGridView1.Rows.RemoveAt(3);
dataGridView1.Rows.RemoveAt(2);
dataGridView1.Rows.RemoveAt(1);
dataGridView1.Rows.RemoveAt(0);
}
foreach (Golfer i in gridteam.Players)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[counter].Cells[0].Value = i.FirstName;
dataGridView1.Rows[counter].Cells[1].Value = i.LastName;
dataGridView1.Rows[counter].Cells[2].Value = i.Handicap;
dataGridView1.Rows[counter].Cells[3].Value = i.RankOnTeam;
counter++;
}
}
else
{
MessageBox.Show("There are no Players entered on the Team you selected");
}
}
catch (Exception o)
{
MessageBox.Show(o.Message);
}
} // End cbViewTeam_SelectedIndexChanged method
private void cbTeams_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
lbMembers.Items.Clear();
playeraddTeam = cbPlayerTeams.Text;
Team LTeam = (from x in myTeams where x.TeamName == playeraddTeam select x).FirstOrDefault();
if (myTeams.Count != 0)
{
if (LTeam.Players != null)
{
foreach (Golfer i in LTeam.Players)
{
lbMembers.Items.Add(i.FirstName + " " + i.LastName);
}
}
}
else
{
MessageBox.Show("You have to Add a Team before you can add an Golfer");
}
}
catch (Exception l)
{
MessageBox.Show(l.Message);
}
} // End cbTeams_SelectedIndexChanged method
private void btnAddUpdate_Click(object sender, EventArgs e)
{
// Local Method Variables
Team myteam = new Team();
Golfer i = new Golfer();
try
{
if (cbPlayerTeams.Text != "")
{
i.FirstName = txtFName.Text;
i.LastName = txtLName.Text;
i.RankOnTeam = Convert.ToInt32(txtRank.Text);
i.LastGame = Convert.ToInt32(txtLastScore.Text);
i.Handicap = Convert.ToInt32(txtHandiCap.Text);
// GetTeam is an existing team in the list. At this point we don't need the myteam
Team GetTeam = (from t in myTeams where t.TeamName == cbPlayerTeams.Text select t).FirstOrDefault();
if (GetTeam.Players.Count < 4)
{
GetTeam.Players.Add(i);
lbMembers.Items.Add(i.FirstName + " " + i.LastName);
}
else
{
MessageBox.Show("You must remove a team member before entering a another player.");
}
/*************************************************************************/
txtFName.Clear();
txtLName.Clear();
txtRank.Clear();
txtLastScore.Clear();
txtHandiCap.Clear();
}
else
{
MessageBox.Show("You must Select a Team before entering a player");
}
}
catch (Exception u)
{
MessageBox.Show(u.Message);
}
} // End btnAddUpdate_Click method
private void LoadData_Click(object sender, EventArgs e)
{
myTeams = ReadXML();
PopulateComboboxes();
} // End LoadTeams_Click method
private void SaveData_Click(object sender, EventArgs e)
{
WriteXML(myTeams);
} // End SaveTeams_Click method
private void Exit_Click(object sender, EventArgs e)
{
this.Dispose();
} // End Exit_Click method
private void About_Click(object sender, EventArgs e)
{
AboutBox About = new AboutBox();
About.Show();
} // End LoadTeams_Click method
private void lbMembers_SelectedIndexChanged(object sender, EventArgs e)
{
// Local Method Variables
Golfer lbGolfer = new Golfer();
Team lbTeam = new Team();
try
{
lbTeam = (from x in myTeams where x.TeamName == cbPlayerTeams.Text select x).FirstOrDefault();
lbGolfer = (from i in lbTeam.Players where i.FirstName + " " + i.LastName == lbMembers.SelectedItem.ToString() select i).FirstOrDefault();//.Contains(lbMembers.SelectedItem.ToString()) select i).FirstOrDefault();
if (lbGolfer != null)
{
txtFName.Text = lbGolfer.FirstName;
txtLName.Text = lbGolfer.LastName;
txtHandiCap.Text = lbGolfer.Handicap.ToString();
txtLastScore.Text = lbGolfer.LastGame.ToString();
txtRank.Text = lbGolfer.RankOnTeam.ToString();
}
}
catch (Exception z)
{
MessageBox.Show(z.Message);
}
} // End lbMembers_SelectedIndexChanged method
private void btnDeleteplayer_Click(object sender, EventArgs e)
{
// Local Method Variables
Golfer lbGolfer = new Golfer();
Team lbTeam = new Team();
try
{
lbTeam = (from x in myTeams where x.TeamName == cbPlayerTeams.Text select x).FirstOrDefault();
myTeams.Remove(lbTeam);
lbGolfer = (from i in lbTeam.Players where i.FirstName + " " + i.LastName == lbMembers.SelectedItem.ToString() select i).FirstOrDefault();
lbTeam.Players.Remove(lbGolfer);
myTeams.Add(lbTeam);
lbMembers.Items.Remove(lbMembers.SelectedItem); //lbMembers.Text.StartsWith(lbGolfer.FirstName);// Remove(lbGolfer.FirstName + " " + lbGolfer.LastName);
txtFName.Clear();
txtLName.Clear();
txtRank.Clear();
txtLastScore.Clear();
txtHandiCap.Clear();
}
catch (Exception n)
{
MessageBox.Show(n.Message);
}
} // End btnDeleteplayer_Click method
private void btnThisTeam_Click(object sender, EventArgs e)
{
// Local Method Variables
Team singleteamreport = new Team();
singleteamreport = (from x in myTeams where x.TeamName == cbViewTeam.Text select x).FirstOrDefault();
int length = singleteamreport.Players.Count();
int counter = 1;
String[] ReportOut = new String[length + 1];
ReportOut[0] = "***********TEAM " + singleteamreport.TeamName + " Ranked #" + singleteamreport.TeamRank.ToString() + "****************";
foreach (Golfer i in singleteamreport.Players)
{
string rep = "Golfer: " + i.FirstName + " " + i.LastName + " Handicap: " + i.Handicap.ToString() + " Last Score: " + i.LastGame.ToString() + " Rank: " + i.RankOnTeam.ToString();
ReportOut[counter] = rep;
counter++;
}
File.WriteAllLines(tbFilePath.Text, ReportOut);
} // End btnThisTeam_Click method
private void btnAllTeams_Click(object sender, EventArgs e)
{
foreach (Team Allteamreport in myTeams)
{
int length = Allteamreport.Players.Count();
int counter = 1;
String[] ReportOut = new String[length + 1];
ReportOut[0] = "***********TEAM " + Allteamreport.TeamName + " Ranked #" + Allteamreport.TeamRank.ToString() + "****************";
foreach (Golfer i in Allteamreport.Players)
{
string rep = "Golfer: " + i.FirstName + " " + i.LastName + " Handicap: " + i.Handicap.ToString() + " Last Score: " + i.LastGame.ToString() + " Rank: " + i.RankOnTeam.ToString();
ReportOut[counter] = rep;
counter++;
}
File.WriteAllLines(tbFilePath.Text, ReportOut);
} // End foreach
} // End btnAllTeams_Click method
private void btnbtnCalucateTeamRanks_Click(object sender, EventArgs e)
{
// Calculate the players rankings then use the best players score to rank the teams
try
{
CalculateTeamMemberRankings();
CalculateTeamScore();
MessageBox.Show("Calculation is complete");
}
catch (Exception n)
{
MessageBox.Show(n.Message);
}
} // End btnbtnCalucateTeamRanks_Click method
private void CalculateTeamMemberRankings()
{
// Local Method Variables
Golfer teamMember = new Golfer();
int counter = 1;
foreach (Team team in myTeams)
{
// Sort Team members on best score
team.Players.Sort(delegate(Golfer p1, Golfer p2)
{
return p1.Score().CompareTo(p2.Score());
});
// Set rank on team based on position in list
foreach (Golfer golfer in team.Players)
{
golfer.RankOnTeam = counter;
counter++;
}
// Reset counter for next teams ranking
counter = 1;
}
} // End CalculateTeamMemberRankings method
private void CalculateTeamScore()
{
// Local Method Variables
Golfer teamMember = new Golfer();
int counter = 1;
// Sort Teams on #1 players score
myTeams.Sort(delegate(Team t1, Team t2)
{
return t1.Score().CompareTo(t2.Score());
});
foreach (Team team in myTeams)
{
team.TeamRank = counter;
counter++;
}
} // End CalculateTeamScore method
private void PopulateComboboxes()
{
// This method is required because the comboboxes do not have the Team list as their data source
foreach (Team team in myTeams)
{
cbUpdateTeam.Items.Add(team.TeamName);
cbViewTeam.Items.Add(team.TeamName);
cbPlayerTeams.Items.Add(team.TeamName);
}
} // End PopulateComboboxes method
static List ReadXML()
{
XmlSerializer reader = new XmlSerializer(typeof(List));
StreamReader file = new StreamReader(@"GolfTeams.xml");
List teams;
teams = (List)reader.Deserialize(file);
file.Close();
return teams;
} // End ReadXML(List) method
static public void WriteXML(List teams)
{
XmlSerializer writer = new XmlSerializer(typeof(List));
StreamWriter file = new StreamWriter(@"GolfTeams.xml");
teams.Sort(delegate(Team t1, Team t2)
{
return t1.TeamRank.CompareTo(t2.TeamRank);
});
writer.Serialize(file, teams);
file.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
} // End WriteXML(List) method
} // End Form1 cLass
} // End Namespace