/
client.go
119 lines (97 loc) · 2.76 KB
/
client.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
package main
import (
"bufio"
"os"
"fmt"
"log"
"net/http"
"github.com/gorilla/rpc/json"
"bytes"
)
type Request1 struct {
StockSymbolAndPercentage string `json:"stockSymbolAndPercentage"`
Budget float32 `json:"budget"`
}
type Reply struct {
TradeId int
StrOut string
UnvestedAmount float32
}
type Request2 struct {
TradeId int `json:"tradeId"`
}
type Response2 struct {
Stocks string
CurrentMarketValue float32
UnvestedAmount float32
}
func main() {
var option int
var money float32
var input string
var errChar string
bio := bufio.NewReader(os.Stdin)
fmt.Println("Choose one of the following options: ")
fmt.Println("1. Buy stocks")
fmt.Println("2. View profile")
fmt.Println("Enter 1 or 2")
line,_,_ := bio.ReadLine()
fmt.Sscanf(string(line),"%d %s", &option, &errChar)
if(errChar != "") {
fmt.Println("Invalid input")
os.Exit(1)
}
if(option == 1) {
//Get input
var a Request1
var reply Reply
fmt.Println("Enter string input")
line,_,_ := bio.ReadLine()
fmt.Sscanf(string(line),"%s", &input)
fmt.Println("Enter budget")
line2,_ ,_ := bio.ReadLine()
fmt.Sscanf(string(line2),"%f", &money)
a.Budget= money
a.StockSymbolAndPercentage=input
buf, _ := json.EncodeClientRequest("StockMarket.BuyStocks", &a)
resp, err := http.Post("http://localhost:10000/rpc", "application/json", bytes.NewBuffer(buf))
if err != nil {
fmt.Println("Error occurred", err)
}
defer resp.Body.Close()
err = json.DecodeClientResponse(resp.Body, &reply)
if err != nil {
log.Fatal("Error occurred.. ",err)
}
fmt.Println("Your trade Id is: ", reply.TradeId)
fmt.Println("Summary of your trade: ", reply.StrOut)
fmt.Println("Remaining balance: ", reply.UnvestedAmount)
} else if(option == 2) {
var tid int
var b Request2
var reply2 Response2
fmt.Println("Enter your trade id")
line,_ ,_ := bio.ReadLine()
fmt.Sscanf(string(line),"%d %s", &tid, &errChar)
if(errChar != "") {
fmt.Println("Invalid input")
os.Exit(1)
}
b.TradeId=tid
buf, _ := json.EncodeClientRequest("StockMarket.CheckPortfolio", &b)
resp2, err := http.Post("http://localhost:10000/rpc", "application/json", bytes.NewBuffer(buf))
if err != nil {
fmt.Println("Error:", err)
}
defer resp2.Body.Close()
err = json.DecodeClientResponse(resp2.Body, &reply2)
if err != nil {
log.Fatal(err)
}
fmt.Println("Stocks bought: ", reply2.Stocks)
fmt.Println("CurrentMarketValue: ", reply2.CurrentMarketValue)
fmt.Println("Remaining balance: ", reply2.UnvestedAmount)
} else {
fmt.Println("Invalid input")
}
}