Improve star-shapes-mk2

This commit is contained in:
raul 2024-04-18 10:34:39 +02:00
parent b8fe0a216b
commit 829a84fabb
1 changed files with 46 additions and 20 deletions

View File

@ -8,40 +8,66 @@ import (
) )
type Shaper interface { type Shaper interface {
Shape(int) Square GenerateCube(num int)
GenerateLine(num int)
GenerateFlag(num int)
} }
type Square struct { type Shape struct {
form string shape string
quantity int 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++ {
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) { func (s Shape) GenerateLine(num int) {
fmt.Println(s.Shape(num).form) for i := 0; i < num; i++ {
fmt.Printf("* ")
}
fmt.Printf("\n")
} }
func getNumberStars() int { func (s Shape) GenerateFlag(num int) {
numStr := os.Args[1] for a := 0; a < num; a++ {
num, err := strconv.Atoi(numStr) for i := -1; i < a; i++ {
if err != nil { fmt.Printf("* ")
log.Panicln(err) }
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() { func main() {
s := Square{} if len(os.Args) != 3 {
showShape(s, getNumberStars()) 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)
} }