mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 12:46:03 +03:00
Create Interpreter.go
This commit is contained in:
parent
d4d86b1c2c
commit
11eb3eb608
62
behavioral/interpreter/Interpreter.go
Normal file
62
behavioral/interpreter/Interpreter.go
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* @author : Jagepard <jagepard@yandex.ru>
|
||||
* @license https://mit-license.org/ MIT
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Interpreter is ...
|
||||
type Interpreter struct {
|
||||
registry map[int]Album
|
||||
}
|
||||
|
||||
var i int = 1
|
||||
|
||||
func (interpreter *Interpreter) addAlbumToRegistry(album Album) {
|
||||
interpreter.registry[i] = album
|
||||
i++
|
||||
}
|
||||
|
||||
func (interpreter *Interpreter) interpret(input string) {
|
||||
var exploded = strings.Split(input, " ")
|
||||
|
||||
for i := range exploded {
|
||||
if interpreter.isNumeric(exploded[i]) {
|
||||
number, err := strconv.Atoi(exploded[i])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
interpreter.getDataFromRegistry(exploded, interpreter.registry[number])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (interpreter *Interpreter) getDataFromRegistry(exploded []string, album Album) {
|
||||
var output strings.Builder
|
||||
|
||||
for i := range exploded {
|
||||
if exploded[i] == "author" {
|
||||
output.WriteString(album.author)
|
||||
output.WriteString(" ")
|
||||
}
|
||||
|
||||
if exploded[i] == "album" {
|
||||
output.WriteString(album.name)
|
||||
output.WriteString(" ")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(output.String())
|
||||
}
|
||||
|
||||
func (interpreter *Interpreter) isNumeric(s string) bool {
|
||||
_, err := strconv.ParseFloat(s, 64)
|
||||
return err == nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user