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

bookChallenge: Safe Parsing for User Input

Tâche

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.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 4
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

bookChallenge: Safe Parsing for User Input

Glissez pour afficher le menu

Tâche

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.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 4
single

single

some-alt