Using Using Using Using Using: Program
Using Using Using Using Using: Program
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
/// Teaches: 1. Sorting with IComparable Interface
/// 2. Reading text file with StreamReader
/// 3. Writing text file using StreamWriter
/// Written by: OA Fakinlede
/// With Visual Studio 2010
/// Creation Date: January 13, 2011
/// Last Modified: January 13, 2011
/// The student.txt file in bin/debug in the program directory
namespace SuperStudent
{
class Program
{
static void Main(string[] args)
{
Officer[] MyOfficers = new Officer[50];
for (int iota = 0; iota < 50; iota++)
MyOfficers[iota] = new Officer();
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader strRead = new StreamReader("students.txt"))
{
string LineSentence;
// Read and display lines from the file until the end of
// the file is reached.
for (int i = 0; i < 50; i++)
{
if ((LineSentence = strRead.ReadLine()) == null)
break;
string[] WordToken = LineSentence.Split(' ');
// Split each line read into the formatted token
// values with a blank delimiter:
MyOfficers[i].MyLastName = WordToken[0];
MyOfficers[i].MyFirstName = WordToken[1];
MyOfficers[i].MyMatric = WordToken[2];
MyOfficers[i].MyAge = short.Parse(WordToken[3]);
MyOfficers[i].MySSG313 = float.Parse(WordToken[4]);
MyOfficers[i].MySSG314 = float.Parse(WordToken[5]);
MyOfficers[i].MySSG303 = float.Parse(WordToken[6]);
MyOfficers[i].MySSG401 = float.Parse(WordToken[7]);
MyOfficers[i].MySSG402 = float.Parse(WordToken[8]);
MyOfficers[i].MySSG403 = float.Parse(WordToken[9]);
MyOfficers[i].MyHostel =
(Hostel)(int.Parse(WordToken[10]));
MyOfficers[i].MyClub =
(Club)(int.Parse(WordToken[11]));
}
Console.WriteLine("Before Sorting with Hostel and Average");
for (int j = 0; j < 50; j++)
{
Console.WriteLine(" {0} Average {1:F} {2} {3} ",
MyOfficers[j].MyHostel, MyOfficers[j].Average(),
MyOfficers[j].MyFirstName, MyOfficers[j].MyLastName);
}
Console.ReadKey();
Array.Sort(MyOfficers);
Console.WriteLine("After Sorting with Hostel and Average");
for (int j = 0; j < 50; j++)
{
Console.WriteLine(" {0} Average {1:F} {2} {3} ",
MyOfficers[j].MyHostel, MyOfficers[j].Average(),
MyOfficers[j].MyFirstName, MyOfficers[j].MyLastName);
}
Console.ReadKey();
}
using (StreamWriter strWrite = new StreamWriter("StdOut.txt"))
{
for (int j = 0; j < 50; j++)
{
strWrite.WriteLine(" {0} Average {1:F} {2} {3} ",
MyOfficers[j].MyHostel, MyOfficers[j].Average(),
MyOfficers[j].MyFirstName, MyOfficers[j].MyLastName);
}
}
}
}
class Student
{
// Fields
string LastName;
short Age;
string Matric;
float SSG313;
float SSG314;
string Gender;
// Constructor with parameters
public Student(string ln, short ag, string mat, int grd1,
int grd2, string gen)
{
LastName = ln;
Age = ag;
Matric = mat;
SSG313 = grd1;
SSG314 = grd2;
Gender = gen;
// Console.WriteLine("Parameterized Constructor");
}
// Constructor without parameters
public Student()
: this("Lagbaja", 19, "123456", 45, 75, "Male")
{
// Console.WriteLine("Default Constructor {0}", ++i);
}
// Public Properties/
public string MyLastName
{
set { LastName = value; }
get { return LastName; }
}
public short MyAge
{
set { Age = value; }
get { return Age; }
}
public string MyMatric
{
set { Matric = value; }
get { return Matric; }
}
public float MySSG313
{
set { SSG313 = value; }
get { return SSG313; }
}
public float MySSG314
{
set { SSG314 = value; }
get { return SSG314; }
}
public string MyGender
{
set { Gender = value; }
get { return Gender; }
}
// Compute the average score of students
public virtual float Average()
{
float Ave = (MySSG313 + MySSG314) / 2;
return Ave;
}
public static void Swap(ref Student S1, ref Student S2)
{
Student temp = new Student();
temp = S1;
S1 = S2;
S2 = temp;
}
}
class Officer : Student, IComparable
{
UnionPost Post;
TenurePost Term;
string FirstName;
Hostel hostel;
Club club;
float SSG303;
float SSG401;
float SSG402;
float SSG403;
int VotesAgainst, VotesFor;
public bool elected()
{
if(VotesAgainst<VotesFor)
return true;
return false;
}
public Officer(UnionPost up, TenurePost tp, int va, int vf) {
Post = up;
Term = tp;
VotesFor = vf;
VotesAgainst = va;
}
public Officer(UnionPost up, TenurePost tp, int va, int vf,
string ln, short ag, string mat, int grd1, int grd2, string gen)
:base(ln, ag, mat, grd1, grd2, gen)
{
Post = up;
Term = tp;
VotesFor = vf;
VotesAgainst = va;
SSG303 = 0; SSG401 = 0; SSG402 = 0; SSG403 = 0;
club = Club.AISEC; hostel = Hostel.Mariere;
}
public Officer(): this(UnionPost.Sports, TenurePost.current, 100,100)
{}
public int CompareTo(object obj)
{
// Accept the regular Hostel ordering unless Hostel
// values are equal; In that case, compare averages ...
if (this.MyHostel == (obj as Officer).MyHostel)
{
return (this.Average()).CompareTo(((Officer)obj).Average());
}
else
return (this.MyHostel).CompareTo((obj as Officer).MyHostel);
}
public override float Average() {
return (MySSG313 + MySSG314 + MySSG303 +
MySSG401 + MySSG402 + MySSG403) / 6;
}
public string MyFirstName
{
set { FirstName = value; }
get { return FirstName; }
}
public Hostel MyHostel
{
set { hostel = value; }
get { return hostel; }
}
public Club MyClub
{
set { club = value; }
get { return club; }
}
public float MySSG303
{
set { SSG303 = value; }
get { return SSG303; }
}
public float MySSG401
{
set { SSG401 = value; }
get { return SSG401; }
}
public float MySSG402
{
set { SSG402 = value; }
get { return SSG402; }
}
public float MySSG403
{
set { SSG403 = value; }
get { return SSG403; }
}
}
enum UnionPost{President, SecGeneral, FinSec, Welfare,
Sports, Council, FacDelegate, VP, AssSG, Treasurer}
enum TenurePost { last, current, next }
enum Hostel { Mariere=1, Jaja, Moremi, Tinubu, ElKanemi, Amina, HenryCarr}
enum Club { SCM=1, Kegites, Sigma, Arsenal, AISEC}
}