-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (132 loc) · 2.97 KB
/
main.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type Account struct {
Name string
Balance int
Owe map[string]Owe
}
type Owe struct {
Name string
Amount int
Type int // 1: owed to, 2: owed from
}
var (
userAccount = map[string]Account{}
)
var loginUser Account
func main() {
input := command()
action(input)
}
func command() []string {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
err := scanner.Err()
if err != nil {
log.Fatal(err)
}
return strings.Split(scanner.Text(), " ")
}
func action(input []string) {
switch input[0] {
case "login":
if _, ok := userAccount[input[1]]; !ok {
userAccount[input[1]] = Account{
Name: input[1],
}
}
loginUser = userAccount[input[1]]
fmt.Printf("Hello %s", loginUser.Name)
fmt.Println()
fmt.Printf("Your balance is $%d", loginUser.Balance)
fmt.Println()
if len(loginUser.Owe) > 0 {
for user, data := range loginUser.Owe {
if data.Type == 1 {
fmt.Printf("Owed $%d to %s", data.Amount, user)
fmt.Println()
}
if data.Type == 2 {
fmt.Printf("Owed $%d from %s", data.Amount, user)
fmt.Println()
}
}
}
main()
case "deposit":
amount, err := strconv.Atoi(input[1])
if err != nil {
return
}
loginUser.Balance += amount
userAccount[loginUser.Name] = loginUser
fmt.Printf("Your balance is $%d", userAccount[loginUser.Name].Balance)
fmt.Println()
main()
case "withdraw":
amount, err := strconv.Atoi(input[1])
if err != nil {
return
}
loginUser.Balance -= amount
userAccount[loginUser.Name] = loginUser
fmt.Printf("Your balance is $%d", userAccount[loginUser.Name].Balance)
fmt.Println()
main()
case "transfer":
amount, err := strconv.Atoi(input[2])
if err != nil {
return
}
targetUser := Account{
Name: input[1],
Balance: userAccount[input[1]].Balance,
}
if amount > loginUser.Balance {
targetUser.Balance += amount - loginUser.Balance
if len(loginUser.Owe) == 0 {
loginUser.Owe = map[string]Owe{}
}
loginUser.Owe[targetUser.Name] = Owe{
Name: targetUser.Name,
Amount: amount - loginUser.Balance,
Type: 1,
}
if len(targetUser.Owe) == 0 {
targetUser.Owe = map[string]Owe{}
}
targetUser.Owe[loginUser.Name] = Owe{
Name: loginUser.Name,
Amount: amount - loginUser.Balance,
Type: 2,
}
fmt.Printf("Transferred $%d to %s", loginUser.Balance, targetUser.Name)
fmt.Println()
fmt.Printf("Your balance is $%d", loginUser.Balance)
fmt.Println()
fmt.Printf("Owed $%d to %s", amount-loginUser.Balance, targetUser.Name)
fmt.Println()
loginUser.Balance = 0
} else {
loginUser.Balance -= amount
targetUser.Balance += amount
fmt.Printf("Transferred $%d to %s", amount, targetUser.Name)
fmt.Println()
fmt.Printf("Your balance is $%d", loginUser.Balance)
fmt.Println()
}
userAccount[loginUser.Name] = loginUser
userAccount[targetUser.Name] = targetUser
main()
case "logout":
loginUser = Account{}
main()
}
}