Skip to content

Instantly share code, notes, and snippets.

@Xevion
Created December 25, 2021 19:21
Show Gist options
  • Save Xevion/3456f2140df7cb607fa32a532cb58540 to your computer and use it in GitHub Desktop.
Save Xevion/3456f2140df7cb607fa32a532cb58540 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Given students' names along with the grade that they are in, create a roster for the school.
.DESCRIPTION
In the end, you should be able to:
Add a student's name to the roster for a grade
"Add Jim to grade 2."
"OK."
Get a list of all students enrolled in a grade
"Which students are in grade 2?"
"We've only got Jim just now."
Get a sorted list of all students in all grades. Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
"Who all is enrolled in school right now?"
"Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe. Grade 3…"
Note that all our students only have one name. (It's a small town, what do you want?)
.EXAMPLE
PS C:\> $roster = [Roster]::new()
$roster.AddStudent(1,'Billy')
$roster.AddStudent(1,'Josh')
$roster.AddStudent(2,'Allison')
$roster.GetRoster()
$roster.GetRoster(2)
This will create a new roster and add 3 students to it.
When no arguments are supplied to the GetRoster method, all students will be returned.
When a grade number is supplied to the GetRoster method, students from that grade will be returned.
#>
class Student {
[int] $Grade
[string] $Name
Student([int] $grade, [string] $name) {
$this.Grade = $grade
$this.Name = $name
}
}
class Roster {
[Student[]] $Student = @()
Roster() {
}
[Student[]] GetRoster() {
return $this.Student
}
[Student[]] GetRoster([int] $grade) {
return $this.Student | Where-Object {$_.Grade -eq $grade}
}
[void] AddStudent([int] $grade, [string] $name) {
[Student] $temp = [Student]::new($grade, $name)
$this.Student.Add($temp)
}
}
BeforeAll {
$ScriptFile = '.\GradeSchool.ps1'
# Load the script file
If (Test-Path "$ScriptFile"){
Write-Output ("Loading: {0}" -f "$ScriptFile")
. ("$ScriptFile")
try {
$Types = @([Student],[Roster])
foreach ($type in $Types) {
$type | Out-Null
}
}
catch {
# Display an error and stop the tests
Write-Error "The file $ScriptFile must implement the two PowerShell classes Student and Roster" -ErrorAction Stop
}
}
Else {
# Display an error and stop the tests
Write-Error "The file $ScriptFile was not found. You need to create your answer in a file named $ScriptFile" -ErrorAction Stop
}
}
Describe "Grade School Test cases" {
It "Adding a student adds them to the sorted roster" {
$roster = [Roster]::new()
$roster.AddStudent(2,'Aimee')
$roster.Student.Name | Should -Be 'Aimee'
}
It "Adding more student adds them to the sorted roster" {
$roster = [Roster]::new()
$roster.AddStudent(2,'Blair')
$roster.AddStudent(2,'James')
$roster.AddStudent(2,'Paul')
$roster.GetRoster().Name | Should -Be ('Blair', 'James', 'Paul')
}
It "Adding students to different grades adds them to the same sorted roster" {
$roster = [Roster]::new()
$roster.AddStudent(3,'Chelsea')
$roster.AddStudent(7,'Logan')
$roster.GetRoster().Name | Should -Be ('Chelsea', 'Logan')
}
It "Roster returns an empty list if there are no students enrolled" {
$roster = [Roster]::new()
$roster.GetRoster() | Should -BeNullOrEmpty
}
It "Student names with grades are displayed in the same sorted roster" {
$roster = [Roster]::new()
$roster.AddStudent(2,'Peter')
$roster.AddStudent(1,'Anna')
$roster.AddStudent(1,'Barb')
$roster.AddStudent(2,'Zoe')
$roster.AddStudent(2,'Alex')
$roster.AddStudent(3,'Jim')
$roster.AddStudent(1,'Charlie')
$roster.GetRoster().Name | Should -Be ('Anna', 'Barb', 'Charlie', 'Alex', 'Peter', 'Zoe', 'Jim')
}
It "Grade returns the students in that grade in alphabetical order" {
$roster = [Roster]::new()
$roster.AddStudent(5,'Franklin')
$roster.AddStudent(5,'Bradley')
$roster.AddStudent(1,'Jeff')
$roster.GetRoster(5).Name | Should -Be ('Bradley', 'Franklin')
}
It "Grade returns an empty list if there are no students in that grade" {
$roster = [Roster]::new()
$roster.GetRoster(1) | Should -BeNullOrEmpty
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment