-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticket.go
59 lines (48 loc) · 1.04 KB
/
ticket.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
package main
import (
"fmt"
"os"
)
type passenger struct {
name string
seatNo int
destination string
price float64
}
type ticket struct {
name string
passengers []passenger
}
func newTicket(name string) ticket {
return ticket{
name: name,
passengers: []passenger{},
}
}
func (t *ticket) addPassenger(pName string, sNo int, place string, price float64) {
p := passenger{
name: pName,
seatNo: sNo,
destination: place,
price: price,
}
t.passengers = append(t.passengers, p)
}
func (t *ticket) format() string {
fs := "Nomad Ticket \n"
var total float64 = 0
for _, p := range t.passengers {
fs += fmt.Sprintf("%-25v ...Seat No: %v ...Destination: %v ...₹%v\n", p.name+":", p.seatNo, p.destination, p.price)
total += p.price
}
fs += fmt.Sprintf("\n%-25v ...₹%v\n", "Total:", total)
return fs
}
func (t *ticket) save() {
data := []byte(t.format())
err := os.WriteFile("tickets/"+t.name+".txt", data, 0644)
if err != nil {
panic(err)
}
fmt.Println("Ticket is saved")
}