using System.Text; namespace Pizza { internal class CustomerMap { private readonly int _mapSize; private readonly string[,] _customerMap; public CustomerMap(int mapSize) { _mapSize = mapSize; _customerMap = GenerateCustomerMap(); } /// /// Gets customer on position X, Y. /// /// Represents X position. /// Represents Y position. /// If positions is valid then returns customer name otherwise returns empty string. public string GetCustomerOnPosition(int x, int y) { if(IsPositionOutOfRange(x, y)) { return string.Empty; } return _customerMap[y, x]; } /// /// Overridden ToString for getting text representation of customers map. /// /// Text representation of customers map. public override string ToString() { int verticalSpace = 4; int horizontalSpace = 5; var mapToDisplay = new StringBuilder(); AppendXLine(mapToDisplay, horizontalSpace); for (int i = _customerMap.GetLength(0) - 1; i >= 0; i--) { mapToDisplay.AppendLine("-", verticalSpace); mapToDisplay.Append($"{i + 1}"); mapToDisplay.Append(' ', horizontalSpace); for (var j = 0; j < _customerMap.GetLength(1); j++) { mapToDisplay.Append($"{_customerMap[i, j]}"); mapToDisplay.Append(' ', horizontalSpace); } mapToDisplay.Append($"{i + 1}"); mapToDisplay.Append(' ', horizontalSpace); mapToDisplay.Append(Environment.NewLine); } mapToDisplay.AppendLine("-", verticalSpace); AppendXLine(mapToDisplay, horizontalSpace); return mapToDisplay.ToString(); } /// /// Checks if position is out of range or not. /// /// Represents X position. /// Represents Y position. /// True if position is out of range otherwise false. private bool IsPositionOutOfRange(int x, int y) { return x < 0 || x > _mapSize - 1 || y < 0 || y > _mapSize - 1; } /// /// Generates array which represents customers map. /// /// Returns customers map. private string[,] GenerateCustomerMap() { string[,] customerMap = new string[_mapSize, _mapSize]; string[] customerNames = GetCustomerNames(_mapSize * _mapSize); int currentCustomerNameIndex = 0; for (int i = 0; i < customerMap.GetLength(0); i++) { for (int j = 0; j < customerMap.GetLength(1); j++) { customerMap[i, j] = customerNames[currentCustomerNameIndex++].ToString(); } } return customerMap; } /// /// Generates customer names. Names are represented by alphanumerics from 'A'. Name of last customer depends on passed parameter. /// /// How many customers need to be generated. /// List of customer names. private static string[] GetCustomerNames(int numberOfCustomers) { // returns ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"]; return Enumerable.Range(65, numberOfCustomers).Select(c => ((Char)c).ToString()).ToArray(); } /// /// Appends line with X coordinates. /// /// Current map where a new line will be appended. /// Number of horizontal delimiters which will be added between each coordination. private void AppendXLine(StringBuilder mapToDisplay, int horizontalSpace) { mapToDisplay.Append(' '); mapToDisplay.Append('-', horizontalSpace); for (var i = 0; i < _customerMap.GetLength(0); i++) { mapToDisplay.Append($"{i + 1}"); mapToDisplay.Append('-', horizontalSpace); } mapToDisplay.Append(Environment.NewLine); } } }