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 }
}//AMAbuy public static void AMA_Sell(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; } foreach (int cmdty in userData.Commodities.Keys) //passing on all commodities { if (cmdty == commodity && userData.Commodities[cmdty] > 0) //check if we own that commodity { //if item is the right commodity & we own it if (amount > userData.Commodities[cmdty] || amount == -1) //we cant sell more than we have OR -1 is our sign to sell ALL amount = userData.Commodities[cmdty]; MarketBuySell sellreq = client.SendSellRequest(desiredPrice, commodity, amount); NotOverLoadServer(); if (sellreq.Error == null) //the sell req is successfuly passed to the server HistoryLogger.WriteHistory(sellreq.Id, "Sell", commodity, desiredPrice, amount); } } FLAG_isRunning = false; return; }//AMAsell
/// <summary> /// checks if the input is valid and if so intilazing a sell request with the gven parameters. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SellButton_Click(object sender, RoutedEventArgs e) { if (!(Int32.TryParse(SellCommodityField.Text, out int Commodity)) && Commodity > 9) { 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)) && Amount == 0) { MessageBox.Show("Invalid Amount", "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Error); } if (Commodity > 0 || Price > 0 || Amount > 0) { MarketBuySell marketBuySell = market.SendSellRequest(Price, Commodity, Amount); if (marketBuySell.Error == null) { MessageBox.Show("Sucsess!! Your Sell request has been placed. your id is: " + marketBuySell.Id); HistoryLogger.WriteHistory(marketBuySell.Id, "Sell", Commodity, Price, Amount); Updater(); } else { MessageBox.Show(marketBuySell.ToString()); } } else { MessageBox.Show("Invalid Input", "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Information); } }
public void TestFalsePrice() { MarketClientClass client = new MarketClientClass(); MarketBuySell sellreq1 = client.SendSellRequest(2000000, 2, 1); //no such price : 2,000,000 Assert.IsNotNull(sellreq1.Error); MarketBuySell sellreq2 = client.SendSellRequest(-25, 5, 3); //no such price : -25 Assert.IsNotNull(sellreq2.Error); //Errors expected (not null) }
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) }
public void TestFalseAmount() { MarketClientClass client = new MarketClientClass(); MarketBuySell sellreq1 = client.SendSellRequest(1, 2, -8); //no such amount : -8 Assert.IsNotNull(sellreq1.Error); MarketBuySell sellreq2 = client.SendSellRequest(4, 4, -3); //no such amount : -3 Assert.IsNotNull(sellreq2.Error); MarketBuySell sellreq3 = client.SendSellRequest(2, 7, -100); //no such amount : -100 Assert.IsNotNull(sellreq3.Error); //Errors expected (not null) }
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