BillHung.Net


powered by FreeFind     sms

Lab 03 Airline Seats

31 Oct 2006

Output
using System;

namespace Lab03_Airline_Seats
{
//Bill Chun Wai Hung
//10/31/2006
//learned how to use BinarySearch in ArrayList
//learned passing parameters to the base class
//use properties to access class variables
//sub functions are listed in alphabetical order
class Program
{
private Airline airline = new Airline("AIRLINE.CSV");

static void Main(string[] args)
{
Program p = new Program();

p.airline.WriteFile("AIRLINE.output.CSV");
p.airline.lookUp();
p.HoldtheConsoleWindow();
}

//hold the display of the Console Window during debugging (pressing F5)
public void HoldtheConsoleWindow()
{
Console.WriteLine("Press any keys to continue...");
Console.ReadLine();
}
}

public class Airline
{
private System.Collections.ArrayList seats = new System.Collections.ArrayList();
private System.IO.StreamReader reader = null;
private System.IO.StreamWriter writer = null;

//constructor for the Airline class
public Airline() { }
public Airline(String fname)
{
ReadPassengers(fname);
}

//look up and print the passenger name at certain row and column
public void lookUp()
{
String strRow = "";
String strColumn = "";
CompareSeat seatCompare = new CompareSeat();

Console.Write("Row (a-z): ");
strRow = Console.ReadLine();

Console.Write("Column (1-6): ");
strColumn = Console.ReadLine();
Seat tempSeat = new Seat(strRow, strColumn); //the seat we are searching

int arrayIndex = seats.BinarySearch(tempSeat, seatCompare);
if (arrayIndex < 0)
{
Console.WriteLine("Empty");
}
else
{
tempSeat = (Seat)seats[arrayIndex];
Console.WriteLine(tempSeat.fullName);
}
}
//read a file for input data and store the data into an ArrayList
public int ReadPassengers(string sfilename)
{
string delimitStr = ",\n";

char[] delimiter = delimitStr.ToCharArray();
string[] strLine = null;

try
{
reader = new System.IO.StreamReader(sfilename);
}
catch
{
Console.WriteLine("open file {0} error\n", sfilename);
}

for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
Console.WriteLine(line);

strLine = line.Split(delimiter);
if (strLine.Length == 4)
{
seats.Add(new Seat(strLine[0], strLine[1], strLine[2], strLine[3]));
}
}
reader.Close();

return seats.Count;
}

//write the airline data from the array list to an output file 
public void WriteFile(string sfilename)
{
try
{
writer = new System.IO.StreamWriter(sfilename);
}
catch
{
Console.WriteLine("open file {0} error\n", sfilename);
}

foreach (Seat s in seats)
{
Console.WriteLine("Seat: {0}", s.ToString());
writer.WriteLine("Seat: {0}", s.ToString());
}
writer.Close();
}

public Seat this[int index]
{
get { return (Seat)seats[index]; }
set { seats[index] = value; }
}
}

public class Person
{
protected String sFirstName = "Empty";
protected String sLastName = null;

//constructors for the Person class
public Person() { }
public Person(String strFirstName, String strLastName)
{
sFirstName = strFirstName;
sLastName = strLastName;
}

//accessor for first name
public string firstName
{
get { return sFirstName; }
set { sFirstName = value; }
}

//accessor for last name
public string lastName
{
get { return sLastName; }
set { sLastName = value; }
}
//overriding the ToString() function
public override string ToString()
{
return String.Format("{0}\t{1}", firstName, lastName);
}
}

public class Seat : Person
{
private char chRow;
private int intColumn;

//3 constructor for the Seat class
public Seat() {}
public Seat(String strRow, String strColumn)
{
chRow = System.Convert.ToChar(strRow);
intColumn = System.Convert.ToInt32(strColumn);
}
public Seat(String strLastName, String strFirstName, String strRow, String strColumn)
: base(strLastName, strFirstName)
{
chRow = System.Convert.ToChar(strRow);
intColumn = System.Convert.ToInt32(strColumn);
}

//accessors for row
public string row
{
get { return chRow.ToString(); }
set { chRow = System.Convert.ToChar(value); }
}

//accessors for column
public string column
{
get { return intColumn.ToString(); }
set { intColumn = System.Convert.ToInt32(value); }
}

//accessor for the full name of a person
public string fullName
{
get { return lastName + ", " + firstName; }
}

//overriding the ToString() function
public override string ToString()
{
return String.Format("{0}\t{1}\t{2}", row, column, base.ToString());
}
}
//comparer for the binarysearch of the arraylist seats
class CompareSeat : System.Collections.IComparer
{
int System.Collections.IComparer.Compare(Object x, Object y)
{
Seat sx = (Seat)x;
Seat sy = (Seat)y;
return ((System.Convert.ToInt32(sx.column) - System.Convert.ToInt32(sy.column))
+
(System.Convert.ToChar(sx.row)*100 - System.Convert.ToChar(sy.row)*100));
}
}
}