COMP1551 - Application Development Coursework
COMP1551 - Application Development Coursework
Number of words: 1732 (excluding table of contents, heading and code fields)
GCD220190
001306081 COMP1551
Table of Contents
1. Description of the System..................................................................................................2
2. Software Requirements Specification................................................................................ 2
2.1. Introduction..................................................................................................................... 2
2.1.1. Purpose................................................................................................................... 2
2.1.2. Project Scope...........................................................................................................2
2.2. Overall Description.......................................................................................................... 2
2.2.1. Product.................................................................................................................... 2
2.2.2. Users........................................................................................................................3
2.2.3. Operational Environment........................................................................................ 3
2.3. System Features...............................................................................................................3
2.3.1. Description.............................................................................................................. 3
2.3.2. Functional Requirements........................................................................................ 3
2.4. User Interface.................................................................................................................. 3
2.4.1. View All Users Interface...........................................................................................4
2.4.2. View Admin Interface.............................................................................................. 5
2.4.3. View Teacher Interface............................................................................................ 5
2.4.4. View Student Interface............................................................................................6
2.5. Platform........................................................................................................................... 6
2.6. Quality Attributes.............................................................................................................7
2.6.1. Performance............................................................................................................7
2.6.2. Security....................................................................................................................7
2.6.3. Safety.......................................................................................................................7
3. UML.................................................................................................................................. 7
3.1. Class Diagram...................................................................................................................7
3.2. Use Case Diagram............................................................................................................ 8
4. Object-Oriented Programming...........................................................................................9
4.1. Class................................................................................................................................. 9
4.1.1. Person......................................................................................................................9
4.1.2. Admin.................................................................................................................... 12
4.1.3. Teacher.................................................................................................................. 15
4.1.4. Student.................................................................................................................. 18
4.2. OOP Features................................................................................................................. 19
GCD220190
0
001306081 COMP1551
4.2.1. Inheritance............................................................................................................ 19
4.2.2. Encapsulation........................................................................................................ 19
4.2.3. Polymorphism....................................................................................................... 19
5. Testing.............................................................................................................................19
6. Future Improvements...................................................................................................... 21
7. Appendix.........................................................................................................................22
7.1. Classes............................................................................................................................22
7.1.1. Person.cs............................................................................................................... 22
7.1.2. Admin.cs................................................................................................................25
7.1.3. Teacher.cs.............................................................................................................. 27
7.1.4. Student.cs..............................................................................................................30
7.1.5. Role.cs................................................................................................................... 33
7.1.6. EmploymentType.cs.............................................................................................. 33
7.2. Form and User Controls................................................................................................. 34
7.2.1. MainForm.cs..........................................................................................................34
7.2.2. AllUsers.cs............................................................................................................. 37
7.2.3. AdminData.cs........................................................................................................ 40
7.2.4. TeacherData.cs...................................................................................................... 48
7.2.5. StudentData.cs...................................................................................................... 58
7.3. Testing Evidence.............................................................................................................69
7.3.1. Test Case 1.............................................................................................................69
7.3.2. Test Case 2.............................................................................................................69
7.3.3. Test Case 3.............................................................................................................70
7.3.4. Test Case 4.............................................................................................................71
7.3.5. Test Case 5.............................................................................................................72
7.3.6. Test Case 6.............................................................................................................73
7.3.7. Test Case 7.............................................................................................................74
7.3.8. Test Case 8.............................................................................................................75
7.3.9. Test Case 9.............................................................................................................76
7.3.10. Test Case 10.........................................................................................................77
7.3.11. Test Case 11.........................................................................................................78
7.3.12. Test Case 12.........................................................................................................79
7.3.13. Test Case 13.........................................................................................................80
7.3.14. Test Case 14.........................................................................................................81
GCD220190 1
001306081 COMP1551
GCD220190 2
001306081 COMP1551
2.2.2. Users
The administrators of the education centre are the system's main users. These users will
deal with staff and student records on a daily basis through the system. The teachers
and students are the users that have information stored in the system, not directly the
ones that use the system.
2.2.3. Operational Environment
The Desktop Information System is designed to run on the Microsoft Windows OS,
requiring a minimum of 4 GB of RAM, a multi-core processor, and adequate storage
capacity for database management.
Database management system that I use for this project is SQL Server by Microsoft. and
the project is developed with C#.
2.3. System Features
2.3.1. Description
Several important modules will be included in the Desktop Information System,
including:
a. Administrator Management is in charge of handling data regarding wages, types
of employment, and working hours.
b. Teacher Management, which oversees the subjects each teacher teaches as well
as pay information.
c. Information on the disciplines that students are studying and finishing is kept in
Student Management.
2.3.2. Functional Requirements
Upon starting, all user data that is kept in the system database should be displayed. The
system administrator has the option to see the data according to role (student,
instructor, administrator). The system shall enable the administrator to view every
record for every role, add new records, change current records, and remove records for
the selected role in each role view.
2.4. User Interface
The system features a user-friendly graphical user interface, allowing the administrator
to navigate through different modules easily. The interface includes a clear menu and a
data entry form to help the administrators manage the records efficiently.
In addition, the system uses consistent graphical elements throughout the modules.
GCD220190 3
001306081 COMP1551
In the “View All Users” interface, a table of all information of the users stored in
database is displayed. The columns represent the attributes of the users. The empty
values mean that the user does not have that attribute.
In this interface, you can only view but can not modify the information of the users.
GCD220190 4
001306081 COMP1551
In the View Admin interface, there is a a table displaying the information of the
administrators. This interface is also where you can add, edit and delete an admin.
2.4.3. View Teacher Interface
GCD220190 5
001306081 COMP1551
Similar to the “View Admin” interface, this interface shows the information of the users
with the role “Teacher”, including their PersonID, TeacherID, basic information, and
role-specific information, such as salary and teaching subjects.
This interface is also where you can add, edit and delete users with “Teacher” role.
2.4.4. View Student Interface
In the “View Student” interface, the information of the students are displayed in the
table, including basic information and the subjects they are learning and completed.
In this interface, you can add new student, edit the information of the student, and
delete the students’ records. You can choose the subjects with the drop-down field,
which ensure the consistency in storing the information about the subjects.
2.5. Platform
The system is designed for deployment on the Microsoft Windows OS and requires a
minimum of 4 GB of RAM, a multi-core processor, and sufficient storage space for
handling databases.
GCD220190 6
001306081 COMP1551
3. UML
3.1. Class Diagram
The Desktop Information System for an educational institution, which manages data
concerning three main user groups—Administration, Teaching Staff, and Students—is
depicted in the Class Diagram.
GCD220190 7
001306081 COMP1551
The Person class, which encapsulates common attributes like name, phone number,
email, and role in the education facility, is the main class, as illustrated in figure 5. The
particular user groups inherit from this superclass.
The Administration class is exclusive to the system administrator. It contains
characteristics pertaining to employment and pay in addition to system activities for
managing records.
The Teacher class adds unique properties pertinent to teaching professionals, like
compensation and subjects taught, while inheriting basic attributes from the "Pesron"
class.
Presenting the students of the education centre, the Student class focusses on their
academic records while capturing particular traits that are pertinent to the students.
A person's potential roles within the system are defined by the Role class.
The "Administration" class is linked to the EmploymentType class, which indicates
whether an administrator works full- or part-time.
3.2. Use Case Diagram
The Use Case diagram above shows the functionalities and interactions within the
system. In the system, there are three primary user roles: Administrator, Teacher and
Student.
GCD220190 8
001306081 COMP1551
The core functions of the system are encapsulated in use cases such as “Add New Data”,
“Edit Existing Data”, Delete Existing Data”, and “View All Existing Data”. These functions
are fundamental to data management within the system.
The use case “View Existing Data of the Role” is included within the “Add New Data”
meaning that when you create a new user record of a certain role, you will see the
records of that role.
4. Object-Oriented Programming
4.1. Class
4.1.1. Person
public class Person
{
// Connect to the database
SqlConnection conn = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ADMIN\Documents\comp1551
_coursework.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False");
// Data members
public int PersonID { get; internal set; } // internal set so that it can
only be set within the class
public string Name { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
public Role UserRole { get; set; }
public decimal? Salary { get; set; }
public string TeachingSubject1 { get; set; }
public string TeachingSubject2 { get; set; }
public string CurrentSubject1 { get; set; }
public string CurrentSubject2 { get; set; }
public string PreviousSubject1 { get; set; }
public string PreviousSubject2 { get; set; }
Above is the data members of the class Person, including PersonID, Name,
Telephone, Email, UserRole, Salary, TeachingSubject1, TeachingSubject2,
CurrentSubject1, CurrentSubject2, PreviousSubject1, and
PreviousSubject2. Not all the data members are for the Person object, but they are
there to help retrieve all people from the database later on by joining multiple tables.
GCD220190 9
001306081 COMP1551
// Default constructor
public Person() { }
The class has a default constructor and a constructor that takes name, telephone,
email, and userRole parameters to create a Person object.
// Method to retrieve a list of all people from the database
public List<Person> UserList()
{
// Create an empty list to store the retrieved users
List<Person> users = new List<Person>();
if (conn.State != ConnectionState.Open)
{
try
{
conn.Open();
// SQL query to retrieve user data from multiple tables
string query = @"SELECT
p.PersonID, p.Name, p.Telephone, p.Email, p.Role,
COALESCE(a.Salary, t.Salary) AS Salary,
t.TeachingSubject1, t.TeachingSubject2,
s.CurrentSubject1, s.CurrentSubject2,
s.PreviousSubject1, s.PreviousSubject2
FROM Person p
LEFT JOIN Admin a ON p.PersonID = a.PersonID
LEFT JOIN Teacher t ON p.PersonID = t.PersonID
LEFT JOIN Student s ON p.PersonID = s.PersonID
";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
// Execute the command and create a SqlDataReader to read the
results
SqlDataReader reader = cmd.ExecuteReader();
{
GCD220190 10
001306081 COMP1551
GCD220190 11
001306081 COMP1551
{
conn.Close();
}
}
return users;
}
The UserList() method retrieves a list of all people from the database using a SQL
query that joins Person, Admin, and Student tables to get all the relevant data of all
the user groups. The method then returns a List<Person> object.
The UserList() method handles any SqlException or Exception that may occur
during the database operation and displays the error message using
MessageBox.Show().
For the full code of the Person class, you can find it the Appendix 9.6
4.1.2. Admin
public class Admin : Person // Inherite from Person class
{
SqlConnection conn = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ADMIN\Documents\comp1551
_coursework.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False");
// Data members
public string AdminID { get; set; }
public new decimal Salary { get; set; } // Overrides Person.Salary
public EmploymentType employmentType { get; set; }
public int WorkingHours { get; set; }
The Admin class inherits from the Person class. The Sqlconnection conn is used to
connect to the SQL Server database for SQL query below.
The data members of the Admin class are AdminID, Salary, employmentType, and
WorkingHours. The Salary property in the Admin class overrides the
Person.Salary property.
// Default constructor
public Admin() { }
GCD220190 12
001306081 COMP1551
public Admin (int personID, string name, string telephone, string email, Role
userRole, string adminID, decimal salary, EmploymentType employmentType, int
workingHours)
: base(name, telephone, email, userRole)
{
this.PersonID = personID;
this.Name = name;
this.Telephone = telephone;
this.Email = email;
this.UserRole = userRole;
this.AdminID = adminID;
this.Salary = salary;
this.employmentType = employmentType;
this.WorkingHours = workingHours;
}
The class has a default constructor and a constructor that takes personID, name,
telephone, email, and userRole parameters inherited from the Person class
together with adminID, salary, employmentType, and workingHours parameters
to create an Admin object.
The this. prefix keyword is used to explicitly refer to the class member variables,
ensuring that they are assigned the values passed as parameters to the constructor.
This prefix helps ensuring that the class member variables are properly initialized.
// Method to retrieve a list of all Admin objects from the databas
public List<Admin> admins()
{
// Create an empty list to store retrieved Admin objects
List<Admin> adminList = new List<Admin>();
if (conn.State != ConnectionState.Open)
{
try
{
// Open the connection to the database
conn.Open();
// SQL query to retrieve Admin data with a join on the Person table
string query = @"SELECT
p.PersonID, a.AdminID, p.Name, p.Telephone, p.Email,
a.Salary, a.EmploymentType, a.WorkingHours
FROM Admin a
LEFT JOIN Person p ON a.PersonID = p.PersonID
WHERE p.Role = 'Admin'
GCD220190 13
001306081 COMP1551
ORDER BY a.AdminID";
GCD220190 14
001306081 COMP1551
}
return adminList;
}
The admins() method retrieves a list of all Admin objects from the database using a
SQL query that joins the Admin and Person tables to get all the relevant data. This
method returns a List<Admin> object. The method also ensures that the database
connection is closed arter the operation is completed, regardless of whether an
exception occurred or not.
The method also handles any Exception may occur during the database operation
4.1.3. Teacher
public class Teacher : Person // Inherite from Person class
{
SqlConnection conn = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ADMIN\Documents\comp1551
_coursework.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False");
// Data members
public string TeacherID { get; set; }
public new decimal Salary { get; set; } // Overrides Person.Salary
public new string TeachingSubject1 { get; set; } // Overrides
Person.TeachingSubject1
public new string TeachingSubject2 { get; set; } // Overrides
Person.TeachingSubject2
The Teacher class also inherited from the Person class, and has several data
members, including TeacherID, Salary that overrides the Person.Salary
property, TeachingSubject1 and TeachingSubject2, which also override the
properties of the Person class.
// Default constructor
public Teacher() { }
GCD220190 15
001306081 COMP1551
this.PersonID = personID;
this.Name = name;
this.Telephone = telephone;
this.Email = email;
this.UserRole = userRole;
this.TeacherID = teacherID;
this.Salary = salary;
this.TeachingSubject1 = teachingSubject1;
this.TeachingSubject2 = teachingSubject2;
}
The class has a default constructor as well, and a constructor that takes parameters to
create a Teacher object.
// Method to retrieve a list of all Teacher objects from the database
public List<Teacher> teachers()
{
// Create an empty list to store retrieved Teacher objects
List<Teacher> teacherList = new List<Teacher>();
if(conn.State != ConnectionState.Open)
{
try
{
conn.Open(); // Open the connection to the database
GCD220190 16
001306081 COMP1551
{
Teacher teacher = new Teacher(); // Create a new Teacher
object
{
// Assign values from the data reader to the Teacher
object properties
teacher.PersonID = Convert.ToInt32(reader["PersonID"]);
teacher.TeacherID = reader["TeacherID"].ToString();
teacher.Name = reader["Name"].ToString();
teacher.Telephone = reader["Telephone"].ToString();
teacher.Email = reader["Email"].ToString();
teacher.Salary = Convert.ToDecimal(reader["Salary"]);
teacher.TeachingSubject1 =
reader["TeachingSubject1"].ToString();
teacher.TeachingSubject2 =
reader["TeachingSubject2"].ToString();
};
teacherList.Add(teacher);
}
reader.Close();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
return teacherList;
}
The teachers() method retrieves a list of all Teacher objects from the database by
joining the Teacher and Person tables to get all the relevant data. The method
returns a List<Teacher> object.
The method handles any Exception that may occur and displays the error message
using MessageBox.Show()
GCD220190 17
001306081 COMP1551
4.1.4. Student
public class Student : Person // Inherite from Person class
{
SqlConnection conn = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ADMIN\Documents\comp1551
_coursework.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False");
// Data members
public string StudentID { get; set; }
public new string CurrentSubject1 { get; set; } // Overrides
Person.CurrentSubject1
public new string CurrentSubject2 { get; set; } // Overrides
Person.CurrentSubject2
public new string PreviousSubject1 { get; set; } // Overrides
Person.PreviousSubject1
public new string PreviousSubject2 { get; set; } // Overrides
Person.PreviousSubject2
The Student class is another class that inherits from the Person class. Its data
members are also override some of the Person class properties.
// Default constructor
public Student() { }
GCD220190 18
001306081 COMP1551
Similar to the Admin and Teacher classes, the Student class also has a default
constructor and a constructor that takes the parameters to create a Student object.
4.2. OOP Features
4.2.1. Inheritance
The Admin, Teacher, and Student classes all inherit from the Person class,
demonstrating the use of inheritance in OOP.
4.2.2. Encapsulation
Data encapsulation is demonstrated by the classes, which have private data members
and offer public properties for data access and modification.
4.2.3. Polymorphism
Method overriding, a type of polymorphism, is demonstrated by the Salary,
TeachingSubject1, TeachingSubject2, CurrentSubject1,
CurrentSubject2, PreviousSubject1, and PreviousSubject2 properties in the
derived classes overriding the corresponding properties in the parent Person class.
5. Testing
No. Test Scenario Expected Actual Evidence Pass/Fail
Result Result
GCD220190 19
001306081 COMP1551
GCD220190 20
001306081 COMP1551
6. Future Improvements
This is a simple application and still lacks some functionalities that can improve the
experience of using the system. In the future, there is a potential for further
improvements, such as adding authentication and allow the teachers and students to
use the system.
GCD220190 21
001306081 COMP1551
7. Appendix
7.1. Classes
7.1.1. Person.cs
using COMP1551_Coursework.Models.Enums;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace COMP1551_Coursework.Models
{
public class Person
{
// Connect to the database
SqlConnection conn = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ADMIN\Documents\comp1551
_coursework.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False");
// Data members
public int PersonID { get; internal set; } // internal set so that it
can only be set within the class
public string Name { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
public Role UserRole { get; set; }
public decimal? Salary { get; set; }
public string TeachingSubject1 { get; set; }
public string TeachingSubject2 { get; set; }
public string CurrentSubject1 { get; set; }
public string CurrentSubject2 { get; set; }
public string PreviousSubject1 { get; set; }
public string PreviousSubject2 { get; set; }
// Default constructor
public Person() { }
GCD220190 22
001306081 COMP1551
if (conn.State != ConnectionState.Open)
{
try
{
conn.Open();
// SQL query to retrieve user data from multiple tables
string query = @"SELECT
p.PersonID, p.Name, p.Telephone, p.Email,
p.Role,
COALESCE(a.Salary, t.Salary) AS Salary,
t.TeachingSubject1, t.TeachingSubject2,
s.CurrentSubject1, s.CurrentSubject2,
s.PreviousSubject1, s.PreviousSubject2
FROM Person p
LEFT JOIN Admin a ON p.PersonID = a.PersonID
LEFT JOIN Teacher t ON p.PersonID =
t.PersonID
LEFT JOIN Student s ON p.PersonID =
s.PersonID
";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
// Execute the command and create a SqlDataReader to
read the results
SqlDataReader reader = cmd.ExecuteReader();
{
// Loop through each row in the result set retrieved
from the database
GCD220190 23
001306081 COMP1551
while (reader.Read())
{
Person user = new Person();
{
// Assign values from the reader to the user
object
user.PersonID =
Convert.ToInt32(reader["PersonID"]);
user.Name = reader["Name"].ToString();
user.Telephone =
reader["Telephone"].ToString();
user.Email = reader["Email"].ToString();
user.UserRole =
(Role)Enum.Parse(typeof(Role), reader["Role"].ToString());
user.Salary = reader["Salary"] ==
DBNull.Value ? (decimal?)null : Convert.ToDecimal(reader["Salary"]);
user.TeachingSubject1 =
reader["TeachingSubject1"].ToString();
user.TeachingSubject2 =
reader["TeachingSubject2"].ToString();
user.CurrentSubject1 =
reader["CurrentSubject1"].ToString();
user.CurrentSubject2 =
reader["CurrentSubject2"].ToString();
user.PreviousSubject1 =
reader["PreviousSubject1"].ToString();
user.PreviousSubject2 =
reader["PreviousSubject2"].ToString();
}
users.Add(user);
}
// Close the SqlDataReader to release resources
reader.Close();
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
GCD220190 24
001306081 COMP1551
}
return users;
}
}
}
7.1.2. Admin.cs
using COMP1551_Coursework.Models.Enums;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace COMP1551_Coursework.Models
{
public class Admin : Person // Inherite from Person class
{
SqlConnection conn = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ADMIN\Documents\comp1551
_coursework.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False");
// Data members
public string AdminID { get; set; }
public new decimal Salary { get; set; } // Overrides Person.Salary
public EmploymentType employmentType { get; set; }
public int WorkingHours { get; set; }
// Default constructor
public Admin() { }
GCD220190 25
001306081 COMP1551
this.UserRole = userRole;
this.AdminID = adminID;
this.Salary = salary;
this.employmentType = employmentType;
this.WorkingHours = workingHours;
}
if (conn.State != ConnectionState.Open)
{
try
{
// Open the connection to the database
conn.Open();
// SQL query to retrieve Admin data with a join on the
Person table
string query = @"SELECT
p.PersonID, a.AdminID, p.Name, p.Telephone,
p.Email,
a.Salary, a.EmploymentType, a.WorkingHours
FROM Admin a
LEFT JOIN Person p ON a.PersonID =
p.PersonID
WHERE p.Role = 'Admin'
ORDER BY a.AdminID";
GCD220190 26
001306081 COMP1551
admin.PersonID =
Convert.ToInt32(reader["PersonID"]);
admin.AdminID = reader["AdminID"].ToString();
admin.Name = reader["Name"].ToString();
admin.Telephone =
reader["Telephone"].ToString();
admin.Email = reader["Email"].ToString();
admin.Salary =
Convert.ToDecimal(reader["Salary"]);
admin.employmentType =
(EmploymentType)Enum.Parse(typeof(EmploymentType),
reader["EmploymentType"].ToString());
admin.WorkingHours =
Convert.ToInt32(reader["WorkingHours"]);
};
adminList.Add(admin); // Add the created Admin
object to the list
}
reader.Close();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
}
}
return adminList;
}
}
}
7.1.3. Teacher.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
GCD220190 27
001306081 COMP1551
using System.Windows.Forms;
using COMP1551_Coursework.Models.Enums;
namespace COMP1551_Coursework.Models
{
public class Teacher : Person // Inherite from Person class
{
SqlConnection conn = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ADMIN\Documents\comp1551
_coursework.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False");
// Data members
public string TeacherID { get; set; }
public new decimal Salary { get; set; } // Overrides Person.Salary
public new string TeachingSubject1 { get; set; } // Overrides
Person.TeachingSubject1
public new string TeachingSubject2 { get; set; } // Overrides
Person.TeachingSubject2
// Default constructor
public Teacher() { }
GCD220190 28
001306081 COMP1551
if(conn.State != ConnectionState.Open)
{
try
{
conn.Open(); // Open the connection to the database
GCD220190 29
001306081 COMP1551
teacher.TeachingSubject2 =
reader["TeachingSubject2"].ToString();
};
teacherList.Add(teacher);
}
reader.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error connection Database: " + ex.Message);
}
finally
{
conn.Close();
}
}
return teacherList;
}
}
}
7.1.4. Student.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMP1551_Coursework.Models.Enums;
namespace COMP1551_Coursework.Models
{
public class Student : Person // Inherite from Person class
{
SqlConnection conn = new SqlConnection(@"Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ADMIN\Documents\comp1551
_coursework.mdf;Integrated Security=True;Connect Timeout=30;Encrypt=False");
// Data members
public string StudentID { get; set; }
GCD220190 30
001306081 COMP1551
// Default constructor
public Student() { }
if (conn.State != ConnectionState.Open)
{
try
{
conn.Open(); // Open the connection to the database
GCD220190 31
001306081 COMP1551
GCD220190 32
001306081 COMP1551
reader.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Error connecting Database: " + ex.Message);
}
finally
{
conn.Close();
}
}
return studentList;
}
}
}
7.1.5. Role.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace COMP1551_Coursework.Models.Enums
{
public enum Role
{
Admin, // Represents an Admin role
Teacher, // Represents a Teacher role
Student // Represents a Student role
}
}
7.1.6. EmploymentType.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace COMP1551_Coursework.Models.Enums
{
GCD220190 33
001306081 COMP1551
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMP1551_Coursework.Forms.User_Control;
namespace COMP1551_Coursework
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
GCD220190 34
001306081 COMP1551
adminData1.PersonDeleted += AdminData1_PersonDeleted;
studentData1.PersonDeleted += StudentData1_PersonDeleted;
teacherData1.PersonDeleted += TeacherData1_PersonDeleted;
GCD220190 35
001306081 COMP1551
adminData1.Visible = false;
studentData1.Visible = false;
teacherData1.Visible = false;
allUsers1.Visible = false;
}
GCD220190 36
001306081 COMP1551
7.2.2. AllUsers.cs
using COMP1551_Coursework.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace COMP1551_Coursework.Forms.User_Control
{
public partial class AllUsers : UserControl
{
private AdminData adminData;// Create a new AdminData object
private TeacherData teacherData;// Create a new TeacherData object
private StudentData studentData;// Create a new StudentData object
GCD220190 37
001306081 COMP1551
this.adminData = adminData;
this.teacherData = teacherData;
this.studentData = studentData;
GCD220190 38
001306081 COMP1551
GCD220190 39
001306081 COMP1551
previousSubject2Column.DataPropertyName = "PreviousSubject2";
previousSubject2Column.HeaderText = "Previous Subject 2";
dgvUsers.Columns.Add(previousSubject2Column);
7.2.3. AdminData.cs
using COMP1551_Coursework.Models;
using COMP1551_Coursework.Models.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace COMP1551_Coursework.Forms.User_Control
{
public partial class AdminData : UserControl
{
public event EventHandler PersonAdded; // Declare an event to notify
subscribers when a person is added
public event EventHandler PersonUpdated; // Declare an event to notify
subscribers when a person is updated
public event EventHandler PersonDeleted; // Declare an event to notify
subscribers when a person is deleted
GCD220190 40
001306081 COMP1551
public AdminData()
{
InitializeComponent();
LoadEmploymentTypes(); // Call the LoadEmploymentTypes method to
load the employment types into the ComboBox
adminData(); // Call the adminData method to display all admins
}
GCD220190 41
001306081 COMP1551
telephoneColumn.HeaderText = "Telephone";
dgvAdmin.Columns.Add(telephoneColumn);
GCD220190 42
001306081 COMP1551
new person
using (SqlCommand personCmd = new SqlCommand(personQuery,
conn)) // Create a new SqlCommand object
{
// Add parameters to the query
personCmd.Parameters.AddWithValue("@Name",
txtName.Text.Trim());
personCmd.Parameters.AddWithValue("@Telephone",
txtPhone.Text.Trim());
personCmd.Parameters.AddWithValue("@Email",
txtEmail.Text.Trim());
personCmd.Parameters.AddWithValue("@Role",
Role.Admin.ToString());
personID = (int)personCmd.ExecuteScalar(); // Execute
the query and store the ID of the new person
}
GCD220190 43
001306081 COMP1551
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Message Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
}
GCD220190 44
001306081 COMP1551
GCD220190 45
001306081 COMP1551
{
MessageBox.Show("Please select an admin to delete", "Error
Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
GCD220190 46
001306081 COMP1551
{
MessageBox.Show("SQL Error: " + ex.Message, "Message Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Message Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
}
GCD220190 47
001306081 COMP1551
7.2.4. TeacherData.cs
using COMP1551_Coursework.Models;
using COMP1551_Coursework.Models.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace COMP1551_Coursework.Forms.User_Control
{
public partial class TeacherData : UserControl
{
public event EventHandler PersonAdded; // Declare an event to notify
subscribers when a person is added
public event EventHandler PersonUpdated; // Declare an event to notify
subscribers when a person is updated
public event EventHandler PersonDeleted; // Declare an event to notify
GCD220190 48
001306081 COMP1551
GCD220190 49
001306081 COMP1551
GCD220190 50
001306081 COMP1551
|| cBoxTS2.Text == "")
{
MessageBox.Show("Please fill all fields.", "Error Message",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (cBoxTS1.Text == cBoxTS2.Text) // Check if the subjects are
the same (preventing duplicates)
{
MessageBox.Show("Error: Subjects cannot be the same.", "Error
Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
LoadSubjects(); // Reload the form to allow the user to correct
the input
}
else
{
if (conn.State != ConnectionState.Open)
{
try
{
conn.Open();
personID = (int)personCmd.ExecuteScalar(); //
Execute the query and get the PersonID of the newly inserted record
}
GCD220190 51
001306081 COMP1551
teacherCmd.Parameters.AddWithValue("@TeachingSubject1", cBoxTS1.Text);
teacherCmd.Parameters.AddWithValue("@TeachingSubject2", cBoxTS2.Text);
}
// Display a success message
MessageBox.Show("Teacher added successfully.",
"Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
teacherData(); // Reload the DataGridView control
GCD220190 52
001306081 COMP1551
GCD220190 53
001306081 COMP1551
updateTeacherCmd.Parameters.AddWithValue("@TeachingSubject1", cBoxTS1.Text);
updateTeacherCmd.Parameters.AddWithValue("@TeachingSubject2", cBoxTS2.Text);
updateTeacherCmd.Parameters.AddWithValue("@PersonID",
personID);
updateTeacherCmd.ExecuteNonQuery();
GCD220190 54
001306081 COMP1551
}
catch (SqlException ex)
GCD220190 55
001306081 COMP1551
{
MessageBox.Show("SQL Error: " + ex.Message, "Message Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Message Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
}
GCD220190 56
001306081 COMP1551
GCD220190 57
001306081 COMP1551
7.2.5. StudentData.cs
using COMP1551_Coursework.Models;
using COMP1551_Coursework.Models.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace COMP1551_Coursework.Forms.User_Control
{
public partial class StudentData : UserControl
{
public event EventHandler PersonAdded; // Declare an event to notify
subscribers when a person is added
public event EventHandler PersonUpdated; // Declare an event to notify
subscribers when a person is updated
public event EventHandler PersonDeleted; // Declare an event to notify
subscribers when a person is deleted
GCD220190 58
001306081 COMP1551
public StudentData()
{
InitializeComponent();
LoadSubjects(); // Load subjects into ComboBox controls
studentData(); // Display student data in the DataGridView
}
GCD220190 59
001306081 COMP1551
DataGridViewTextBoxColumn();
telephoneColumn.DataPropertyName = "Telephone";
telephoneColumn.HeaderText = "Telephone";
dgvStudents.Columns.Add(telephoneColumn);
GCD220190 60
001306081 COMP1551
if (txtName.Text == ""
|| txtPhone.Text == ""
|| txtEmail.Text == ""
|| cBoxCS1.Text == ""
|| cBoxCS2.Text == ""
|| cBoxPS1.Text == ""
|| cBoxPS2.Text == "")
{
MessageBox.Show("Please fill all fields", "Error Message",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// Check if any of the subjects are the same (preventing duplicates)
else if (cBoxCS1.Text == cBoxCS2.Text || cBoxCS1.Text ==
cBoxPS1.Text || cBoxCS1.Text == cBoxPS2.Text
|| cBoxCS2.Text == cBoxPS1.Text || cBoxCS2.Text == cBoxPS2.Text
|| cBoxPS1.Text == cBoxPS2.Text)
{
MessageBox.Show("Error: Subjects cannot be the same", "Error
Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
LoadSubjects(); // Reload the form to allow the user to correct
the input
}
else
{
if (conn.State != ConnectionState.Open)
{
try
{
conn.Open();
GCD220190 61
001306081 COMP1551
personCmd.Parameters.AddWithValue("@Email",
txtEmail.Text.Trim());
personCmd.Parameters.AddWithValue("@Role",
Role.Student.ToString());
personID = (int)personCmd.ExecuteScalar(); //
Execute the query and get the PersonID of the newly inserted row
}
studentCmd.Parameters.AddWithValue("@CurrentSubject1", cBoxCS1.Text.Trim());
studentCmd.Parameters.AddWithValue("@CurrentSubject2", cBoxCS2.Text.Trim());
studentCmd.Parameters.AddWithValue("@PreviousSubject1", cBoxPS1.Text.Trim());
studentCmd.Parameters.AddWithValue("@PreviousSubject2", cBoxPS2.Text.Trim());
studentCmd.ExecuteNonQuery(); // Execute the query
}
GCD220190 62
001306081 COMP1551
GCD220190 63
001306081 COMP1551
updateStudentCmd.Parameters.AddWithValue("@CurrentSubject1",
cBoxCS1.Text.Trim());
updateStudentCmd.Parameters.AddWithValue("@CurrentSubject2",
cBoxCS2.Text.Trim());
updateStudentCmd.Parameters.AddWithValue("@PreviousSubject1",
cBoxPS1.Text.Trim());
updateStudentCmd.Parameters.AddWithValue("@PreviousSubject2",
cBoxPS2.Text.Trim());
updateStudentCmd.Parameters.AddWithValue("@PersonID",
personID);
updateStudentCmd.ExecuteNonQuery(); // Execute the query
GCD220190 64
001306081 COMP1551
GCD220190 65
001306081 COMP1551
GCD220190 66
001306081 COMP1551
if (dgvStudents.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dgvStudents.SelectedRows[0]; //
Get the selected row
return Convert.ToInt32(selectedRow.Cells[0].Value); // Get the
PersonID of the selected student
}
else
{
throw new InvalidOperationException("No student selected.");
}
}
GCD220190 67
001306081 COMP1551
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Message Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
}
GCD220190 68
001306081 COMP1551
GCD220190 69
001306081 COMP1551
GCD220190 70
001306081 COMP1551
GCD220190 71
001306081 COMP1551
GCD220190 72
001306081 COMP1551
GCD220190 73
001306081 COMP1551
GCD220190 74
001306081 COMP1551
GCD220190 75
001306081 COMP1551
GCD220190 76
001306081 COMP1551
GCD220190 77
001306081 COMP1551
GCD220190 78
001306081 COMP1551
GCD220190 79
001306081 COMP1551
GCD220190 80
001306081 COMP1551
GCD220190 81