/// <summary> /// Get the exact <c>Box</c> by the arguments /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns><c>null</c> if no match</returns> public Box GetBox(double x, double y) { //Search the main tree var mainTreeSearchTerm = new BottomSizeTreeDataModel(x); if (_mainTree.FindExact(mainTreeSearchTerm, out BottomSizeTreeDataModel mainTreeData) == false) { return(null); } //search the inner tree var innerTreeSearchTerm = new HeightSizeTreeDataModel(y); if (mainTreeData.InnerTree.FindExact(innerTreeSearchTerm, out HeightSizeTreeDataModel innerTreeData) == false) { return(null); } //return the box return(new Box { Id = innerTreeData.Id, X = mainTreeData.X, Y = innerTreeData.Y, Count = innerTreeData.Count, TimeLastPurchase = innerTreeData.DateNode.Data.TimeLastPurchase }); }
/// <summary> /// Buy an amount of boxes of an exact size /// </summary> /// <param name="x">X dimention</param> /// <param name="y">Y dimention</param> /// <param name="count">Amount of boxes</param> /// <returns></returns> public bool BuyExactBoxSize(double x, double y, int count) { //search for the exact size var mainSearchTerm = new BottomSizeTreeDataModel(x); if (_mainTree.FindExact(mainSearchTerm, out BottomSizeTreeDataModel mainTreeData) == false) { //if no data found with the x value _logger.Log(new LogData($"Can't find a box with a X value of: {x}", false)); return(false); } var innerSearchTerm = new HeightSizeTreeDataModel(y); if (mainTreeData.InnerTree.FindExact(innerSearchTerm, out HeightSizeTreeDataModel innerTreeData) == false) { //if no data found for this x value with this y value _logger.Log(new LogData($"Can't find a box with a X value of: {x} and a Y value of {y}", false)); return(false); } //if found the spesific box. //check stock avilability, don't allow to perchues more than what you have if (innerTreeData.Count < count) { _logger.Log(new LogData($"Not enugh boxes of this size in stock, in stock: {innerTreeData.Count}", new Box { X = x, Y = y, Count = count }, false)); return(false); } BuyHelper(mainTreeData, innerTreeData, count); //Save the changes in the db //SaveToDB(x, y, innerTreeData.Count, DateTime.Now); return(true); }
/// <summary> /// Add Boxes to the stock. /// if the box exist will only update the Count /// if the box doesn't exist will create a new record /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="count"></param> /// <param name="timeLastPurchase"></param> void AddToStock(double x, double y, int count, DateTime?timeLastPurchase = null, bool saveToDB = true, int?id = null) { bool newBox = false; //Get the mainTreeData for the search term from the main tree var mainTreeSearchTerm = new BottomSizeTreeDataModel(x); _mainTree.FindAndUpdate(mainTreeSearchTerm, out BottomSizeTreeDataModel mainTreeData); //Get the innerTreeData for the searchTerm in the inner tree var innerTreeSearchTerm = new HeightSizeTreeDataModel(y); if (mainTreeData.InnerTree is null) { mainTreeData.InnerTree = new BinarySearchTree <HeightSizeTreeDataModel>(); } //if the inner TreeData is new if (mainTreeData.InnerTree.FindAndUpdate(innerTreeSearchTerm, out HeightSizeTreeDataModel innerTreeData) == false) { //if there isn't time data, than will get the current date and time if (timeLastPurchase.HasValue == false) { innerTreeData.DateNode = _dataByTime.Add(new TimeListDataModel() { X = x, Y = y, TimeLastPurchase = DateTime.Now }); } //if there is a time last purchase it means this should be added to a diffrent position. //will never be null here else { innerTreeData.DateNode = _dataByTime.InsertByValue(new TimeListDataModel { X = x, Y = y, TimeLastPurchase = timeLastPurchase.Value }); } newBox = true; } //if the inner tree data is old else { //update the data innerTreeData.DateNode.Data.TimeLastPurchase = DateTime.Now; //Shift the node to the end of the list _dataByTime.ShiftNodeToEnd(innerTreeData.DateNode); } //Set the Properties innerTreeData.Count += count; //if there is no value than id will be 0 by default(int) if (id.HasValue) { innerTreeData.Id = id.Value; } //the box of this size var box = new Box { Id = innerTreeData.Id, X = mainTreeData.X, Y = innerTreeData.Y, Count = innerTreeData.Count, TimeLastPurchase = innerTreeData.DateNode.Data.TimeLastPurchase }; //Add Data To DB if (saveToDB) { _logger.Log(new LogData("Updated Stock", true)); //save the id innerTreeData.Id = SaveToDB(box, newBox).Id; } }