I over engineered this but I felt like having a bit of fun with this one so lets have a Go shall we!?
coffee.go
packagecoffeeimport"fmt"// Product represents a type of product available at the coffee shoptypeProductintconst(AmericanoProduct=iotaLatteFlatWhiteFilter)const(// AmericanoPrice is the price of the product AmericanoAmericanoPricefloat64=2.20// LattePrice is the price of the product LatteLattePricefloat64=2.30// FlatWhitePrice is the price of the product FlatWhiteFlatWhitePricefloat64=2.40// FilterPrice is the price of the product FilterFilterPricefloat64=3.50)const(exactChangestring="Here is your %s, have a nice day!"notExactstring="Sorry, exact change only, try again tomorrow!")// String gives the text representation of the productfunc(pProduct)String()string{switchp{caseAmericano:return"Americano"caseLatte:return"Latte"caseFlatWhite:return"Flat White"caseFilter:return"Filter"default:return""}}// Price returns of the price of the productfunc(pProduct)Price()float64{switchp{caseAmericano:returnAmericanoPricecaseLatte:returnLattePricecaseFlatWhite:returnFlatWhitePricecaseFilter:returnFilterPricedefault:returnfloat64(0)}}// Order represents an order submitted to the coffee shoptypeOrderstruct{Items[]Product// Items is the collection of products being orderedTenderfloat64// Tender is the amount of money given for the order}// Total calculates the total value of the order from all the products in itfunc(oOrder)Total()float64{vartotalfloat64for_,item:=rangeo.Items{total+=item.Price()}returntotal}func(oOrder)String()string{count:=len(o.Items)switchcount{case0:return""case1:returno.Items[0].String()case2:returnfmt.Sprintf("%s and %s",o.Items[0].String(),o.Items[1].String())default:varstrstringfori,item:=rangeo.Items{ifi+1==count{str+="and "}str+=item.String()ifi+1!=count{str+=", "}}returnstr}}// Purchase responds to an order submitted at the coffee shopfuncPurchase(orderOrder)string{iforder.Total()==order.Tender{returnfmt.Sprintf(exactChange,order)}returnnotExact}
coffee_test.go
packagecoffeeimport"testing"funcTestPurchase(t*testing.T){testCases:=[]struct{descriptionstringinputOrderexpectedstring}{{"buy an Americano with exact change",Order{[]Product{Americano,},AmericanoPrice,},"Here is your Americano, have a nice day!",},{"buy an Americano without exact change",Order{[]Product{Americano,},2.30,},notExact,},{"buy a Latte with exact change",Order{[]Product{Latte,},LattePrice,},"Here is your Latte, have a nice day!",},{"buy a Latte without exact change",Order{[]Product{Latte,},5.00,},notExact,},{"buy a Flat White with exact change",Order{[]Product{FlatWhite,},FlatWhitePrice,},"Here is your Flat White, have a nice day!",},{"buy a Flat White without exact change",Order{[]Product{FlatWhite,},10.00,},notExact,},{"buy a Filter with exact change",Order{[]Product{Filter,},FilterPrice,},"Here is your Filter, have a nice day!",},{"buy a Filter without exact change",Order{[]Product{Filter,},4.00,},notExact,},{"buy two drinks with exact change",Order{[]Product{Americano,FlatWhite,},AmericanoPrice+FlatWhitePrice,},"Here is your Americano and Flat White, have a nice day!",},{"buy two drinks without exact change",Order{[]Product{Filter,Latte,},10.00,},notExact,},{"buy many drinks with exact change",Order{[]Product{Americano,Americano,FlatWhite,Latte,Filter,},(AmericanoPrice*2)+FlatWhitePrice+LattePrice+FilterPrice,},"Here is your Americano, Americano, Flat White, Latte, and Filter, have a nice day!",},{"buy many drinks without exact change",Order{[]Product{Americano,Americano,FlatWhite,Latte,Filter,},30.00,},notExact,},}for_,test:=rangetestCases{ifresult:=Purchase(test.input);result!=test.expected{t.Fatalf("FAIL: %s - Purchase(%+v): '%s' - expected '%s'",test.description,test.input,result,test.expected)}t.Logf("PASS: %s",test.description)}}
I used this challenge as my "hello world" with Go. At first I tried a map thinking that it might be easy to have float64 type as key. Can you please post a solution using a map? Here's mine:
packagemainimport"fmt"funcbuy(amountstring)string{types:=map[string]string{"2.2":"Americano","2.3":"Latte","2.4":"Flat white","3.5":"Filter",}returnfmt.Sprintf("Here is your %s have a nice day!",types[amount])}funcmain(){fmt.Println(buy("2.2"))fmt.Println(buy("3.5"))}
That would also enable you to pass in a float as an argument to the func instead of a string as well.
Don't forget to include logic for checking if they have exact change!
If you would still like me to post a solution using a map I would be more than happy to.
Note
In Golang, if you attempt to access a value of a map with a key that doesn't exist, you will get the zero value of the type stored in the map. That may be useful for the exact change logic check in your solution.
If we use your map for example:
types[5.0]
would return "", since the key 5.0 doesn't exist and "" is the zero value for a string in Golang.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I over engineered this but I felt like having a bit of fun with this one so lets have a Go shall we!?
coffee.go
coffee_test.go
Tweaked to allow purchasing multiple drinks, inspired by Ben Halpern's solution.
I used this challenge as my "hello world" with Go. At first I tried a map thinking that it might be easy to have float64 type as key. Can you please post a solution using a map? Here's mine:
Hello Cherny!
You can most certainly use floats as keys in a map, you just need to indicate what type the keys are in the map declaration.
For your code above you have:
If you want to use floats as the keys, you simply need to indicate as so:
Then you could so something like this:
That would also enable you to pass in a float as an argument to the func instead of a string as well.
Don't forget to include logic for checking if they have exact change!
If you would still like me to post a solution using a map I would be more than happy to.
Note
In Golang, if you attempt to access a value of a map with a key that doesn't exist, you will get the zero value of the type stored in the map. That may be useful for the exact change logic check in your solution.
If we use your map for example:
would return "", since the key 5.0 doesn't exist and "" is the zero value for a string in Golang.