예제 #1
0
func (s *StockService) ReturnStock(r *http.Request, args *Args, reply *Reply) error {
	var receivedData string = args.StockSymbolAndPercentage
	var budget int = args.Budget
	var stockInfo []StockInfo = splitStockData(receivedData)

	var nameOfCompanies string
	for i := 0; i < len(stockInfo)-1; i++ {
		nameOfCompanies += stockInfo[i].companyName + ","
	}
	nameOfCompanies += stockInfo[len(stockInfo)-1].companyName

	var priceList []float64 = yahoofinance.ReturnStockPrice(nameOfCompanies)
	var replyFromStockMarket Reply = buyStock(stockInfo, priceList, budget)
	reply.TradeId = replyFromStockMarket.TradeId
	reply.Stocks = replyFromStockMarket.Stocks
	reply.UnvestedAmount = replyFromStockMarket.UnvestedAmount
	return nil
}
예제 #2
0
func getStock(tradeId int) TradeReply {
	var nameOfCompanies string
	var totalStocks int = len(tradeStorage[tradeId].stockList)
	var currentMarketValue float64
	var responseData string
	var reply TradeReply

	for i := 0; i < (totalStocks - 1); i++ {
		nameOfCompanies += tradeStorage[tradeId].stockList[i].companyName + ","
	}
	nameOfCompanies += tradeStorage[tradeId].stockList[totalStocks-1].companyName
	var currentPriceList []float64 = yahoofinance.ReturnStockPrice(nameOfCompanies)

	for i := 0; i < totalStocks; i++ {
		companyName := tradeStorage[tradeId].stockList[i].companyName
		numberOfStock := tradeStorage[tradeId].stockList[i].numberOfStock
		buyingPrice := tradeStorage[tradeId].stockList[i].stockPrice
		currentMarketValue += float64(numberOfStock) * currentPriceList[i]
		responseData += "'" + companyName + ":" + strconv.Itoa(numberOfStock) + ":"

		if buyingPrice < currentPriceList[i] {
			responseData += "+" + "$" + strconv.FormatFloat(currentPriceList[i], 'f', -1, 64) + "'"
		} else if buyingPrice > currentPriceList[i] {
			responseData += "-" + "$" + strconv.FormatFloat(currentPriceList[i], 'f', -1, 64) + "'"
		} else {
			responseData += "$" + strconv.FormatFloat(currentPriceList[i], 'f', -1, 64) + "'"
		}
		if i != (totalStocks - 1) {
			responseData += ","
		}
	}
	reply.Stocks = responseData
	reply.CurrentMarketValue = currentMarketValue
	reply.UnvestedAmount = tradeStorage[tradeId].uninvestedAmount

	return reply
}