source: server/cli.go@ b715712

main
Last change on this file since b715712 was b715712, checked in by Stefan <trsunovstefan@…>, 8 weeks ago

Add the server side and configuration

  • Property mode set to 100644
File size: 2.1 KB
Line 
1package main
2
3import (
4 "bufio"
5 "fmt"
6 "os"
7 "strings"
8)
9
10// Session holds the currently-logged-in user, if any.
11type Session struct {
12 UserID string
13 Username string
14}
15
16var stdin = bufio.NewReader(os.Stdin)
17
18func prompt(label string) string {
19 fmt.Print(label)
20 line, err := stdin.ReadString('\n')
21 // On EOF (Ctrl-D, or the end of a piped script) ReadString keeps returning
22 // an error forever. Without this the menu loop would spin printing
23 // "Unknown option." indefinitely instead of ending.
24 if err != nil && strings.TrimSpace(line) == "" {
25 fmt.Println("\nInput closed. Goodbye.")
26 os.Exit(0)
27 }
28 return strings.TrimSpace(line)
29}
30
31func RunCLI() {
32 fmt.Println("=========================================")
33 fmt.Println(" EduBerza - Crypto Exchange Simulation")
34 fmt.Println("=========================================")
35
36 var s Session
37 for {
38 if s.UserID == "" {
39 anonymousMenu(&s)
40 } else {
41 authenticatedMenu(&s)
42 }
43 }
44}
45
46func anonymousMenu(s *Session) {
47 fmt.Println()
48 fmt.Println("[1] Register")
49 fmt.Println("[2] Login")
50 fmt.Println("[3] Browse markets")
51 fmt.Println("[0] Exit")
52 switch prompt("> ") {
53 case "1":
54 Register()
55 case "2":
56 Login(s)
57 case "3":
58 ListMarkets()
59 case "0":
60 fmt.Println("Goodbye.")
61 os.Exit(0)
62 default:
63 fmt.Println("Unknown option.")
64 }
65}
66
67func authenticatedMenu(s *Session) {
68 fmt.Printf("\n--- Logged in as %s ---\n", s.Username)
69 fmt.Println("[1] View balance")
70 fmt.Println("[2] Deposit virtual funds")
71 fmt.Println("[3] Browse markets")
72 fmt.Println("[4] Place market BUY order")
73 fmt.Println("[5] Place market SELL order")
74 fmt.Println("[6] View portfolio")
75 fmt.Println("[7] View transaction history")
76 fmt.Println("[8] Manage watchlist")
77 fmt.Println("[9] Logout")
78 fmt.Println("[0] Exit")
79 switch prompt("> ") {
80 case "1":
81 ShowBalance(s)
82 case "2":
83 Deposit(s)
84 case "3":
85 ListMarkets()
86 case "4":
87 PlaceOrder(s, "buy")
88 case "5":
89 PlaceOrder(s, "sell")
90 case "6":
91 ShowPortfolio(s)
92 case "7":
93 ShowTransactions(s)
94 case "8":
95 ManageWatchlist(s)
96 case "9":
97 s.UserID = ""
98 s.Username = ""
99 fmt.Println("Logged out.")
100 case "0":
101 fmt.Println("Goodbye.")
102 os.Exit(0)
103 default:
104 fmt.Println("Unknown option.")
105 }
106}
Note: See TracBrowser for help on using the repository browser.