Create Interpreter.go

This commit is contained in:
Edward 2020-05-11 21:16:36 +08:00 committed by GitHub
parent d4d86b1c2c
commit 11eb3eb608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View 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
}