How to improve

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Author : Owen Jordan S00122290

* Date : 30/03/2012

* Description :
- C# Assignment 2 (20%)
-Issued March 27/3/2012

-Printout Due in class Tuesday 17/4/2012

- A menu driven program is required the user create an report and/or search the file

- The user shall be able to create report and search as often as they like. The program will terminate when exit is selected.

- Search

- Allow the user enter a player number, the program will then search the file, and display the players name, and the score for chosen player.
If the player is not found display a message stating “No match found”.

- Alongside the score, you must display the appropriate citation for each player, use the table below do to determine the citation.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Assignment2
{


class Assignment2
{
static int enterChoice = 0;
static string lineIn ;
static string[] fields = new string[3];
static char[] seperator = { , };

static void Main(string[] args)
{

while (enterChoice != 3)
{
gameMenu();
switchChoice();
Console.ReadKey();
Console.Clear();
}
}
static void gameMenu()
{
//Console.ForegroundColor(ConsoleColor.Yellow);
Console.WriteLine(" ------------------------------GAME MENU----------------------------------");
Console.Write("");
Console.WriteLine(" 1. Player Report ");
Console.WriteLine(" 2. Search for a Player ");
Console.WriteLine(" 3. Exit ");
Console.Write("");
Console.WriteLine(" Enter Choice : ");
enterChoice = Convert.ToInt32(Console.ReadLine());


}
static void playerReport()
{
// you must make this file and put on your h:drive

StreamReader txtFile = File.OpenText("score.txt");
double total=0,avg,count=0;
lineIn = txtFile.ReadLine();

string[] fields = new string[3];
string rewardName = "";
int rewardDecider;

char[] seperator = { , };
Console.WriteLine("{0,-30} {1,-10} {2,-20}", "Player Name", "Score", "Citation");


while (lineIn != null) // checking for end-of-file
{
// process record

fields = lineIn.Split(seperator);

rewardDecider = Convert.ToInt32(fields[2]);

if (rewardDecider <= 400)
{
rewardName= ("Sluggish Snail");
}

if (rewardDecider > 400 && rewardDecider <= 599)
{
rewardName= ("Ambling Armadillo");
}

if (rewardDecider > 600 && rewardDecider <= 699)
{
rewardName= ("Bobbing Bobcat");
}

if (rewardDecider > 700 && rewardDecider <= 999 )
{
rewardName= ("Rocketing Rabbit");
}
if( rewardDecider > 999)
{
rewardName= ("TurboCharged Cheetah");
}

total += Convert.ToDouble(fields[2]);
count++;
Console.WriteLine("{0,-30} {1,-10} {2,-20}",fields[1],fields[2],rewardName);

// get the next record

lineIn = txtFile.ReadLine();
}

avg = total / count;
Console.WriteLine("===========================================================");
Console.WriteLine("{0,-30} {1,-10} ","Average Score", avg);
Console.WriteLine("{0,-30} {1,-10}", "Top Player", "FireFox");

// close connection
txtFile.Close();
Console.ReadKey();
}
/* static void searchPlayer()
{
string searchNumber;
Console.Write( "Please Enter player number : ");
searchNumber = Console.ReadLine();

StreamReader txtFile = File.OpenText("score.txt");
lineIn = txtFile.ReadLine();
fields = lineIn.Split(seperator);

// if (searchNumber == fields[0])
if(lineIn.Contains(searchNumber))
{
Console.WriteLine("Player Name : {0}", fields[1]);
Console.WriteLine("Score : {0}", fields[2]);
}

else
{
Console.WriteLine("No Matches Found");
lineIn = txtFile.ReadLine();
}


} */
static void searchPlayer()
{
string searchNumber;
Console.Write("Please Enter player number : ");
searchNumber = Console.ReadLine();
bool matches = false;

StreamReader txtFile = File.OpenText("score.txt");
lineIn = txtFile.ReadLine();
//fields = lineIn.Split(seperator);


while (lineIn != null && matches== false)
{
fields = lineIn.Split(seperator);
if (lineIn.Contains(searchNumber))
{
Console.WriteLine("Player Name : {0}", fields[1]);
Console.WriteLine("Score : {0}", fields[2]);
matches = true;
}


lineIn = txtFile.ReadLine();

}

if (matches == false)
{
Console.WriteLine("No matches Found");

}
txtFile.Close();
}


static void switchChoice()
{
switch (enterChoice)
{
case 1:
playerReport();
break;

case 2:
searchPlayer();
break;

case 3:
//exit
Console.WriteLine("Goodbye");
break;

default:
Console.WriteLine("");
Console.WriteLine("Wrong selection, you must select option 1 , 2 or 3!.");
break;
}
}
}
}

View the full article
 

Similar threads

P
Replies
0
Views
174
Policy standard local admin account with Active Di
P
Back
Top