0% found this document useful (0 votes)
18 views

IOFiles With Students

This document contains C# code that defines classes and methods for managing student data. It defines a Student class with properties like ID, name, GPA and password. It also defines methods to write a single student or array of students to a text file, read from the file, and update/check student passwords. The main method tests creating sample student objects, writing them to a file, and reading the file contents.

Uploaded by

Weirdly GG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

IOFiles With Students

This document contains C# code that defines classes and methods for managing student data. It defines a Student class with properties like ID, name, GPA and password. It also defines methods to write a single student or array of students to a text file, read from the file, and update/check student passwords. The main method tests creating sample student objects, writing them to a file, and reading the file contents.

Uploaded by

Weirdly GG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

using System;

using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project27March
{
class Program
{
/// <summary>
/// a function that add the information of one student into a specified file
/// </summary>
/// <param name="s">the student to be added</param>
/// <param name="file_name">the file name</param>
static void WriteOneStudentToFile(Student s, String file_name)
{
StreamWriter sw = new StreamWriter(file_name, true);
sw.WriteLine(s.getID() + "|" + s.getFN() + "|" + s.getLN() + "|" + s.getGPA()
+ "|" + s.getPassword());
sw.Close();
}

static void WriteArrayOfStudentsToFile1(Student[] students, String file_name)


{
foreach (Student s in students)
{
WriteOneStudentToFile(s, file_name);
}
}

/// <summary>
/// Function to copy the info of many students into the file at once
/// </summary>
/// <param name="students">array of students</param>
/// <param name="file_name">file name</param>
static void WriteArrayOfStudentsToFile(Student[] students, String file_name)
{
StreamWriter sw = new StreamWriter(file_name, true);

foreach (Student s in students)


{
sw.WriteLine(s.getID() + "|" + s.getFN() + "|" + s.getLN() + "|" +
s.getGPA() + "|" + s.getPassword());
}

sw.Close();
}

static void Main(string[] args)


{
/*
// test the class student
Student s = new Student("A01223", "Rachad", "Wehbe");
s.setPassword("NewPass");
s.print();
// test the StreamWriter with one object of type student
String file_name="students.txt";

//Console.WriteLine("Enter the file name");


//file_name=Console.ReadLine();
// File.Delete(file_name); to delete the file if already exists

/// Write the information of this student into the file


/*StreamWriter sw = new StreamWriter(file_name,true);
sw.WriteLine(s.getID()+"|"+s.getFN()+"|"+s.getLN()+"|"+s.getGPA()+"|" +
s.getPassword());
sw.Close();
WriteOneStudentToFile(s, file_name);
*/

// create array of 3 students and fill with 3 students

if (File.Exists("students.txt"))
File.Delete("students.txt");

Student[] MECT280 = new Student[3];


MECT280[0] = new Student("AAA", "Roy", "Wehbe");
MECT280[0].setPassword("Password");
MECT280[1] = new Student("BBB", "Eyad", "Nasr");
MECT280[1].setPassword("Password");
MECT280[2] = new Student("CCC", "Roni", "Zeidan");
MECT280[2].setPassword("Password");

WriteArrayOfStudentsToFile(MECT280, "students.txt");

Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project27March
{
class Student
{
/// <summary>
/// Data Members or Properties
/// </summary>
private string id;
private string fn, ln;
private double gpa;
private string password;
public static int nb = 0;

/// <summary>
/// A default constructor
/// </summary>
public Student()
{
id = "-1";
fn = "";
ln = "";
gpa = 0;
password = "Password";
nb++;
}

/// <summary>
/// A Constructor that take parameters
/// </summary>
/// <param name="identifier"> the Students id </param>
/// <param name="first_name"> the first name </param>
/// <param name="last_name"> the last name </param>
public Student(string identifier, String first_name, String last_name)
{
id = identifier;
fn = first_name;
ln = last_name;
gpa = 0;
password = "password";
nb++;
}

public string getPassword()


{
return password;
}

/// <summary>
/// sets a default password fn.ln
/// </summary>
public void setPassword()
{
password = fn + "." + ln;
}

/// <summary>
/// setter for password
/// </summary>
/// <param name="pass"></param>
public void setPassword(String pass)
{
password = pass;
}

/// <summary>
/// prints the attributes of a student id, name and gpa
/// </summary>
public void print()
{
Console.WriteLine("Student id: " + id);
Console.WriteLine("Name: " + fn + " " + ln);
Console.WriteLine("gpa: " + gpa.ToString("0.0"));
}

/// <summary>
/// setter for gpa
/// </summary>
/// <param name="v"></param>
public void setGPA(double v)
{
gpa = v;
}

/// <summary>
/// getter for gpa
/// </summary>
/// <returns></returns>
public double getGPA()
{
return gpa;
}

/// <summary>
/// setter for id
/// </summary>
/// <param name="id"></param>
public void setID(string id)
{
this.id = id;
}

/// <summary>
/// getter for id
/// </summary>
/// <returns></returns>
public string getID()
{
return id;
}
/// <summary>
/// setter for fn
/// </summary>
/// <param name="fn"></param>
public void setFN(String fn)
{
this.fn = fn;
}

/// <summary>
/// getter for fn
/// </summary>
/// <returns></returns>
public String getFN()
{
return fn;
}

/// <summary>
/// setter for ln
/// </summary>
/// <param name="ln"></param>
public void setLN(String ln)
{
this.ln = ln;
}

/// <summary>
/// getter for ln
/// </summary>
/// <returns></returns>
public String getLN()
{
return ln;
}

/// <summary>
/// it checks if the id and password are correct
/// </summary>
/// <param name="id"></param>
/// <param name="password"></param>
/// <returns></returns>
public bool login(string id, String password)
{
if ((this.id == id) && (this.password == password))
{
return true;
}
else
return false;
}

/// <summary>
/// changes the password
/// </summary>
/// <param name="curr_password"></param>
/// <param name="new_pass1"></param>
/// <param name="new_pass2"></param>
/// <returns></returns>
public bool changePassord(String curr_password, String new_pass1, String
new_pass2)
{
bool res = false;

if (curr_password == this.password)
{
if (new_pass1 == new_pass2)
{
this.password = new_pass1;
res = true;
}
}
return res;
}

You might also like