Created
November 9, 2022 11:21
-
-
Save TheBraveByte/22ed9dab935f25317b3074966ef84dd1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
) | |
// pricePerWeight for the carrot | |
const pricePerWeight = 3 | |
// pricePerUnit for cucumber | |
const pricePerUnit = 1 | |
const Discount = 10 | |
type Carrot struct { | |
//Weight in kg | |
Weight float64 | |
Price float64 | |
DiscountPrice float64 | |
} | |
type Cucumber struct { | |
//quantity of cucumber | |
Unit float64 | |
Price float64 | |
DiscountPrice float64 | |
} | |
type Basket []struct { | |
Carrot Carrot | |
Cucumber Cucumber | |
TotalDiscountedPrice float64 | |
} | |
func main() { | |
var basket = Basket{ | |
{Carrot: Carrot{Weight: 2}, Cucumber: Cucumber{Unit: 5}}, | |
{Carrot: Carrot{Weight: 3}, Cucumber: Cucumber{Unit: 6}}, | |
} | |
for _, r := range basket { | |
switch { | |
case r.Carrot.Weight == 2: | |
r.Carrot.Price = (r.Carrot.Weight - 1) * pricePerWeight | |
r.Cucumber.Price = (r.Cucumber.Unit - 2) * pricePerUnit | |
// To calculate the discount price | |
r.Carrot.DiscountPrice = ((r.Carrot.Weight - 1) * pricePerWeight) + r.Carrot.Price - (r.Carrot.Price * Discount / 100) | |
r.Cucumber.DiscountPrice = ((r.Cucumber.Unit - 3) * pricePerUnit) + r.Cucumber.Price - (r.Cucumber.Price * Discount / 100) | |
r.TotalDiscountedPrice = r.Carrot.DiscountPrice + r.Cucumber.DiscountPrice | |
fmt.Printf("Discount for products in first basket %v\n", r.TotalDiscountedPrice) | |
case r.Carrot.Weight > 2: | |
r.Carrot.Price = (r.Carrot.Weight - 1) * pricePerWeight | |
r.Cucumber.Price = r.Cucumber.Unit * pricePerUnit | |
// To calculate the discount price | |
r.Carrot.DiscountPrice = pricePerWeight + r.Carrot.Price - (r.Carrot.Price * Discount / 100) | |
r.Cucumber.DiscountPrice = r.Cucumber.Price - (r.Cucumber.Price * Discount / 100) | |
r.TotalDiscountedPrice = r.Carrot.DiscountPrice + r.Cucumber.DiscountPrice | |
fmt.Printf("Discount for products in second basket %v\n", r.TotalDiscountedPrice) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment