package main
import (
"encoding/json"
"fmt"
)
type Seller struct {
Name string
ShareName string
Holdings int
Quantity int
PerShare float64
}
type Buyer struct {
Name string
ShareName string
Holdings int
Quantity int
PerShare float64
}
func validateTransaction(seller Seller,buyer Buyer) bool{
var isallowTransact bool =false
if (seller.Quantity >=buyer.Quantity &&seller.PerShare == buyer.PerShare && seller.ShareName ==buyer.ShareName){
isallowTransact=true;
}
return isallowTransact
}
func executeTransaction(seller Seller,buyer Buyer) {
seller.Holdings -=seller.Quantity;
buyer.Holdings +=seller.Quantity;
fmt.Printf("seller current holding : %d, \n buyyer current holding: %d",seller.Holdings,buyer.Holdings)
}
func main() {
sellerJson :=`{"name":"pavan","ShareName":"TCS","holdings":100,"quantity":30,"perShare":11.11}`
buyerJson :=`{"name":"Raju","ShareName":"TCS","holdings":0,"quantity":30,"perShare":14.11}`
var seller Seller
var buyer Buyer
json.Unmarshal([]byte(sellerJson ), &seller)
json.Unmarshal([]byte(buyerJson ), &buyer)
//fmt.Printf("seller name : %s, shares of firm: %s,total holdings: %d, want to sell: %d,unit cost: %f", seller.Name , seller.ShareName,seller.Holdings , seller.Quantity,seller.PerShare )
var isallowExecute bool =false
isallowExecute =validateTransaction(seller,buyer)
if(isallowExecute){
executeTransaction(seller,buyer)
}else{
fmt.Print("\n seems buyer quotes are not matching with seller so we are not able to perform transacrion ,Please check and try again");
}
fmt.Println("\n Happy Trading...!!");
}