Hello, I am new to this field and therefore I am still learning simple things.

I make a program to store information about products in one catalog, but I don’t want to be able to put a negative price on a product or make a negative number of products in stock.

Here is my code:

public enum ProductCategory { Electric, Household, Garden, Miscellaneous } class Product { // Properties private int productID; private string productName; private double unitPrice; private double unitsInStock; // Get Product Categories public ProductCategory category { get; } // Validator for Unit Price public double UnitPrice { get { return unitPrice; } set { unitPrice = value > 0.0 ? value : 0.0; } } // Validator for Units In Stock public double UnitsInStock { get { return unitsInStock; } set { unitsInStock = value > 0.0 ? value : 0.0; } } // Constructor public Product(int productID, string productName, ProductCategory category, double unitPrice, double unitsInStock) { this.productID = productID; this.productName = productName; this.category = category; this.unitPrice = unitPrice; this.unitsInStock = unitsInStock; } // Default Constructor with Chaining public Product(int productID, string productName) : this(productID, productName, ProductCategory.Miscellaneous, 0.0, 0.0) { } // Override which returns a string with full product information public override string ToString() { return "The product " + productName + " with ID " + productID + " costs " + unitPrice + " euro per unit. We have " + unitsInStock + " items left in our " + category + " stock."; } } //Testing the app class TestProduct { static void Main(string[] args) { // Assigning correct properties to the product Product p1 = new Product(1234567, "Cake", ProductCategory.Miscellaneous, 7.5, 150); Product p2 = new Product(2345678, "Drill", ProductCategory.Household, -23, 2); Product p3 = new Product(3456789, "Shovel", ProductCategory.Garden, 12.7, -10); Console.WriteLine(p1); Console.WriteLine(p2); Console.WriteLine(p3); Console.ReadLine(); } } 

Here is what the console issues:

 The product Cake with ID 1234567 costs 7.5 euro per unit. We have 150 items left in our Miscellaneous stock. The product Drill with ID 2345678 costs -23 euro per unit. We have 2 items left in our Household stock. The product Shovel with ID 3456789 costs 12.7 euro per unit. We have -10 items left in our Garden stock. 

As you can see, I have a price for Drill -23 euros, and the amount of Shovel in stock -10.

How to make it so that with negative numbers it returns 0?

  • four
    When entering negative values, it is necessary to check this and give an error to the user, and not silently change to zero or take a module. - teran
  • I support, quiet swallowing of mistakes is fraught with fat problems. An incorrect value should result in an exception. - VladD 2:49 pm
  • @teran please share how to give an error, and not to change to 0. - Rufat
  • @VladD there are more problems with design. In theory, these checks should be redundant. But this is an educational project. - Trymount 2:53 pm
  • @VladD please share how to improve this code. - Rufat

3 answers 3

You already have it all done. Just in the constructor, assign values ​​not to the fields of the method, but to the properties:

 //this.unitPrice= unitPrice; this.UnitPrice= unitPrice;` 
  • Thank you missed. Everything is working. - Rufat

I'll put in my five kopecks. For the correct handling of input errors by the user of the program in .Net there is IDataErrorInfo and a newer version on the same subject INotifyDataErrorInfo , I highly recommend reading the documentation about these interfaces.

  • when you send to read the documentation, leave links to go quickly - rdorn
  • @rdorn OK, I will consider. - Bulson

In addition to the existing good answers: tacit suppression of the problem is not a good idea, because while errors in other parts of the program go unnoticed.

There are two approaches. First, it checks the compile time. Suppose you have an integer field and you want it to take only positive values. Then it often makes sense to declare it not as an int , but as a uint (although the CLS compilance will be violated), so that negative values ​​simply cannot get there.

If checking during compilation is impossible or impractical (for example, you have requirements for CLS compliancy, or as in your case, a floating point type that cannot be limited at compile time), it makes sense to throw an exception with incorrect parameters. Thus, any code that tries to load incorrect values ​​will result in the exclusion and crash of the program during the testing phase.

In this case, your code will look something like this:

 public double UnitPrice { get { return unitPrice; } set { if (value < 0.0) // или <= в зависимости от того, какая логика вам нужна throw new ArgumentException("Unit price cannot be negative"); unitPrice = value; } }