How to resolve the algorithm Nautical bell step by step in the PowerShell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Nautical bell step by step in the PowerShell programming language

Table of Contents

Problem Statement

Write a small program that emulates a nautical bell producing a ringing bell pattern at certain times throughout the day. The bell timing should be in accordance with Greenwich Mean Time, unless locale dictates otherwise. It is permissible for the program to daemonize, or to slave off a scheduler, and it is permissible to use alternative notification methods (such as producing a written notice "Two Bells Gone"), if these are more usual for the system type.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Nautical bell step by step in the PowerShell programming language

Source code in the powershell programming language

function Get-Bell
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,
                   Position=0)]
        [ValidateRange(1,12)]
        [int]
        $Hour,

        [Parameter(Mandatory=$true,
                   Position=1)]
        [ValidateSet(0,30)]
        [int]
        $Minute
    )

    $bells = @{
        OneBell    =          1
        TwoBells   =          2
        ThreeBells =       2, 1
        FourBells  =       2, 2
        FiveBells  =    2, 2, 1
        SixBells   =    2, 2, 2
        SevenBells = 2, 2, 2, 1
        EightBells = 2, 2, 2, 2
    }

    filter Invoke-Bell
    {
        if ($_ -eq 1)
        {
            [System.Media.SystemSounds]::Asterisk.Play()
            Write-Host -NoNewline "♪"
        }
        else
        {
            [System.Media.SystemSounds]::Exclamation.Play()
            Write-Host -NoNewline "♪♪  "
        }

        Start-Sleep -Milliseconds 500
    }


    $time = New-TimeSpan -Hours $Hour -Minutes $Minute

    switch ($time.Hours)
    {
         1 {if ($time.Minutes -eq 0) {$bells.TwoBells   | Invoke-Bell} else {$bells.ThreeBells | Invoke-Bell}; break}
         2 {if ($time.Minutes -eq 0) {$bells.FourBells  | Invoke-Bell} else {$bells.FiveBells  | Invoke-Bell}; break}
         3 {if ($time.Minutes -eq 0) {$bells.SixBells   | Invoke-Bell} else {$bells.SevenBells | Invoke-Bell}; break}
         4 {if ($time.Minutes -eq 0) {$bells.EightBells | Invoke-Bell} else {$bells.OneBell    | Invoke-Bell}; break}
         5 {if ($time.Minutes -eq 0) {$bells.TwoBells   | Invoke-Bell} else {$bells.ThreeBells | Invoke-Bell}; break}
         6 {if ($time.Minutes -eq 0) {$bells.FourBells  | Invoke-Bell} else {$bells.FiveBells  | Invoke-Bell}; break}
         7 {if ($time.Minutes -eq 0) {$bells.SixBells   | Invoke-Bell} else {$bells.SevenBells | Invoke-Bell}; break}
         8 {if ($time.Minutes -eq 0) {$bells.EightBells | Invoke-Bell} else {$bells.OneBell    | Invoke-Bell}; break}
         9 {if ($time.Minutes -eq 0) {$bells.TwoBells   | Invoke-Bell} else {$bells.ThreeBells | Invoke-Bell}; break}
        10 {if ($time.Minutes -eq 0) {$bells.FourBells  | Invoke-Bell} else {$bells.FiveBells  | Invoke-Bell}; break}
        11 {if ($time.Minutes -eq 0) {$bells.SixBells   | Invoke-Bell} else {$bells.SevenBells | Invoke-Bell}; break}
        12 {if ($time.Minutes -eq 0) {$bells.EightBells | Invoke-Bell} else {$bells.OneBell    | Invoke-Bell}}
    }

    Write-Host
}

Write-Host "Time Bells`n---- -----`n"

1..12 | ForEach-Object {

    $date = Get-Date -Hour $_ -Minute  0
    Write-Host -NoNewline "$($date.ToString("hh:mm")) "
    Get-Bell -Hour $_ -Minute  0

    $date = $date.AddMinutes(30)
    Write-Host -NoNewline "$($date.ToString("hh:mm")) "
    Get-Bell -Hour $_ -Minute 30
}


  

You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Lang programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Icon and Unicon programming language