Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Safe Parsing for User Input | Validating Input and Safe Parsing
C# Exceptions and Error Handling Practice

bookChallenge: Safe Parsing for User Input

Tarea

Swipe to start coding

Implement a method that safely parses string input for a product price and quantity. Your method should use TryParse to convert the price to a decimal and the quantity to an int, and return a message indicating the result.

  • Attempt to parse priceInput as a decimal.
  • Attempt to parse quantityInput as an int.
  • If both values are invalid, return "Invalid price and quantity input.".
  • If only priceInput is invalid, return "Invalid price input.".
  • If only quantityInput is invalid, return "Invalid quantity input.".
  • If both values are valid, return a message in the format: "Total cost for {quantity} items at {price:C} each is {total:C}.", where {price} is the parsed decimal, {quantity} is the parsed int, and {total} is the product of price and quantity.

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

close

bookChallenge: Safe Parsing for User Input

Desliza para mostrar el menú

Tarea

Swipe to start coding

Implement a method that safely parses string input for a product price and quantity. Your method should use TryParse to convert the price to a decimal and the quantity to an int, and return a message indicating the result.

  • Attempt to parse priceInput as a decimal.
  • Attempt to parse quantityInput as an int.
  • If both values are invalid, return "Invalid price and quantity input.".
  • If only priceInput is invalid, return "Invalid price input.".
  • If only quantityInput is invalid, return "Invalid quantity input.".
  • If both values are valid, return a message in the format: "Total cost for {quantity} items at {price:C} each is {total:C}.", where {price} is the parsed decimal, {quantity} is the parsed int, and {total} is the product of price and quantity.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 4
single

single

some-alt