-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tempeste National Library.go
301 lines (257 loc) · 7.09 KB
/
Tempeste National Library.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"time"
)
type Book struct {
Name string `json:"name"`
Author string `json:"author"`
ID int `json:"ID"`
State []interface{} `json:"state"`
}
const (
filePath = "./LibraryIndex.json"
借書天數 = 7
//預定使用者名稱 = "訪客"
//預定使用者密碼 = "abcd1234"
)
func loadBooks() ([]Book, error) {
fileData, err := ioutil.ReadFile(filePath)
var books []Book
if err != nil {
return nil, fmt.Errorf("讀取文件錯誤: %v", err)
}
if err := json.Unmarshal(fileData, &books); err != nil {
return nil, fmt.Errorf("解析JSON錯誤: %v", err)
}
return books, nil
}
func saveBooks(books []Book) error {
newData, err := json.MarshalIndent(books, "", " ")
if err != nil {
return fmt.Errorf("生成JSON錯誤: %v", err)
}
if err := ioutil.WriteFile(filePath, newData, 0644); err != nil {
return fmt.Errorf("寫入文件錯誤: %v", err)
}
return nil
}
func getStateString(state []interface{}, index int) string {
if index >= len(state) {
return ""
}
switch v := state[index].(type) {
case string:
return v
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
default:
return fmt.Sprintf("%v", v)
}
}
func ANewBook() {
var newBook Book
books, err := loadBooks()
if err != nil {
fmt.Println(err)
return
}
fmt.Print("請輸入新書的書名:")
fmt.Scanln(&newBook.Name)
fmt.Print("請輸入作者名:")
fmt.Scanln(&newBook.Author)
fmt.Print("請輸入書本的ID或索引碼:")
fmt.Scanln(&newBook.ID)
fmt.Println("\n")
newBook.State = []interface{}{"未借出", "無", "", ""}
books = append(books, newBook)
if err := saveBooks(books); err != nil {
fmt.Println(err)
return
}
fmt.Println("登記成功!")
}
func LossingBook() {
var TheLossingBook string
var CheckLB string
var BookID int
books, err := loadBooks()
if err != nil {
fmt.Println(err)
return
}
fmt.Print("請輸入丟失的書名:")
fmt.Scanln(&TheLossingBook)
fmt.Println("\n")
for i, book := range books {
if book.Name == TheLossingBook {
fmt.Printf("請確認丟失的書籍:\n書名:%s,作者:%s,書籍ID:%d,借還狀態:%s,借閱人:%s\na.正確 b.重來:",
book.Name, book.Author, book.ID, getStateString(book.State, 0), getStateString(book.State, 1))
fmt.Scanln(&CheckLB)
fmt.Print("為避免同名書,請確認書本ID:")
fmt.Scanln(&BookID)
fmt.Println("\n")
if CheckLB == "a" && book.ID == BookID {
books = append(books[:i], books[i+1:]...)
if err := saveBooks(books); err != nil {
fmt.Println(err)
return
}
fmt.Println("成功註銷該書!")
return
}
fmt.Println("已取消操作")
return
}
}
fmt.Println("書籍已不存在,或檢查是不是打錯字了喔!")
}
func Borrow() {
var WantFind string
var WantBorrow string
var BookID int
var BorrowerName string
books, err := loadBooks()
if err != nil {
fmt.Println(err)
return
}
fmt.Print("請問你想借甚麼書呢?")
fmt.Scanln(&WantFind)
fmt.Println("\n")
for i, book := range books {
if book.Name == WantFind {
fmt.Printf("想要借這本書嗎?\n書名:%s,作者:%s,書籍ID:%d,借還狀態:%s,借閱人:%s\na.是 b.否:",
book.Name, book.Author, book.ID, getStateString(book.State, 0), getStateString(book.State, 1))
fmt.Scanln(&WantBorrow)
fmt.Println("\n")
if WantBorrow == "a" {
fmt.Print("為避免同名書,請確認書本ID:")
fmt.Scanln(&BookID)
if getStateString(book.State, 0) == "未借出" && book.ID == BookID {
now := time.Now()
fmt.Print("請輸入你的名字:")
fmt.Scanln(&BorrowerName)
fmt.Println("\n")
BorrowTime := now.Format("01月02號")
GiveBackTime := now.AddDate(0, 0, 借書天數).Format("01月01號")
books[i].State = []interface{}{"已借出", BorrowerName, BorrowTime, GiveBackTime}
if err := saveBooks(books); err != nil {
fmt.Println(err)
return
}
fmt.Println("借書成功!")
return
}
fmt.Printf("好書被搶先啦,%s之後再來吧!\n", getStateString(book.State, 3))
return
}
fmt.Println("好書不可錯過,歡迎下次來借這本書喔!")
return
}
}
fmt.Println("請檢查是不是打錯字了喔!")
}
func GiveBack() {
var TheGiveBackBook string
var WantGiveBack string
var BookID int
books, err := loadBooks()
now := time.Now()
if err != nil {
fmt.Println(err)
return
}
fmt.Print("請輸入歸還的書名:")
fmt.Scanln(&TheGiveBackBook)
fmt.Println("\n")
for i, book := range books {
if book.Name == TheGiveBackBook {
fmt.Printf("確定歸還以下書籍:\n書名:%s,作者:%s,書籍ID:%d,借還狀態:%s,借閱人:%s\n", book.Name, book.Author, book.ID, getStateString(book.State, 0), getStateString(book.State, 1))
fmt.Print("為避免同名書,請確認書本ID:")
fmt.Scanln(&BookID)
fmt.Print("是否還書 a.是 b.否:")
fmt.Scanln(&WantGiveBack)
fmt.Println("\n")
if WantGiveBack == "a" && getStateString(book.State, 0) == "已借出" && book.ID == BookID {
books[i].State = []interface{}{"未借出", "無", "", ""}
fmt.Println("還書中......")
if err := saveBooks(books); err != nil {
fmt.Println(err)
return
}
if now.Format("01月01號") == getStateString(book.State, 3) {
fmt.Print("養成有借有還得習慣很重要的喔")
}
fmt.Println("還書成功!")
return
} else {
fmt.Print("書籍狀態:", getStateString(book.State, 0), ",借閱人:", getStateString(book.State, 1))
return
}
fmt.Println("該書已經歸還,如有疑問請找圖書管理員")
return
}
}
fmt.Println("請檢查是不是打錯字了喔!")
}
func login() bool {
var InputName string
var InputPassWord string
var UserDict = map[string]string{"visitor": "abcd1234"}
YN := true
fmt.Println("請先登入")
fmt.Print("使用者名稱 : ")
fmt.Scanln(&InputName)
fmt.Print("密碼 : ")
fmt.Scanln(&InputPassWord)
fmt.Println("\n")
for k, v := range UserDict {
fmt.Println("k, v =", k, v)
if InputName == k && InputPassWord == v {
fmt.Println("登入成功!")
YN = true
} else {
fmt.Println("登入失敗")
YN = false
}
}
return YN
}
func main() {
var DoIt string
if AreYouLogin := login(); AreYouLogin {
for {
fmt.Println("+_______ଲ坦佩斯特國家圖書館ଲ_______+")
fmt.Print("|需要做甚麼? \n|\ta.新書紀錄 \t\t| \n|\tb.想要借書 \t\t| \n|\tc.想要還書 \t\t| \n|\td.登記遺失書籍 \t\t| \n|\te.退出 : \t\t|")
fmt.Println("\n|______________________________」")
fmt.Scanln(&DoIt)
switch DoIt {
case "a":
ANewBook()
fmt.Println("\n")
case "b":
Borrow()
fmt.Println("\n")
case "c":
GiveBack()
fmt.Println("\n")
case "d":
LossingBook()
fmt.Println("\n")
case "e":
fmt.Println("歡迎再來!")
fmt.Println("\n")
return
default:
fmt.Println("似乎輸入錯了,再來一次吧!")
fmt.Println("\n")
}
}
} else {
fmt.Println("請再試一次吧!")
}
}