-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortidIntListRepl.go
143 lines (134 loc) · 3.15 KB
/
SortidIntListRepl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package repl
import (
"fmt"
"github.com/comdiv/golang_course_comdiv/internal/sortedintlist"
"github.com/comdiv/golang_course_comdiv/internal/sortedintlist/slices"
"io"
"strconv"
"strings"
)
type SortedIntListRepl struct {
in io.Reader
out io.Writer
list sortedintlist.IIntListMutable
}
func New(in io.Reader, out io.Writer) *SortedIntListRepl {
return NewCustom(in, out, slices.New())
}
func NewCustom(in io.Reader, out io.Writer, list sortedintlist.IIntListMutable) *SortedIntListRepl {
return &SortedIntListRepl{
list: list,
in: in,
out: out,
}
}
func (r *SortedIntListRepl) asSet() sortedintlist.IIntSet {
return r.list.(sortedintlist.IIntSet)
}
func (r *SortedIntListRepl) asMinMax() sortedintlist.IIntMinMax {
return r.list.(sortedintlist.IIntMinMax)
}
func (r *SortedIntListRepl) PrintHelp() {
fmt.Println("Sorted list holder application")
fmt.Println("Commands:")
fmt.Println("any positive int ( 10 ) - add it to list")
fmt.Println("any negative int ( -10) - remove it counterpart from list (single)")
fmt.Println("double minus negative int ( --10) - remove it counterpart from list (all)")
fmt.Println("size - prints list size (unique value count)")
fmt.Println("count - prints list size (all value count)")
fmt.Println("all - prints all values (with duplicates)")
fmt.Println("unique - prints only unique values")
fmt.Println("min - prints min value")
fmt.Println("max - prints max value")
}
func (r *SortedIntListRepl) Execute() {
if nil == r.in {
return
}
var cmd string
for {
fmt.Fscan(r.in, &cmd)
exit := false
switch cmd {
case "exit":
exit = true
case "help":
r.PrintHelp()
default:
err := r.ExecuteCommand(cmd)
if err != nil {
fmt.Printf("Error in repl: %v\n", err)
break
}
}
if exit {
break
}
}
}
func (r *SortedIntListRepl) ExecuteCommand(cmd string) error {
print := func(format string, a ...interface{}) {
if nil != r.out {
fmt.Fprintf(r.out, format, a...)
}
}
switch cmd {
case "all":
print("%v\n", r.list.GetAll())
case "unique":
if nil == r.asSet() {
print("Is not a set - `unique` - not supported\n")
break
}
print("%v\n", r.asSet().GetUnique())
case "count":
print("%v\n", r.list.Size())
case "size":
if nil == r.asSet() {
print("Is not a set - `size` - not supported\n")
break
}
print("%v\n", r.asSet().UniqueSize())
case "min":
if nil == r.asMinMax() {
print("Is not a IIntMinMax - `min` - not supported\n")
break
}
min, err := r.asMinMax().GetMin()
if nil != err {
print("error: %v\n", err)
break
}
print("%v\n", min)
case "max":
if nil == r.asMinMax() {
print("Is not a IIntMinMax - `max` - not supported\n")
break
}
max, err := r.asMinMax().GetMax()
if nil != err {
print("error: %v\n", err)
break
}
print("%v\n", max)
default:
removeAll := false
numberPart := cmd
if strings.HasPrefix(cmd, "--") {
removeAll = true
numberPart = cmd[1:]
}
ival, err := strconv.Atoi(numberPart)
if err != nil {
print("Error command: %v (%v)\n", cmd, err)
return err
} else {
if ival >= 0 {
r.list.Insert(ival)
} else {
r.list.Delete(-ival, removeAll)
}
}
}
return nil
}