@@ -10,6 +10,16 @@ Class Calculator {
1010 [int ]$RightOperand ,
1111 [string ]$Operation
1212 ) {
13+ # # Add input parameter checks
14+ if (-not [int ]::TryParse($LeftOperand , [ref ]$null )) {
15+ throw " LeftOperand must be an integer."
16+ }
17+ if (-not [int ]::TryParse($RightOperand , [ref ]$null )) {
18+ throw " RightOperand must be an integer."
19+ }
20+ if ($Operation -notin @ (" Add" , " Subtract" , " Multiply" , " Divide" , " Average" , " Remainder" , " Power" , " Percentage" , " Modulus" )) {
21+ throw " Operation must be a valid option."
22+ }
1323 # # Initializing the Calculator object with the input values passed to the constructor
1424 $this.LeftOperand = $LeftOperand
1525 $this.RightOperand = $RightOperand
@@ -38,6 +48,10 @@ Class Calculator {
3848 [int ] Average() {
3949 return ($this.LeftOperand + $this.RightOperand ) / 2
4050 }
51+ # # Method to calculate remainder
52+ [int ] Remainder() {
53+ return $this.LeftOperand % $this.RightOperand
54+ }
4155 # # Method to perform power calculation
4256 [int ] Power() {
4357 return [Math ]::Pow($this.LeftOperand , $this.RightOperand )
@@ -47,7 +61,14 @@ Class Calculator {
4761 if ($this.RightOperand -eq 0 ) {
4862 throw " Dividing by zero is impossible!"
4963 }
50- return ($this.LeftOperand / $this.RightOperand ) * 100
64+ return [Math ]::Round(($this.LeftOperand * $this.Percentage ) / 100 )
65+ }
66+ # # Method to perform modulus calculation
67+ [int ] Modulus() {
68+ if ($this.RightOperand -eq 0 ) {
69+ throw " Dividing by zero is impossible!"
70+ }
71+ return $this.LeftOperand % $this.RightOperand
5172 }
5273}
5374
@@ -57,9 +78,8 @@ Function SimpleCalc {
5778 Simple calculator that shows how to use class.
5879
5980 . DESCRIPTION
60- Script block takes in numerical input via the LeftOperand and RightOperand parameters.
61- Operation parameter determines which math operation to perform on the LeftOperand and RightOperand parameters.
62-
81+ Numerical input via the LeftOperand and RightOperand parameters with Operation parameter determines which math operation.
82+
6383 . PARAMETER Operation
6484 Mandatory - specifies the mathematical operation to perform.
6585 . PARAMETER LeftOperand
@@ -68,41 +88,48 @@ Function SimpleCalc {
6888 NotMandatory - value on the right side of the math operation.
6989 . PARAMETER Percentage
7090 NotMandatory - specifies whether to calculate percentage. If specified, the value of RightOperand should be considered as percentage of LeftOperand.
71-
91+
7292 . EXAMPLE
7393 SimpleCalc -Operation Add -LeftOperand 4 -RightOperand 4
7494 SimpleCalc -Operation Subtract -LeftOperand 64 -RightOperand 48
7595 SimpleCalc -Operation Multiply -LeftOperand 4 -RightOperand 8
96+ SimpleCalc -Operation Remainder -LeftOperand 10 -RightOperand 3
7697 SimpleCalc -Operation Divide -LeftOperand 512 -RightOperand 8
7798 SimpleCalc -Operation Average -LeftOperand 56 -RightOperand 200
7899 SimpleCalc -Operation Power -LeftOperand 8 -RightOperand 3
79- SimpleCalc -Operation Percentage -LeftOperand 264 -Percentage 97
80-
100+ SimpleCalc -Operation Percentage -LeftOperand 1600 -Percentage 64
101+ SimpleCalc -Operation Modulus -LeftOperand 20048 -RightOperand 3000
102+
81103 . NOTES
82- v0.6.3
104+ v0.6.5
83105 #>
84106 [CmdletBinding ()]
85107 param (
86108 [Parameter (Mandatory = $true , Position = 0 )]
87- [ValidateSet (" Add" , " Subtract" , " Multiply" , " Divide" , " Average" , " Percentage " , " Power" )]
109+ [ValidateSet (" Add" , " Subtract" , " Multiply" , " Divide" , " Average" , " Remainder " , " Power" , " Percentage " , " Modulus " )]
88110 [string ]$Operation ,
89111
90112 [Parameter (Mandatory = $true , Position = 1 )]
113+ [ValidateScript ({ [int ]::TryParse($_ , [ref ]$null ) })]
91114 [int ]$LeftOperand ,
92115
93116 [Parameter (Mandatory = $false , Position = 2 )]
117+ [ValidateScript ({ [int ]::TryParse($_ , [ref ]$null ) })]
94118 [int ]$RightOperand ,
95119
96120 [Parameter (Position = 3 )]
121+ [ValidateRange (0 , 100 )]
97122 [int ]$Percentage
98123 )
99124 BEGIN {
100- Write-Verbose - Message $Operation
101- # # Here we can now initialize a new calculator object based on our class
102- $SimpleCalc = [Calculator ]::new($LeftOperand , $RightOperand , $Operation )
125+ try {
126+ $SimpleCalc = [Calculator ]::new($LeftOperand , $RightOperand , $Operation )
127+ }
128+ catch {
129+ throw " Error: $_ "
130+ }
103131 }
104132 PROCESS {
105- # # Now, we will assign a switch statemant block to a variable called $Res
106133 $Res = switch ($Operation ) {
107134 " Add" {
108135 $SimpleCalc.Add ()
@@ -119,20 +146,26 @@ Function SimpleCalc {
119146 " Average" {
120147 $SimpleCalc.Average ()
121148 }
149+ " Remainder" {
150+ $SimpleCalc.Remainder ()
151+ }
122152 " Power" {
123153 $SimpleCalc.Power ()
124154 }
125155 " Percentage" {
126156 [Math ]::Round(($LeftOperand * $Percentage ) / 100 )
127157 }
158+ " Modulus" {
159+ $LeftOperand % $RightOperand
160+ }
128161 }
129162 }
130163 END {
131164 if ($Operation -ne " Percentage" ) {
132- Write-Output - InputObject " Result: $Res "
165+ Write-Output - InputObject " Result: $Res "
133166 }
134167 else {
135168 Write-Output " Result: $Percentage % of $LeftOperand is $Res "
136169 }
137170 }
138- }
171+ }
0 commit comments