source: server/cli.go@ a531b45

main
Last change on this file since a531b45 was 9577c79, checked in by Stefan <trsunovstefan@…>, 13 days ago

add reserved_quantity and modify the phases, add v_03.png and v_03.xml for P1

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