-
-
Save dotchetter/990f76f5e02a62a119703bf09c73a96a to your computer and use it in GitHub Desktop.
PowerShell OOP example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Email | |
{ | |
hidden [string]$prefix | |
hidden [string]$suffix | |
hidden [string]$domain | |
hidden [string]$fullAddress | |
hidden [string]$reEmailDomainPattern = "^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$" | |
Email($prefix, $suffix, $domain) | |
{ | |
[string]$concatenated = ('{0}.{1}@{2}' -f $prefix, $suffix, $domain) | |
if ($this.EmailPatternIsValid($concatenated)) | |
{ | |
$this.fullAddress = $concatenated | |
return | |
} | |
Throw 'Error: Invalid email address received'# | |
} | |
hidden [bool] EmailPatternIsValid([string]$emailAddress) | |
{ | |
return $emailAddress -match $this.reEmailDomainPattern | |
} | |
[string] Get() | |
{ | |
return $this.fullAddress | |
} | |
} | |
class Employee | |
{ | |
hidden [string]$firstName | |
hidden [string]$lastName | |
hidden [string]$address | |
hidden [string]$reIsAlphaPattern = '^([A-Za-z]+)$' | |
hidden [string]$domain | |
hidden [datetime]$instantiationTimeStamp | |
Employee([string]$firstName, [string]$lastName, [string]$domain) | |
{ | |
$this.domain = $domain | |
$this.SetfirstName($firstName) | |
$this.SetLastName($lastName) | |
$this.instantiationTimeStamp = Get-Date | |
} | |
hidden [bool] validateInputAsAlpha([string]$arg) | |
{ | |
return $arg.Length -gt 0 -and $arg -match $this.reIsAlphaPattern | |
} | |
[void] SetFirstName([string]$Name) | |
{ | |
if ($this.validateInputAsAlpha($Name)) | |
{ | |
$this.firstName = $Name | |
return | |
} | |
Throw 'Error: Parameter $name deviates from pattern a-ö' | |
} | |
[string] GetFirstName() | |
{ | |
return $this.firstName | |
} | |
[void] SetLastName([string]$Name) | |
{ | |
if ($this.validateInputAsAlpha($Name)) | |
{ | |
$this.LastName = $Name | |
return | |
} | |
Throw 'Error: Parameter $name deviates from pattern a-ö' | |
} | |
[string] GetLastName() | |
{ | |
return $this.lastName | |
} | |
[string] GetFullName() | |
{ | |
return ('{0} {1}' -f $this.firstName, $this.lastName) | |
} | |
[void] SetAddress([string]$address) | |
{ | |
$this.address = $address | |
} | |
[string] GetAddress() | |
{ | |
return $this.address | |
} | |
[string] GetEmail() | |
{ | |
return [email]::new($this.firstName, $this.lastName, $this.domain).Get() | |
} | |
[datetime] GetRegistrationTimeStamp() | |
{ | |
return $this.instantiationTimeStamp | |
} | |
} | |
# Skapa ett objekt (en instans) av klassen Employee. Provar med en giltig email. | |
$employee_1 = [Employee]::new('Simon', 'Olofsson', 'nackademin.se') | |
# Kommentera ut detta för att se Email objektets felhantering fungera!! =) | |
# //// $employee_1.setDomain('n$ackademin@_.s@e') # Ger ett automatiskt fel | |
# Skriv ut HELA namnet med hjälp av Get-metod | |
Write-Host $employee_1.GetFullName() | |
# Skriv ut en automatiskt dynamiskt genererad email sträng... byter man namn med ex SetFirstName() | |
# så ändras detta iom att Email objektet tar nya värden, och sköter kontrolleringen åt oss. | |
Write-Host $employee_1.GetEmail() | |
# Så.. byter man namn på den anställde och provar emailen nu.. | |
Write-Host "--- Byter namn ---" | |
$employee_1.SetFirstName('Yngve') | |
Write-Host $employee_1.GetEmail() | |
# Skapa 3 Employee's och spara i en ArrayList | |
# (Så här kan du skapa instanser utan att hårdkoda dem som ovan på rad 89) | |
Write-Host `n'Nu dynamiskt...' `n -f Green | |
[System.Collections.ArrayList]$employeeBase = @() | |
for ($i = 0; $i -lt 3; $i++) | |
{ | |
Write-Host ('{0} {1}' -f 'Employee', ($i + 1)) | |
$fname = Read-Host 'Enter first name for employee' | |
$lname = Read-Host 'Enter last name for employee' | |
$domain = Read-Host 'Enter email domain for employee' | |
$employeeBase.Add([Employee]::new($fname, $lname, $domain)) | |
} | |
# Skriv ut informationen för varje "anställd". | |
foreach ($_ in $employeeBase) | |
{ | |
Write-Host 'Name:' $_.GetFullName() | |
Write-Host 'Email:' $_.GetEmail() | |
Write-Host 'Registered:' $_.GetRegistrationTimeStamp() `n`n | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment