Станислав обнови решението на 06.12.2015 16:51 (преди над 2 години)
+package main
+
+import (
+ "encoding/json"
+ "encoding/xml"
+ "fmt"
+)
+
+type error interface {
+ Error() string
+}
+
+type errorString struct {
+ s string
+}
+
+func (e *errorString) Error() string {
+ return e.s
+}
+
+type Stringer interface {
+ String() string
+}
+
+type Book struct {
+ Title string `xml:"title" json:"title"`
+ Isbn string `xml:"isbn,attr" json:"isbn"`
+ Author map[string]string `xml:"author>first_name" "author>last_name" json:"author"`
+ Ratings [5]int `xml:"ratings>rating" json:"ratings"`
+}
+
+func (b Book) String() string {
+ return "[" + b.Isbn + "]" + " " + b.Title + " " + "от" + " " + b.Author["first_name"] + " " + b.Author["last_name"] + "\n"
+}
+
+type Librarian struct {
+ busy bool
+}
+
+type Mylib struct {
+ Collection map[string]int
+ workers []Librarian
+}
+
+type Request struct {
+ Isbn string
+ Type int
+}
+
+func (r *Request) GetType() int {
+ return r.Type
+}
+
+func (r *Request) GetIsbn() string {
+ return r.Isbn
+}
+
+type Response struct {
+ l Library
+ req Request
+}
+
+func (r *Response) GetBook() (fmt.Stringer, error) {
+ book := new(Book)
+ t := r.req.GetType()
+ var err error
+ switch {
+ //Borrow book
+ case t == 1:
+ x := r.l.GetCollection()
+ for key, _ := range x {
+ json.Unmarshal([]byte(key), book)
+ if r.req.Isbn == book.Isbn {
+ if x[key] == 0 {
+ return nil, &errorString{"Няма наличност на книга" + " " + r.req.Isbn}
+ } else {
+ x[key] = x[key] - 1
+ return book, nil
+ }
+ }
+ return nil, &errorString{"Непозната книга" + " " + r.req.Isbn}
+ }
+ // Return book
+ case t == 2:
+ x := r.l.GetCollection()
+ for key, _ := range x {
+ json.Unmarshal([]byte(key), book)
+ if r.req.Isbn == book.Isbn {
+ x[key] = x[key] + 1
+ break
+ }
+ return nil, &errorString{"Непозната книга" + " " + r.req.Isbn}
+ }
+ marshalledb, e := json.Marshal(book)
+ if e != nil {
+ return nil, err
+ }
+ _, err = r.l.AddBookJSON(marshalledb)
+ return nil, err
+ }
+ return book, err
+}
+
+func NewLibrary(librarians int) Library {
+ new_lib := new(Mylib)
+ var x = make([]Librarian, librarians)
+ new_lib.workers = x
+ return new_lib
+}
+
+func (l *Mylib) AddBookJSON(data []byte) (int, error) {
+ var count int
+ var err error
+ value, ok := l.Collection[string(data)]
+ if ok && value >= 0 && value < 4 {
+ count = value + 1
+ l.Collection[string(data)] = count
+ err = nil
+ } else {
+ if ok == false {
+ l.Collection = make(map[string]int)
+ count = 1
+ l.Collection[string(data)] = 1
+ err = nil
+ } else {
+ book := new(Book)
+ json.Unmarshal(data, &book)
+ return 0, &errorString{"Има 4 копия на книга" + " " + book.Isbn}
+ }
+ }
+ return count, err
+}
+
+func (l *Mylib) AddBookXML(data []byte) (int, error) {
+ b := new(Book)
+ xml.Unmarshal(data, &b)
+ mb, _ := json.Marshal(b)
+ x, e := l.AddBookJSON(mb)
+ return x, e
+}
+
+func (lib *Mylib) GetCollection() map[string]int {
+ return lib.Collection
+}
+
+func (lib *Mylib) GetWorkers() []Librarian {
+ return lib.workers
+}
+
+type Library interface {
+ AddBookJSON(data []byte) (int, error)
+ AddBookXML(data []byte) (int, error)
+ //Hello() (chan<- LibraryRequest, <-chan LibraryResponse)
+ GetCollection() map[string]int
+ GetWorkers() []Librarian
+}
+
+type LibraryRequest interface {
+ GetType() int
+ GetISBN() string
+}
+
+type LibraryResponse interface {
+ GetBook() (fmt.Stringer, error)
+ //GetAvailability() (available int, registered int)
+}
Така както си си закоментирал половината от интерфейсите ще получиш 0 точки
Просто не съм дефинирал част от функциите, защото не успях да ги направя, не виждам какъв е проблемът.
Не ти се компилира - 0 точки.