-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
af43ee2
commit d6643e8
Showing
7 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,18 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type iPizzaFactory interface { | ||
createPizza() iPizza | ||
createGarlicBread() iGarlicBread | ||
} | ||
|
||
func getFactory(chain string) (iPizzaFactory, error) { | ||
if chain == "P" { | ||
return &pizzaHutFactory{}, nil | ||
} | ||
if chain == "D" { | ||
return &dominosFactory{}, nil | ||
} | ||
return nil, fmt.Errorf("Enter a valid chain type next time") | ||
} |
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,30 @@ | ||
package main | ||
|
||
type dominosFactory struct{} | ||
|
||
type dominosPizza struct { | ||
pizza | ||
} | ||
|
||
type dominosGarlicBread struct { | ||
garlicBread | ||
} | ||
|
||
func (d *dominosFactory) createPizza() iPizza { | ||
return &dominosPizza{ | ||
pizza{ | ||
name: "margherita", | ||
price: 200.5, | ||
toppings: []string{"tomatoes", "basil", "olive oil"}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *dominosFactory) createGarlicBread() iGarlicBread { | ||
return &dominosGarlicBread{ | ||
garlicBread{ | ||
name: "cheesy bread sticks", | ||
price: 150.00, | ||
}, | ||
} | ||
} |
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,19 @@ | ||
package main | ||
|
||
type iGarlicBread interface { | ||
GetPrice() float64 | ||
GetName() string | ||
} | ||
|
||
type garlicBread struct { | ||
name string | ||
price float64 | ||
} | ||
|
||
func (g *garlicBread) GetName() string { | ||
return g.name | ||
} | ||
|
||
func (g *garlicBread) GetPrice() float64 { | ||
return g.price | ||
} |
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,25 @@ | ||
package main | ||
|
||
type iPizza interface { | ||
GetPrice() float64 | ||
GetName() string | ||
GetToppings() []string | ||
} | ||
|
||
type pizza struct { | ||
name string | ||
price float64 | ||
toppings []string | ||
} | ||
|
||
func (p *pizza) GetName() string { | ||
return p.name | ||
} | ||
|
||
func (p *pizza) GetPrice() float64 { | ||
return p.price | ||
} | ||
|
||
func (p *pizza) GetToppings() []string { | ||
return p.toppings | ||
} |
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,31 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func describePizza(pizza iPizza) { | ||
fmt.Printf("the pizza %s has toppings %s. It costs Rs. %.2f\n", pizza.GetName(), strings.Join(pizza.GetToppings(), ", "), pizza.GetPrice()) | ||
} | ||
|
||
func describeGarlicBread(garlicBread iGarlicBread) { | ||
fmt.Printf("the garlic bread, %s costs Rs. %.2f\n", garlicBread.GetName(), garlicBread.GetPrice()) | ||
} | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
fmt.Println("Dominos or PizzaHut? (D/P)") | ||
pizzaType, _ := reader.ReadString('\n') | ||
pizzaType = strings.Split(pizzaType, "\n")[0] | ||
|
||
pizzaFactory, _ := getFactory(pizzaType) | ||
|
||
pizza := pizzaFactory.createPizza() | ||
garlicBread := pizzaFactory.createGarlicBread() | ||
|
||
describePizza(pizza) | ||
describeGarlicBread(garlicBread) | ||
} |
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,30 @@ | ||
package main | ||
|
||
type pizzaHutFactory struct{} | ||
|
||
type pizzaHutPizza struct { | ||
pizza | ||
} | ||
|
||
type pizzaHutGarlicBread struct { | ||
garlicBread | ||
} | ||
|
||
func (p *pizzaHutFactory) createPizza() iPizza { | ||
return &pizzaHutPizza{ | ||
pizza{ | ||
name: "pepperoni", | ||
price: 230.3, | ||
toppings: []string{"olives", "mozzarella", "pork"}, | ||
}, | ||
} | ||
} | ||
|
||
func (p *pizzaHutFactory) createGarlicBread() iGarlicBread { | ||
return &pizzaHutGarlicBread{ | ||
garlicBread{ | ||
name: "garlic bread", | ||
price: 180.99, | ||
}, | ||
} | ||
} |