From 810d704afb3735280b828a4d592a8105fa58b3ff Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Mon, 29 Jan 2024 15:57:49 -0500 Subject: [PATCH] finished chapter 9 --- playground/fuel.go | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/playground/fuel.go b/playground/fuel.go index b2d7840..d365b04 100644 --- a/playground/fuel.go +++ b/playground/fuel.go @@ -6,11 +6,44 @@ import "fmt" type Liters float64 type Gallons float64 +type Milliliters float64 + +func (l Liters) ToGallons() Gallons { + return Gallons(l * 0.264) +} + +func (m Milliliters) ToGallons() Gallons { + return Gallons(m * 0.000264) +} + +func (g Gallons) ToLiters() Liters { + return Liters(g * 3.785) +} + +func (g Gallons) ToMilliliters() Milliliters { + return Milliliters(g * 3785.41) +} func fuel() { - var carFuel Gallons - var busFuel Liters - carFuel = Gallons(10.0) - busFuel = Liters(240.0) - fmt.Println(carFuel, busFuel) + // long version + //var carFuel Gallons + //var busFuel Liters + //carFuel = Gallons(10.0) + //busFuel = Liters(240.0) + // short version + soda := Liters(2) + fmt.Printf("%0.0f liters equals %0.3f gallons\n", soda, soda.ToGallons()) + water := Milliliters(500) + fmt.Printf("%0.0f milliliters equals %0.3f gallons \n", water, water.ToGallons()) + //carFuel := Gallons(10.0) + //busFuel := Liters(240.0) + // + ////if you want to convert between types, save variable with correct calculation + //carFuel += ToGallons(Liters(40.0)) + //busFuel += ToLiters(Gallons(30.0)) + //fmt.Printf("Car Fuel: %0.1f gallons\n", carFuel) + //fmt.Printf("Bus Fuel: %0.1f liters\n", busFuel) + milk := Gallons(2) + fmt.Printf("%0.0f gallons equals %0.3f liters\n", milk, milk.ToLiters()) + fmt.Printf("%0.0f gallons equals %0.3f milliliters\n", milk, milk.ToMilliliters()) }