示例#1
0
 /// <summary>
 /// checks if the input is valid and if so intilazing a buy request with the gven parameters.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void BuyButton_Click(object sender, RoutedEventArgs e)
 {
     if (!(Int32.TryParse(SellCommodityField.Text, out int Commodity)))
     {
         MessageBox.Show("Invalid Commodity", "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Error);
     }
     if (!(Int32.TryParse(SellPriceField.Text, out int Price)))
     {
         MessageBox.Show("Invalid Price", "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Error);
     }
     if (!(Int32.TryParse(SellAmountField.Text, out int Amount)))
     {
         MessageBox.Show("Invalid Amount", "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Error);
     }
     if (Commodity > 0 || Price > 0 || Amount > 0)
     {
         MarketBuySell marketBuySell = market.SendBuyRequest(Price, Commodity, Amount);
         if (marketBuySell.Error == null)
         {
             MessageBox.Show("Sucsess!! Your Buy request has been placed. your id is: " + marketBuySell.Id);
             HistoryLogger.WriteHistory(marketBuySell.Id, "Buy", Commodity, Price, Amount);
             Updater();
         }
         else
         {
             MessageBox.Show(marketBuySell.ToString());
         }
     }
     else
     {
         MessageBox.Show("Invalid Input", "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Information);
     }
 }
示例#2
0
        private String Interperate(String cmd)
        {
            String[] input = cmd.Split(); //split the input into words in order to analyze it

            String err;

            switch (input[0]) //gose throw every case senarieo possibale for the input
            {
            case "buy":
                err = "Invalid Paramaters for 'buy' request. Correct format is: buy <int price> <int commodity> <int amount>";
                if (input.Length != 4)
                {
                    Console.WriteLine(err);
                }
                else if (!Shell.isNumeric(input, 1))
                {
                    Console.WriteLine(err);
                }
                else     //the input is leagle
                {
                    int response = -1;

                    try     //send buy requst and return the received output
                    {
                        response = marketClient.SendBuyRequest(Int32.Parse(input[1]), Int32.Parse(input[2]), Int32.Parse(input[3]));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }

                    return(response == -1 ? "" : response.ToString());    //if the send request failed return -1 else return the response
                }
                break;


            case "sell":
                err = "Invalid Paramaters for 'sell' request. Correct format is: sell <int price> <int commodity> <int amount>";
                if (input.Length != 4)
                {
                    Console.WriteLine(err);
                }
                else if (!Shell.isNumeric(input, 1))
                {
                    Console.WriteLine(err);
                }
                else     //send sell requst and return the received output
                {
                    int response = -1;

                    try
                    {
                        response = marketClient.SendSellRequest(Int32.Parse(input[1]), Int32.Parse(input[2]), Int32.Parse(input[3]));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    return(response == -1 ? "" : response.ToString());    //if the buy request failed return -1 else return the response
                }
                break;


            case "cancel":
                err = "Invalid Paramaters for 'cancel' request. Correct format is: cancel <int request id>";
                if (input.Length != 2)
                {
                    Console.WriteLine(err);
                }
                else if (!Shell.isNumeric(input, 1))
                {
                    Console.WriteLine(err);
                }
                else     //send cancel requst and return the received output
                {
                    bool response = false;

                    try
                    {
                        response = marketClient.SendCancelBuySellRequest(Int32.Parse(input[1]));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                    if (response)
                    {
                        return("OK");
                    }
                    else
                    {
                        return("");
                    }
                }
                break;


            case "qreq":
                err = "Invalid Paramaters for 'qreq' request. Correct format is: qreq <int request id>";
                if (input.Length != 2)
                {
                    Console.WriteLine(err);
                }
                else if (!Shell.isNumeric(input, 1))
                {
                    Console.WriteLine(err);
                }
                else     //send Market Item Query requst and return the received output
                {
                    IMarketItemQuery response = new RealMarketItemQuery();

                    bool error = false;

                    try
                    {
                        response = marketClient.SendQueryBuySellRequest(Int32.Parse(input[1]));
                    }
                    catch (Exception e)
                    {
                        error = true;
                        Console.WriteLine(e.Message);
                    }

                    if (error)
                    {
                        return("");
                    }
                    else
                    {
                        return(response.ToString());
                    }
                }
                break;


            case "quser":
                err = "Invalid Paramaters for 'quser' request. Correct format is: quser";
                if (input.Length != 1)
                {
                    Console.WriteLine(err);
                }
                else     //send market user data requst and return the received output
                {
                    IMarketUserData response = new RealMarketUserData();
                    bool            error    = false;

                    try
                    {
                        response = marketClient.SendQueryUserRequest();
                    }
                    catch (Exception e)
                    {
                        error = true;
                        Console.WriteLine(e.Message);
                    }

                    if (error)
                    {
                        return("");
                    }
                    else
                    {
                        return(response.ToString());
                    }
                }
                break;



            case "qmarket":
                err = "Invalid Paramaters for 'qmarket' request. Correct format is: qmarket <int commodity>";
                if (input.Length != 2)
                {
                    Console.WriteLine(err);
                }
                else if (!Shell.isNumeric(input, 1))
                {
                    Console.WriteLine(err);
                }
                else     //send Market commodity offer requst and return the received output
                {
                    IMarketCommodityOffer response = new RealMarketCommodityOffer();

                    bool error = false;

                    try
                    {
                        response = marketClient.SendQueryMarketRequest(Int32.Parse(input[1]));
                    }
                    catch (Exception e)
                    {
                        error = true;
                        Console.WriteLine(e);
                    }

                    if (error)
                    {
                        return("");
                    }
                    else
                    {
                        return(response.ToString());
                    }
                }
                break;

            default:     //in case of invalid input
                return("Unknown Command");
            }
            Console.WriteLine();
            return(" ");
        }