-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflashcards.go
369 lines (299 loc) · 7.22 KB
/
flashcards.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"math/rand"
"os"
"strconv"
"strings"
)
type (
flashcards struct {
reader *bufio.Reader
TermDef map[string]string `json:"termToDef"`
defTerm map[string]string
err map[string]int
hiCounter int
log []string
}
)
func main() {
c := initCardsStruct()
expPath := c.handleFlags()
c.menu(expPath)
}
func initCardsStruct() *flashcards {
return &flashcards{
reader: bufio.NewReader(os.Stdin),
TermDef: make(map[string]string),
defTerm: make(map[string]string),
err: make(map[string]int),
}
}
func (c *flashcards) handleFlags() string {
var imp, exp string
flag.StringVar(&imp, "import_from", "", "")
flag.StringVar(&exp, "export_to", "", "")
flag.Parse()
if imp != "" {
c.importCards(imp)
}
return exp
}
func (c *flashcards) menu(expPath string) {
for {
msg := "Input the action (add, remove, import, export, ask, exit, log, hardest card, reset stats):"
c.writeLogEntry(msg, true)
line, _ := c.reader.ReadString('\n')
line = strings.ToLower(strings.TrimSpace(line))
c.writeLogEntry(line, false)
switch line {
case "add":
c.addCard()
case "remove":
c.removeCard()
case "import":
c.importCards("")
case "export":
c.exportCards("")
case "ask":
c.ask()
case "log":
c.saveLog()
case "hardest card":
c.printHardestCards()
case "reset stats":
c.resetStats()
case "exit":
msg = "Bye bye!"
if expPath != "" {
c.exportCards(expPath)
}
c.writeLogEntry(msg, true)
return
default:
}
}
}
func (c *flashcards) addCard() {
msg := "Input the number of flashcards:"
c.writeLogEntry(msg, true)
term := c.getTerm()
def := c.getDefinition()
c.TermDef[term] = def
c.defTerm[def] = term
msg = fmt.Sprintf("The pair (\"%s\":\"%s\") has been added.", term, def)
c.writeLogEntry(msg, true)
}
func (c *flashcards) getTerm() string {
msg := "The card:"
c.writeLogEntry(msg, true)
for {
line, _ := c.reader.ReadString('\n')
line = strings.TrimSpace(line)
c.writeLogEntry(line, false)
if _, ok := c.TermDef[line]; !ok {
return line
} else {
msg = fmt.Sprintf("The term \"%s\" already exists. Try again:", line)
c.writeLogEntry(msg, true)
}
}
}
func (c *flashcards) getDefinition() string {
msg := "The definition of the card:"
c.writeLogEntry(msg, true)
for {
line, _ := c.reader.ReadString('\n')
line = strings.TrimSpace(line)
c.writeLogEntry(line, false)
if _, ok := c.defTerm[line]; !ok {
return line
} else {
msg = fmt.Sprintf("The definition \"%s\" already exists. Try again:", line)
c.writeLogEntry(msg, true)
}
}
}
func (c *flashcards) removeCard() {
msg := "Which card?"
c.writeLogEntry(msg, true)
line, _ := c.reader.ReadString('\n')
line = strings.TrimSpace(line)
c.writeLogEntry(line, false)
def, ok := c.TermDef[line]
if ok {
delete(c.TermDef, line)
delete(c.defTerm, def)
msg = "The card has been removed."
c.writeLogEntry(msg, true)
} else {
msg = fmt.Sprintf("Can't remove \"%s\": there is no such card.", line)
c.writeLogEntry(msg, true)
}
}
func (c *flashcards) importCards(path string) {
var line, msg string
if path != "" {
line = path
} else {
msg = "File name:"
c.writeLogEntry(msg, true)
line, _ = c.reader.ReadString('\n')
line = strings.TrimSpace(line)
c.writeLogEntry(line, false)
}
file, err := os.Open(line)
if err != nil {
msg = "File not found."
c.writeLogEntry(msg, true)
return
}
defer file.Close()
byteValue, _ := io.ReadAll(file)
fileCards := make(map[string]string)
err = json.Unmarshal(byteValue, &fileCards)
if err != nil {
msg = fmt.Sprintf("Error at Unmarshal: %s", err.Error())
c.writeLogEntry(msg, true)
return
}
for term, def := range fileCards {
c.TermDef[term] = def
c.defTerm[def] = term
}
msg = fmt.Sprintf("%d flashcards have been loaded.", len(fileCards))
c.writeLogEntry(msg, true)
}
func (c *flashcards) exportCards(expPath string) {
var line, msg string
if expPath != "" {
line = expPath
} else {
msg = "File name:"
c.writeLogEntry(msg, true)
line, _ = c.reader.ReadString('\n')
line = strings.TrimSpace(line)
c.writeLogEntry(line, false)
}
cardsJSON, err := json.MarshalIndent(c.TermDef, "", " ")
if err != nil {
msg = fmt.Sprintf("Error at MarshalIndent: %s", err.Error())
c.writeLogEntry(msg, true)
return
}
err = os.WriteFile(line, cardsJSON, 0644)
if err != nil {
msg = fmt.Sprintf("Error at WriteFile: %s", err.Error())
c.writeLogEntry(msg, true)
return
}
msg = fmt.Sprintf("%d flashcards have been saved.", len(c.TermDef))
c.writeLogEntry(msg, true)
}
func (c *flashcards) ask() {
msg := "How many times to ask?"
c.writeLogEntry(msg, true)
line, _ := c.reader.ReadString('\n')
line = strings.TrimSpace(line)
c.writeLogEntry(line, false)
num, _ := strconv.Atoi(line)
for i := 0; i < num; i++ {
term, def := pickRandomEntry(c.TermDef)
userDef := c.getAnswer(term)
if userDef == def {
msg = "Correct!"
c.writeLogEntry(msg, true)
} else {
c.err[term] += 1
if c.err[term] > c.hiCounter {
c.hiCounter = c.err[term]
}
cDef, ok := c.defTerm[userDef]
if ok {
msg = fmt.Sprintf("Wrong. The right answer is \"%s\", but your definition is correct for \"%s\".", def, cDef)
c.writeLogEntry(msg, true)
} else {
msg = fmt.Sprintf("Wrong. The right answer is \"%s\".", def)
c.writeLogEntry(msg, true)
}
}
}
}
func pickRandomEntry(m map[string]string) (string, string) {
i := rand.Intn(len(m))
for term, def := range m {
if i == 0 {
return term, def
}
i--
}
return "", ""
}
func (c *flashcards) getAnswer(term string) string {
msg := fmt.Sprintf("Print the definition of \"%s\":", term)
c.writeLogEntry(msg, true)
line, _ := c.reader.ReadString('\n')
line = strings.TrimSpace(line)
c.writeLogEntry(line, false)
return line
}
func (c *flashcards) writeLogEntry(str string, doPrint bool) {
if doPrint {
fmt.Println(str)
}
c.log = append(c.log, str)
}
func (c *flashcards) saveLog() {
msg := "File name:"
c.writeLogEntry(msg, true)
var log string
for _, entry := range c.log {
log += entry + "\n"
}
line, _ := c.reader.ReadString('\n')
line = strings.TrimSpace(line)
c.writeLogEntry(line, false)
err := os.WriteFile(line, []byte(log), 0644)
if err != nil {
msg = fmt.Sprintf("Error at WriteFile: %s", err.Error())
c.writeLogEntry(msg, true)
return
}
msg = "The log has been saved."
c.writeLogEntry(msg, true)
}
func (c *flashcards) getHardestCards() map[int][]string {
cardsMap := make(map[int][]string)
for term, counter := range c.err {
cardsMap[counter] = append(cardsMap[counter], term)
}
return cardsMap
}
func (c *flashcards) printHardestCards() {
if len(c.err) == 0 {
msg := "There are no flashcards with err."
c.writeLogEntry(msg, true)
} else {
cardsMap := c.getHardestCards()
msg := fmt.Sprintf("The hardest card %s \"%s\". You have %d err answering it.",
func() string {
if len(cardsMap[c.hiCounter]) > 1 {
return "are"
} else {
return "is"
}
}(),
strings.Join(cardsMap[c.hiCounter], ", "), c.hiCounter)
c.writeLogEntry(msg, true)
}
}
func (c *flashcards) resetStats() {
c.err = make(map[string]int)
c.hiCounter = 0
msg := "Card statistics have been reset."
c.writeLogEntry(msg, true)
}