From 829a84fabbcb6ac43424ab0b7bd9fac66f6c61ae Mon Sep 17 00:00:00 2001 From: raul Date: Thu, 18 Apr 2024 10:34:39 +0200 Subject: [PATCH] Improve star-shapes-mk2 --- star-shapes-mk2/main.go | 66 ++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/star-shapes-mk2/main.go b/star-shapes-mk2/main.go index 2e3d016..7d49653 100644 --- a/star-shapes-mk2/main.go +++ b/star-shapes-mk2/main.go @@ -8,40 +8,66 @@ import ( ) type Shaper interface { - Shape(int) Square + GenerateCube(num int) + GenerateLine(num int) + GenerateFlag(num int) } -type Square struct { - form string - quantity int +type Shape struct { + shape string + numOfChars int } -func (s Square) Shape(num int) Square { +func (s Shape) GenerateCube(num int) { for i := 0; i < num; i++ { for i := 0; i < num; i++ { - s.form += "* " + fmt.Printf("* ") } - s.form += "\n" + fmt.Printf("\n") } - - s.quantity = num - return s } -func showShape(s Shaper, num int) { - fmt.Println(s.Shape(num).form) +func (s Shape) GenerateLine(num int) { + for i := 0; i < num; i++ { + fmt.Printf("* ") + } + fmt.Printf("\n") } -func getNumberStars() int { - numStr := os.Args[1] - num, err := strconv.Atoi(numStr) - if err != nil { - log.Panicln(err) +func (s Shape) GenerateFlag(num int) { + for a := 0; a < num; a++ { + for i := -1; i < a; i++ { + fmt.Printf("* ") + } + fmt.Printf("\n") + } + for i := 0; i < num/2; i++ { + fmt.Println("*") + } +} + +func passToShaper(s Shaper, num int, choice int) { + switch choice { + case 1: + s.GenerateLine(num) + case 2: + s.GenerateCube(num) + case 3: + s.GenerateFlag(num) } - return num } func main() { - s := Square{} - showShape(s, getNumberStars()) + if len(os.Args) != 3 { + fmt.Println("Use an argument like 5 and then 1") + os.Exit(0) + } + numToGen, err := strconv.Atoi(os.Args[1]) + if err != nil { + log.Fatalf("Error: %v\n", err) + } + numToChoose, err := strconv.Atoi(os.Args[2]) + + s := Shape{} + passToShaper(s, numToGen, numToChoose) }