Пример #1
0
        public void TestBuySellActions()
        {
            bool error = false;

            try
            {
                MarketClientClass client = new MarketClientClass();

                MarketBuySell buyreq1 = client.SendBuyRequest(1, 1, 3); //legal req
                Assert.IsNull(buyreq1.Error);                           //null expected
                client.SendCancelBuySellRequest(buyreq1.Id);


                MarketBuySell sellreq1 = client.SendBuyRequest(1, 1, 3); //legal req
                Assert.IsNull(sellreq1.Error);                           //null expected
                client.SendCancelBuySellRequest(sellreq1.Id);

                //no errors expected ( null)
            }

            catch (Exception)
            {
                error = true;
            }

            Assert.IsFalse(error);                       //false expected
        }
Пример #2
0
        //this function activate - buy/sell request. it collect info from user - send it to Logic layer and prints an answer
        private static void CollectingInfoBUYSELL(string a)
        {
            //write more legallity checks
            int Commodity, Amount, Price;

            do
            {
                Console.WriteLine("Please enter Commodity");
                Commodity = Myconvert(Console.ReadLine());
                Console.WriteLine("Please enter Amount");
                Amount = Myconvert(Console.ReadLine());
                Console.WriteLine("Please enter Price");
                Price = Myconvert(Console.ReadLine());
            } while (Commodity == -1 | Amount == -1 | Price == -1);

            MarketBuySell IDbuysell;

            if (a.Equals("1"))                                               //'1' means buy
            {
                IDbuysell = client.SendBuyRequest(Price, Commodity, Amount); //call to Logic layer func
                Console.WriteLine(IDbuysell.ToString());
            }
            else                                                              //means sell
            {
                IDbuysell = client.SendSellRequest(Price, Commodity, Amount); //call to Logic layer func
                Console.WriteLine(IDbuysell.ToString());
            }

            return;
        }//collect info buy-sell request
Пример #3
0
        public void TestFalseCommodity()
        {
            MarketClientClass client = new MarketClientClass();

            MarketBuySell buyreq1 = client.SendBuyRequest(5, -2, 3);   //no such commodies : -2

            Assert.IsNotNull(buyreq1.Error);

            MarketBuySell buyreq2 = client.SendBuyRequest(1, 10, 3);    //no such commodies : 10

            Assert.IsNotNull(buyreq2.Error);

            MarketBuySell sellreq3 = client.SendSellRequest(7, 24, 3);   //no such commodies : 24

            Assert.IsNotNull(sellreq3.Error);

            //Errors expected (not null)
        }
Пример #4
0
        public static void AMA_Buy(int commodity, int desiredPrice, int amount)
        {
            FLAG_isRunning = true;
            notOverLoadServer();
            MarketClientClass client = new MarketClientClass();
            AllMarketRequest  all    = client.QueryAllMarketRequest();

            counter++;


            foreach (ItemAskBid item in all.MarketInfo)
            {
                if (item.Id == commodity && item.Info.Ask <= desiredPrice)
                {   //if item is the right commodity & right price
                    MarketUserData userData = client.SendQueryUserRequest();
                    counter++;

                    List <int> l = userData.Requests;

                    if (l.Count != 0)                      //there are open requests in server

                    //if USER dont have enough money, we'll cancel his open buy requests- hoping after that he'll have enough
                    {
                        for (int i = l.Count; i >= 0 & userData.Funds < (item.Info.Ask * amount); i--)   //going from end so in delete won't change index of l
                        {
                            notOverLoadServer();

                            int reqID = l[i];        //saving the ID just for simplicity

                            MarketItemQuery request = client.SendQueryBuySellRequest(l[i]);
                            counter++;
                            if (request.Type.Equals("buy"))    //Note: check with roey
                            {
                                client.SendCancelBuySellRequest(reqID);
                                HistoryLogger.WriteHistory("Cancel," + request.Commodity + "," + request.Price + "," + request.Amount + "," + reqID);
                                counter++;
                            }
                        }
                    }

                    if (userData.Funds >= item.Info.Ask * amount)
                    {
                        int ID = client.SendBuyRequest(item.Info.Ask + 1, commodity, amount).Id;
                        HistoryLogger.WriteHistory("Buy," + commodity + "," + (item.Info.Ask + 1) + "," + amount + "," + ID);
                        counter++;
                    }
                }//bigIf
            }
            FLAG_isRunning = false;
            return;
        }//AMAbuy
Пример #5
0
        public static void AMA_Buy(int commodity, int desiredPrice, int amount)
        {
            FLAG_isRunning = true;
            NotOverLoadServer();

            MarketClientClass client = new MarketClientClass();
            MarketUserData userData = client.SendQueryUserRequest();
            NotOverLoadServer();

            if (userData.Error != null)
            {
                FLAG_isRunning = false;
                return;
            }

            if (userData.Funds >= desiredPrice * amount)    //if we have enough money- just buy and finish running.
            {
                MarketBuySell buyreq = client.SendBuyRequest(desiredPrice, commodity, amount);
                NotOverLoadServer();

                if (buyreq.Error == null)          //the buy req is successfuly passed to the server
                    HistoryLogger.WriteHistory(buyreq.Id, "Buy", commodity, desiredPrice, amount);
                FLAG_isRunning = false;
                return;
            }

            //if USER dont have enough money, we'll cancel his open buy requests- hoping after that he'll have enough
            List<int> l = userData.Requests;

            if (l.Count == 0)               //there are NO open requests in server
            {
                FLAG_isRunning = false;
                return;
            }

            for (int i = l.Count - 1; i >= 0 && userData.Funds < (desiredPrice * amount); i--)   //going from end so in delete won't change index of l
            {
                int reqID = l[i];    //saving the ID just for simplicity

                MarketItemQuery request = client.SendQueryBuySellRequest(reqID);
                NotOverLoadServer();

                if (request.Error != null)
                {
                    FLAG_isRunning = false;
                    return;
                }

                //wish to cancel only buy requests. only this kind of canceling request give back money
                //func SendCancelBuySellRequest returns bool - of the action passed successfuly
                if (request.Type.Equals("buy") && client.SendCancelBuySellRequest(reqID))
                    HistoryLogger.WriteHistory(reqID, "Cancel", request.Commodity, request.Price, request.Amount);

                NotOverLoadServer();
            }

            userData = client.SendQueryUserRequest();   //refresh data
            NotOverLoadServer();

            if (userData.Error != null)
            {
                FLAG_isRunning = false;
                return;
            }

            if (userData.Funds >= desiredPrice * amount)    //if NOW we have enough money-  buy 
            {
                MarketBuySell buyreq = client.SendBuyRequest(desiredPrice, commodity, amount);
                NotOverLoadServer();

                if (buyreq.Error == null)          //the buy req is successfuly passed to the server
                    HistoryLogger.WriteHistory(buyreq.Id, "Buy", commodity, desiredPrice, amount);
            }
            FLAG_isRunning = false;
            return;
        }//AMAbuy