You are on page 1of 3

//Fields

private long id;


private long cedula;
private string name;
private string streetAddress;
private string phone;
private DateTime birthday;

//Properties
public long Id { get { return this.id; } set { this.id = value; } }
public long Cedula { get { return this.cedula; } set { this.id = value; } }
public string Name { get { return this.name; } set { this.name = value; } }
public string StreetAddress { get { return this.streetAddress; } set
{ this.streetAddress = value; } }
public string Phone { get { return this.phone; } set { this.phone =
value; } }
public DateTime Birthday { get { return this.birthday; } set
{ this.birthday = value; } }

//Constructors
public Person()
{
}
private Person(string[] data)
{
this.id = Convert.ToInt64(data[0]);
this.cedula = Convert.ToInt64(data[1])
this.name = data[2];
this.streetAddress = data[3];
this.phone = data[4];
this.birthday = Convert.ToDateTime(data[5]);
}

//Methods
public void Add(string fileName)
{
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter(fileName,
true);
file.WriteLine(this.id.ToString() + "\t" +this.cedula + "\t"
this.name + "\t" + this.streetAddress + "\t" + this.phone + "\t" +
this.birthday.ToString());
file.Close();
}
catch (Exception ex)
{
}
}
public bool Search(string fileName)
{
try
{
System.IO.StreamReader file = new System.IO.StreamReader(fileName);
while (!file.EndOfStream)
{
Person person = new Person(file.ReadLine().Split('\t'));
if (this.id == person.id)
{
this.name = person.name;
this.streetAddress = person.streetAddress;
this.phone = person.phone;
this.birthday = person.birthday;
file.Close();
return true;
}
}
file.Close();
}
catch (Exception ex)
{
}
return false;
}
public void Update(string fileName)
{
try
{
System.IO.StreamReader file = new System.IO.StreamReader(fileName);
while (!file.EndOfStream)
{
Person person = new Person(file.ReadLine().Split('\t'));
if (this.id != person.id)
{
person.Add("$" + fileName);
}
else
{
this.Add("$" + fileName);
}
}
file.Close();
System.IO.File.Delete(fileName);
System.IO.File.Copy("$" + fileName, fileName);
System.IO.File.Delete("$" + fileName);
}
catch (Exception ex)
{
}
}
public void Delete(string fileName)
{
try
{
System.IO.StreamReader file = new System.IO.StreamReader(fileName);
while (!file.EndOfStream)
{
Person person = new Person(file.ReadLine().Split('\t'));
if (this.id != person.id)
{
person.Add("$" + fileName);
}
}
file.Close();
System.IO.File.Delete(fileName);
System.IO.File.Copy("$" + fileName, fileName);
System.IO.File.Delete("$" + fileName);
}
catch (Exception ex)
{
}
}
}
}

You might also like