source: server/cli.go

main
Last change on this file was ef1c1c7, checked in by Stefan <trsunovstefan@…>, 5 days ago

Wiki docs, phase 6 and phase 7 added

  • Property mode set to 100644
File size: 2.5 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 BUY order")
73 fmt.Println("[5] Place 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("[10] Report: top traders")
79 fmt.Println("[11] Report: market performance")
80 fmt.Println("[12] Order book")
81 fmt.Println("[13] My open orders")
82 fmt.Println("[14] Cancel an order")
83 fmt.Println("[0] Exit")
84 switch prompt("> ") {
85 case "1":
86 ShowBalance(s)
87 case "2":
88 Deposit(s)
89 case "3":
90 ListMarkets()
91 case "4":
92 PlaceOrder(s, "buy")
93 case "5":
94 PlaceOrder(s, "sell")
95 case "6":
96 ShowPortfolio(s)
97 case "7":
98 ShowTransactions(s)
99 case "8":
100 ManageWatchlist(s)
101 case "10":
102 ShowTopTraders(s)
103 case "11":
104 ShowMarketPerformance(s)
105 case "12":
106 ShowOrderBook()
107 case "13":
108 ShowMyOrders(s)
109 case "14":
110 CancelOrder(s)
111 case "9":
112 s.UserID = ""
113 s.Username = ""
114 fmt.Println("Logged out.")
115 case "0":
116 fmt.Println("Goodbye.")
117 os.Exit(0)
118 default:
119 fmt.Println("Unknown option.")
120 }
121}
Note: See TracBrowser for help on using the repository browser.