public void EditOrderValidation(int orderNumber, string orderDate, string customerName, string state, string productType, decimal area, decimal tax, decimal total, bool expectedResult) { IOrderRepository _orderRepository = new OrderProdRepository(); OrderManager _orderManager = OrderManagerFactory.Create(); Order order = new Order { Number = orderNumber, CustomerName = customerName, State = state, ProductType = productType, Area = area }; ValidateEditOrderResponse response = _orderManager.ValidateEditOrder(orderDate, order); Assert.AreEqual(expectedResult, response.Success); Assert.AreEqual(tax, order.Tax); Assert.AreEqual(total, order.Total); }
public void Execute() { Console.Clear(); OrderManager orderManager = OrderManagerFactory.Create(); Console.WriteLine("Edit Order"); Console.WriteLine("**********"); _orderDate = prompt.GetOrderDate(); _orderNumber = prompt.GetOrderNumber(); // Sends the order date and number off to the order manager for validation GetOrderResponse getOrderResponse = orderManager.GetOrder(_orderDate, _orderNumber); // Checks if the date is invalid if (!getOrderResponse.IsValidDate) { prompt.PrintError(ErrorCode.NotAValidDate); } // Checks if the date is in the future else if (!getOrderResponse.IsFutureDate) { prompt.PrintError(ErrorCode.NotAFutureDate); } else if (!getOrderResponse.Success) { prompt.PrintError(getOrderResponse.Code); } else { string strInput; Product product; decimal decInput; // Clone the order so it can be stored in _oldOrder and be used to compare the old and new side by side _oldOrder = orderManager.Clone(getOrderResponse.Order); Console.WriteLine("To skip changing a certain field, simply press enter without entering data."); // Gets the new customer name if they entered something strInput = prompt.GetCustomerName(getOrderResponse.Order.CustomerName); if (prompt.DidChange(strInput)) { getOrderResponse.Order.CustomerName = strInput; _madeChanges = true; } // Gets the new customer state if they entered something strInput = prompt.GetCustomerState(getOrderResponse.Order.State); if (prompt.DidChange(strInput)) { getOrderResponse.Order.State = strInput; _madeChanges = true; } // Gets the new product if they entered something product = prompt.ProductPickList(getOrderResponse.Order.ProductType); if (product.ProductName != null) { getOrderResponse.Order.ProductType = product.ProductName; _madeChanges = true; } // Gets the new area if they entered something decInput = prompt.GetArea(getOrderResponse.Order.Area); if (decInput != -1) { getOrderResponse.Order.Area = decInput; _madeChanges = true; } // Checks if the user made any changes to any of the above fields if (!_madeChanges) { prompt.PrintSuccessMessage("\nNo changes were made."); } else { // Sends the edited order off to the order manager for validation ValidateEditOrderResponse response = orderManager.ValidateEditOrder(_orderDate, getOrderResponse.Order); if (!response.Success) { prompt.PrintError(response.Code); } else { // Prints the old order information next to the edited order information prompt.PrintChanges(_oldOrder, getOrderResponse.Order); // Gets confirmation on whether to edit or not do { _code = prompt.GetConfirmation("edit"); } while (_code == YesNo.Invalid); // If the user decides not to make changes, print that to the screen if (_code == YesNo.No) { prompt.PrintError(ErrorCode.DidntConfirmOrder); } else { orderManager.EditOrder(response.Orders); prompt.PrintSuccessMessage("Order edited successfully."); } } } } }
// Takes the order date, the order to be edited, and a copy of the same order to be used in printing old information vs new public ValidateEditOrderResponse ValidateEditOrder(string orderDate, Order order) { ValidateEditOrderResponse response = new ValidateEditOrderResponse { Orders = _loadedOrders }; // Checks the customer name is valid if (!IsValidCustomerName(order.CustomerName)) { response.Success = false; response.Code = ErrorCode.NameIsInvalid; return(response); } // Checks that the area isn't less than the min if (order.Area < ImportantValues.MIN_AREA) { response.Success = false; response.Code = ErrorCode.AreaTooSmall; return(response); } // LoadOrders will return null if it couldn't find a file if (response.Orders == null) { response.Success = false; response.Code = ErrorCode.CouldNotFindFile; return(response); } // LoadOrders will return a corrupt order with number -1 if the file is corrupt if (response.Orders.Any(x => x.Number == -1)) { response.Success = false; response.Code = ErrorCode.CorruptFile; return(response); } // Checks the order number against all the orders that are in the list to see if there's a match. if (!response.Orders.Any(x => x.Number == order.Number)) { response.Success = false; response.Code = ErrorCode.OrderNumberDoesNotExist; return(response); } // Get the product information and then validate it _product = GetProductInfo(order.ProductType); _validation = ValidateProduct(_product); if (_validation != ErrorCode.NoError) { response.Success = false; response.Code = _validation; return(response); } // Get the state information and then validate it _state = GetStateInfo(order.State); _validation = ValidateState(_state.Abbreviation); if (_validation != ErrorCode.NoError) { response.Success = false; response.Code = _validation; return(response); } // Assign all the information pulled from the product and state to the edited order and perform calculations order.TaxRate = GetStateInfo(order.State).TaxRate; order.CostPerSquareFoot = _product.CostPerSquareFoot; order.LaborCostPerSquareFoot = _product.LaborCostPerSquareFoot; order = PerformCalculation(order); response.Success = true; // Gets the index based off of the order number and sets the order to the order in the list int index = response.Orders.FindIndex(x => x.Number == order.Number); response.Orders[index] = order; return(response); }