-
Notifications
You must be signed in to change notification settings - Fork 0
/
reservation.go
50 lines (44 loc) · 1.13 KB
/
reservation.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
package reservation
import (
"bytes"
"encoding/gob"
"fmt"
)
const (
OPNEW string = "New_Reservation"
OPREM string = "Remove_Reservation"
OPCHG string = "Update_Reservation"
)
// Reservation is the main object of the system
type Reservation struct {
ID int `json:"id,omitempty"`
Date string `json:"date"`
Name string `json:"name"`
Hour int `json:"hour"`
Party int `json:"party"`
}
// Bytes converts a Reservation instance into an array of bytes.
func (r Reservation) Bytes() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(r); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Print prints a reservation details
func (r Reservation) Print() {
fmt.Println("RESERVATION : ")
fmt.Printf(" ID : %d\n", r.ID)
fmt.Printf(" Name : %s\n", r.Name)
fmt.Printf(" Party : %d\n", r.Party)
fmt.Printf(" Date : %s\n", r.Date)
fmt.Printf(" Hour : %d\n", r.Hour)
}
// Decode convert array of bytes into a new instance of a reservation.
func Decode(b []byte) (Reservation, error) {
var r Reservation
dec := gob.NewDecoder(bytes.NewReader(b))
err := dec.Decode(&r)
return r, err
}