Files
bootdotdev-fcc-learn-golang…/course/6-errors/exercises/1-errors/complete.go
wagslane 9be3074de6 first
2023-05-01 15:25:27 -06:00

61 lines
1.2 KiB
Go

package main
import (
"fmt"
)
func sendSMSToCouple(msgToCustomer, msgToSpouse string) (float64, error) {
cost, err := sendSMS(msgToCustomer)
if err != nil {
return 0.0, err
}
costSpouse, err := sendSMS(msgToSpouse)
if err != nil {
return 0, err
}
return costSpouse + cost, nil
}
// don't edit below this line
func sendSMS(message string) (float64, error) {
const maxTextLen = 25
const costPerChar = .0002
if len(message) > maxTextLen {
return 0.0, fmt.Errorf("can't send texts over %v characters", maxTextLen)
}
return costPerChar * float64(len(message)), nil
}
func test(msgToCustomer, msgToSpouse string) {
defer fmt.Println("========")
fmt.Println("Message for customer:", msgToCustomer)
fmt.Println("Message for spouse:", msgToSpouse)
totalCost, err := sendSMSToCouple(msgToCustomer, msgToSpouse)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Total cost: $%.4f\n", totalCost)
}
func main() {
test(
"Thanks for coming in to our flower shop today!",
"We hope you enjoyed your gift.",
)
test(
"Thanks for joining us!",
"Have a good day.",
)
test(
"Thank you.",
"Enjoy!",
)
test(
"We loved having you in!",
"We hope the rest of your evening is absolutely fantastic.",
)
}