Answer to Question #189388 in C# for CHANDRASENA REDDY

Question #189388

Create ProductMock Entity which throws DataEntryException when its properties initialize with following values. Get the Product details from the user and handle in-built and user defined exception. Refer the class diagram given below :


Condition | Exception Message

__________________________________________________________________________________

productID <=0 | Product ID must be greater than zero productName = = “” | Product Name cannot be left blank

price <=0 | Price of product must be greater than zero.

productName | Product Name should have alphabets and numbers only


Product Mock:

ProductId: int

ProductName: String

Price: Double


Output:

Enter Id: -32

Enter Product Name: Compaq Laptop

Enter Price: 32000


Product Id must be greater than 0


1
Expert's answer
2021-05-09T23:37:59-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace C_SHARP
{
    class Program
    {


        class DataEntryException: Exception {
            public DataEntryException(string message)
                : base(message)
            {
                
            }
        }
        class ProductMock {
            private int productId;


            public int ProductId
            {
                get { return productId; }
                set { 
                    //productID <=0 | Product ID must be greater than zero 
                    if (value <= 0) {
                        throw new DataEntryException("Product ID must be greater than zero.");
                    }
                    productId = value; 
                }
            }
            private string productName;


            public string ProductName
            {
                get { return productName; }
                set { 
                    //productName = = "" | Product Name cannot be left blank
                    if (value == "")
                    {
                        throw new DataEntryException("Product Name cannot be left blank.");
                    }
                    //productName | Product Name should have alphabets and numbers only
                    bool isLetterorNumbers = true;
                    for (int i = 0; i < value.Length; i++)
                    {
                        if (!char.IsLetter(value[i]) && !char.IsDigit(value[i]))
                        {
                            isLetterorNumbers = false;
                            break;
                        }
                    }
                    if (!isLetterorNumbers)
                    {
                        throw new DataEntryException("Product Name should have alphabets and numbers only.");
                    }
                    productName = value; 
                }
            }
            private double price;


            public double Price
            {
                get { return price; }
                set {
                    //price <=0 | Price of product must be greater than zero.
                    if (price <=0)
                    {
                        throw new DataEntryException("Price of product must be greater than zero.");
                    }
                    price = value; 
                }
            }




        }


        static void Main(string[] args)
        {


            try
            {
                int productId;
                string productName;
                double price;
                ProductMock productMock = new ProductMock();


                Console.Write("Enter Id: ");
                productId = int.Parse(Console.ReadLine());
                Console.Write("Enter Product Name: ");
                productName = Console.ReadLine();
                Console.Write("Enter Price: ");
                price = double.Parse(Console.ReadLine());


                productMock.ProductId = productId;
                productMock.ProductName = productName;
                productMock.Price = price;
            }
            catch (DataEntryException ex)
            {
                Console.WriteLine("\n"+ex.Message);
            }
           

            Console.ReadKey();


        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS