How would you improve on this C# code ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace s00125769
{
class Program
{
// Runs through the whole program
static string userChoice, citation;

static void Main(string[] args)
{
// Method that displays the menu
ShowStartMenu();
}

// Region lets you specify a block of code that you can expand or collapse
#region Application

#region Shows Menu

static void ShowStartMenu()
{
// Changes the colour of the text
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("GAME MENU");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("1. Player Report");
Console.WriteLine("2. Player Search");
Console.WriteLine("3. Score Analysis");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("4. Exitn");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Please enter your choice (1, 2, 3, 4)");

// Displays the menu
userChoice = Console.ReadLine().ToLower();
Console.WriteLine ();
// Method for menu choice
MenuChoice();
}

#endregion

#region Controls Menu Choice

static void MenuChoice()
{
// Switch statement
switch (userChoice)
{

case "1":
// Runs method
PlayerReport();
break;

case "2":
SearchPlayer();
break;

case "3":
ScoreAnalysis();
break;

case "4":
// Code to close program
Environment.Exit(0);
break;

default:
// If user does not pick 1-4 this message will display
Console.WriteLine("Error, Please try again or exit program");
// Brings up menu
ShowStartMenu();
break;
}
}

#endregion

#region Menu choice 1


static void PlayerReport()
{
// Variables
double total = 0, avgScore, counts = 0;
int score;

// Opens text file
StreamReader customerFile = File.OpenText("score.txt");

// Reads in text file
string lineIn = customerFile.ReadLine();

// Defines the array to have 3 values or elements
string[] fields = new string[3];

// Seperates text, Char is a character that has 1 letter in it
char[] separator = { , };

// Writes out the player name, score and citation, {} for lineing them up on the console appication
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0,-25} {1,-15} {2,-20}", "Player Name", "Score", "Citation");
Console.ForegroundColor = ConsoleColor.White;

// While Loop
// LineIn is not equal to null, causes the program to read text until all text is read
while (lineIn != null)
{
// Reads in text file and separates it, split means return the string array
fields = lineIn.Split(separator);

// converts to int 32 for the score in player report to give the citation
score = Convert.ToInt32(fields[2]);

// If statement, eg: If score is below 400 Sluggish Snail will display
if (score <= 400)
{
citation = ("Sluggish Snail");
}

if (score > 400 && score <= 599)
{
citation = ("Ambling Armadillo");
}

if (score > 600 && score <= 699)
{
citation = ("Bobbing Bobcat");
}

if (score > 700 && score <= 999)
{
citation = ("Rocketing Rabbit");
}

if (score > 999)
{
citation = ("TurboCharged Cheetah");
}

// Adds up the total score
total += Convert.ToDouble(fields[2]);

// Counts the amount of players
counts++;

// Writes in the table, brings up fields[1], fields[2] and citation
Console.WriteLine("{0,-25} {1,-15} {2,-20}", fields[1], fields[2], citation);

// Reads in the file on the console appication
lineIn = customerFile.ReadLine();
}

// To work out the average score
avgScore = total / counts;
Console.WriteLine("==============================================================");
Console.WriteLine("{0,-25} {1,-15}", "Average Score", avgScore);
Console.WriteLine("{0,-25} {1,-15}", "Top Player", "Davidn");

// To close the text file
customerFile.Close();
ShowStartMenu();
Console.ReadLine();
}

#endregion

#region Menu choice 2

// Method to search player
static void SearchPlayer()
{

Console.WriteLine("Please enter player number");
// Converts it to upper case
string playerName = Console.ReadLine().ToUpper();

//Switch Statement
switch (playerName)
{
// Player number 1
case "G001":
Console.WriteLine("nPlayer Name: Hot Shot");
Console.WriteLine("Score: t280n");
break;

// Player number 2
case "G002":
Console.WriteLine("nPlayer Name: Loose Cannon");
Console.WriteLine("Score: t650n");
break;

// Player number 3
case "G003":
Console.WriteLine("nPlayer Name: Fire fox");
Console.WriteLine("Score: t720n");
break;

// Player number 4
case "G004":
Console.WriteLine("nPlayer Name: Foxxy");
Console.WriteLine("Score: t100n");
break;

// Player number 5
case "G005":
Console.WriteLine("nPlayer Name: David");
Console.WriteLine("Score: t1200n");
break;

// Error message
default:
Console.WriteLine("No player foundn");

SearchPlayer();
ShowStartMenu();
break;

}

ShowStartMenu();

// End method for searchPlayer
}

#endregion

#region Menu Choice 3


static void ScoreAnalysis()
{

// Open text file
StreamReader customerFile = File.OpenText("score.txt");

// Reads in text file
string lineIn = customerFile.ReadLine();

// Sets the score to 0
int score = 0;

// Displays the range
string[] range =
{
"0-399",
"400-599",
"600-699",
"700-799",
"800-999",
"1000 and over"
};

// Defines the array to have 3 values or elements
string[] players = new string[3];

// Defines the array to have 5 values or elements
int[] fields = new int[5];

// Seperates text, Char is a character that has 1 letter in it
char[] separator = { , };

// While Loop
while (lineIn != null)
{
// Reads in text file and separates it, split means return the string array
players = lineIn.Split(separator);

// Converts to int 32 for the score in Score Analysis to give the range
score = Convert.ToInt32(players[2]);

// Working outs for displaying numbers
if (score <= 399)

fields[0]++;

else if (score > 400 && score <= 599)

fields[1]++;

else if (score > 600 && score <= 699)

fields[2]++;

else if (score > 700 && score <= 999)

fields[3]++;

else if (score > 1000)

fields[4]++;

// Reads in the file on the console appication
lineIn = customerFile.ReadLine();
}
// Writes out the table
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0,-10}{1}", "Range", "Number in this range");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("{0,-10}{1}", "=====", "====================");

// For Loop
for (int i = 0; i < 5; i++)
{
// Brings in the range and fields
Console.WriteLine("{0,-20}{1}n", range, fields);
}

ShowStartMenu();
customerFile.Close();
Console.ReadLine();

#endregion

#endregion

}
}
}

View the full article
 
Back
Top