-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bd5089
commit dd03646
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package opc | ||
|
||
import "fmt" | ||
|
||
const discountRate float64 = 0.9 | ||
|
||
func CalculateOrderPrice( | ||
itemPrice float64, | ||
quantity int, | ||
taxRate float64, | ||
discount bool, | ||
) string { | ||
totalPrice := itemPrice * float64(quantity) | ||
|
||
if discount { | ||
totalPrice = totalPrice * discountRate | ||
} | ||
|
||
taxAmount := totalPrice * taxRate | ||
finalPrice := totalPrice + taxAmount | ||
|
||
return fmt.Sprintf("The final price is: €%.2f", finalPrice) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package opc_test | ||
|
||
import ( | ||
"go-sandbox/opc" | ||
"testing" | ||
"fmt" | ||
) | ||
|
||
func TestCalculateOrderPrice(t *testing.T) { | ||
type testCase struct { | ||
itemPrice, taxRate float64 | ||
quantity int | ||
discount bool | ||
want string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
itemPrice: 2.99, | ||
taxRate: 0.07, | ||
quantity: 3, | ||
discount: true, | ||
want: "The final price is: €8.64", | ||
}, | ||
{ | ||
itemPrice: 2.99, | ||
taxRate: 0.07, | ||
quantity: 3, | ||
discount: false, | ||
want: "The final price is: €9.60", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tcName := fmt.Sprintf( | ||
"itemPrice = %.2f, quantity = %d, taxRate = %.2f, discount = %t", | ||
tc.itemPrice, | ||
tc.quantity, | ||
tc.taxRate, | ||
tc.discount, | ||
) | ||
|
||
t.Run(tcName, func (t *testing.T) { | ||
got := opc.CalculateOrderPrice( | ||
tc.itemPrice, | ||
tc.quantity, | ||
tc.taxRate, | ||
tc.discount, | ||
) | ||
|
||
if tc.want != got { | ||
t.Errorf("Failed! Wanted %q, got %q", tc.want, got) | ||
} | ||
}) | ||
} | ||
} |