static void Main(string[] args) { // Variables // double basePrice = 10; // double pricePerTopping = 1; PriceDefinition priceDefinition = new PriceDefinition { BasePrice = 10, PricePerUnit = 1 }; TaxCalculatorFactory taxFactory = new TaxCalculatorFactory(); // User input Console.WriteLine("Enter your province and press enter:"); string province = Console.ReadLine(); Console.WriteLine("Enter the number of toppings and press enter:"); string toppings = Console.ReadLine(); // Calculation int numberOfToppings = int.Parse(toppings); var pizza = new Pizza(taxFactory.GetTaxCalculator(province), numberOfToppings, priceDefinition); double total = pizza.CalculatePrice(); // Output Console.WriteLine($"Order total is: {total}"); Console.WriteLine("Press any key to exit"); Console.Read(); }
public double CalculatePrice(int numberOfToppings, PriceDefinition priceDefinition) { double price = priceDefinition.BasePrice; price = numberOfToppings * priceDefinition.PricePerUnit + price; price = _taxCalculator.GetTotalAfterTax(price); return(price); }
public void Test1() { PriceDefinition priceDefinition = new PriceDefinition() { BasePrice = 9, PricePerUnit = 1 }; Pizza p = new Pizza(new TaxCalculatorFactory().GetCalculator("AB")); double total = p.CalculatePrice(2, priceDefinition); double expected = 11.55; Assert.Equal(expected, total); }
public void CalculatesAlbertaTax() { //Arrange - where we create variables PriceDefinition priceDefinition = new PriceDefinition { BasePrice = 9, PricePerUnit = 1 }; Pizza p = new Pizza(new AlbertaTaxCalculator(), 1, priceDefinition); //Act - where action is performed double total = p.CalculatePrice(); //Assert double expected = 10.5; Assert.Equal(expected, total); }
/// <summary> /// Gets the price of the offer. /// </summary> /// <param name='offerId'> /// Required. the full offer ID /// /delegatedProviders/{providerId}/offers/{offerId}. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Offer price result. /// </returns> public async Task <OfferGetPriceResult> GetPriceAsync(string offerId, CancellationToken cancellationToken) { // Validate if (offerId == null) { throw new ArgumentNullException("offerId"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("offerId", offerId); TracingAdapter.Enter(invocationId, this, "GetPriceAsync", tracingParameters); } // Construct URL string url = ""; url = url + Uri.EscapeDataString(offerId); url = url + "/estimatePrice"; List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=" + Uri.EscapeDataString(this.Client.ApiVersion)); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Post; httpRequest.RequestUri = new Uri(url); // Set Headers // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.NoContent) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result OfferGetPriceResult result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.NoContent) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new OfferGetPriceResult(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { PriceDefinition priceInstance = new PriceDefinition(); result.Price = priceInstance; JToken amountValue = responseDoc["amount"]; if (amountValue != null && amountValue.Type != JTokenType.Null) { decimal amountInstance = ((decimal)amountValue); priceInstance.Amount = amountInstance; } JToken currencyCodeValue = responseDoc["currencyCode"]; if (currencyCodeValue != null && currencyCodeValue.Type != JTokenType.Null) { string currencyCodeInstance = ((string)currencyCodeValue); priceInstance.CurrencyCode = currencyCodeInstance; } JToken captionValue = responseDoc["caption"]; if (captionValue != null && captionValue.Type != JTokenType.Null) { string captionInstance = ((string)captionValue); priceInstance.Caption = captionInstance; } } } result.StatusCode = statusCode; if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
public Pizza(ITaxCalculator taxCalculator, int noOfToppings, PriceDefinition priceDef) { _taxCalculator = taxCalculator; numberOfToppings = noOfToppings; priceDefinition = priceDef; }