Skip to content

Commit 889d1ba

Browse files
committed
Add code for University Subjects
1 parent 7ce28eb commit 889d1ba

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 4/8/2020
5+
* Time: 10:17 PM
6+
*/
7+
8+
/**
9+
* Class AbstractUniversity
10+
*/
11+
abstract class AbstractUniversity
12+
{
13+
/**
14+
* @var Subject[]
15+
*/
16+
public $subjects = [];
17+
18+
/**
19+
* Method accepts the name and code of the Subject, creates instance of the class,
20+
* adds the instance in $subjects array and returns created instance
21+
*
22+
* @param string $code
23+
* @param string $name
24+
* @return Subject
25+
*/
26+
abstract public function addSubject(string $code, string $name): Subject;
27+
28+
/**
29+
* Method accepts subject code and Student. Finds subject in $subjects array based on code and adds student to its array.
30+
*
31+
* @param string $subjectCode
32+
* @param \Student $student
33+
* @return mixed
34+
*/
35+
abstract public function addStudentOnSubject(string $subjectCode, Student $student);
36+
37+
/**
38+
* Method returns students for given subject
39+
*
40+
* @param string $subjectCode
41+
* @return mixed
42+
*/
43+
abstract public function getStudentsForSubject(string $subjectCode);
44+
45+
/**
46+
* This method returns number of total students registered on all subjects
47+
*
48+
* @return int
49+
*/
50+
abstract public function getNumberOfStudents(): int;
51+
52+
/**
53+
* Method must iterate over $subjects, print the subject name then "-" 25 times,
54+
* then iterate over students of the subject and print student name and student number in format
55+
* StudentName - StudentNumber
56+
* Student2Name - Student2Number
57+
*
58+
* @return mixed
59+
*/
60+
abstract public function print();
61+
}

University_Subjects/Student.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 4/8/2020
5+
* Time: 10:40 PM
6+
*/
7+
8+
/**
9+
* Class Student
10+
*/
11+
class Student
12+
{
13+
public string $name;
14+
public string $studentNumber;
15+
16+
// TODO Generate getters and setters for both arguments
17+
// TODO Generate constructor with both arguments.
18+
}

University_Subjects/Subject.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 4/8/2020
5+
* Time: 10:16 PM
6+
*/
7+
8+
/**
9+
* Class Subject
10+
*/
11+
class Subject
12+
{
13+
public $code;
14+
public $name;
15+
/**
16+
* @var Student[]
17+
*/
18+
public $students = [] ;
19+
20+
// TODO Generate getters and setters
21+
// TODO Generate constructor for all attributes. $students argument of the constructor can be empty
22+
23+
/**
24+
* Method accepts student name and number, creates instance of the Student class, adds inside $students array
25+
* and returns created instance
26+
*
27+
* @param string $name
28+
* @param string $studentNumber
29+
* @return \Student
30+
*/
31+
public function addStudent(string $name, string $studentNumber): Student
32+
{
33+
// TODO Implement method according to method annotation
34+
}
35+
}

University_Subjects/University.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
require_once "AbstractUniversity.php";
3+
4+
/**
5+
* Class University
6+
*/
7+
class University extends AbstractUniversity
8+
{
9+
// TODO Implement all the methods declared in parent
10+
}

University_Subjects/index.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
require_once "Student.php";
4+
require_once "Subject.php";
5+
require_once "University.php";
6+
7+
$university = new University();
8+
$subject = $university->addSubject('112', 'Web II');
9+
$subject->addStudent('George', '123');
10+
$subject->addStudent("Mary", '234');
11+
$university->addStudentOnSubject('112', new Student('David', '345'));
12+
13+
$subject2 = $university->addSubject('113', 'Web III');
14+
$subject2->addStudent('Bob', '456');
15+
$subject2->addStudent('Brad', '567');
16+
17+
echo $university->getNumberOfStudents(); // This must print 5, because we totally added 5 students in different subjects
18+
19+
/**
20+
* This method must print all subjects and for each subject all students registered on the subject int the following format
21+
*
22+
* SubjectCode - SubjectName
23+
* -------------------------
24+
* StudentName - StudentNumber
25+
* Student2Name - Student2Number
26+
* ... // and so on...
27+
*
28+
* Subject2Code - Subject2Name
29+
* --------------------
30+
* StudentName - StudentNumber
31+
* Student2Name - Student2Number
32+
* ... // and so on...
33+
*/
34+
$university->print();
35+
// Example
36+
/*
37+
112 - Web II
38+
--------------------
39+
George - 123
40+
Mary - 234
41+
David - 345
42+
43+
113 - Web III
44+
--------------------
45+
Bob - 456
46+
Brad - 567
47+
*/

0 commit comments

Comments
 (0)