Answer to Question #189379 in C# for CHANDRASENA REDDY

Question #189379

ABC Corp wants to maintain list of Customers. While accepting the data, you need to validate CreditLimit property. If the value is invalid, you need to raise Exception. We need to implement custom exception class to implement the same.


Task 1: Define a Customer class with following members CustomerId, Customer Name, Address, City, Phone, CreditLimit


Task 2: Define the properties for all these members.


Task 3: Define two constructors (Default and Parameterised) to assign the values.


Task 4: You need to validate the CreditLimit. If the value is above 50000, then you need to raise Exception to handle this. Create InvalidCreditLimit custom Exception class to achieve the same.


Task 5: Use the Exception class created to throw the exception. Ensure that the Client application catches the exception and handles the error properly.


1
Expert's answer
2021-05-08T23:16:38-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace C_SHARP
{
    class Program
    {
         //Create InvalidCreditLimit custom Exception class to achieve the same.
        class InvalidCreditLimit:Exception 
        {
            public InvalidCreditLimit(string message):base(message) {
            
            }
        }
        //Task 1: Define a Customer class with following members CustomerId, Customer Name, Address, City, Phone, CreditLimit
        class Customer {
            private int id;


            public int Id
            {
                get { return id; }
                set { id = value; }
            }
            private string name;
            //Task 2: Define the properties for all these members.
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private string address;


            public string Address
            {
                get { return address; }
                set { address = value; }
            }
            private string city;


            public string City
            {
                get { return city; }
                set { city = value; }
            }
            private string phone;


            public string Phone
            {
                get { return phone; }
                set { phone = value; }
            }
            private decimal creditLimit;


            public decimal CreditLimit
            {
                get { return creditLimit; }
                set {
                    //Task 4: You need to validate the CreditLimit. 
                    //If the value is above 50000, then you need to raise Exception to handle this.
                    if (value > 50000) {
                        //Task 5: Use the Exception class created to throw the exception
                        throw new InvalidCreditLimit("The value is above 50000.");
                    }
                    creditLimit = value; 
                }
            }


            //Task 3: Define two constructors (Default and Parameterised) to assign the values.
            public Customer() { }


            public Customer(int id, string name, string address, string city, string phone, decimal creditLimit) { 
                this.id=id;
                this.name = name;
                this.address = address;
                this.city = city;
                this.phone = phone;
                this.CreditLimit = creditLimit;
            }
       
        }


       static void Main(string[] args)
        {
            //the Client application catches the exception and handles the error properly.
            try
            {
                
                Customer customer1 = new Customer(1, "Mary Clark", "Main Road 24", "London", "045523658", 2522);
                //Display CustomerId, Customer Name, Address, City, Phone, CreditLimit
                Console.WriteLine("Customer Id: {0}", customer1.Id);
                Console.WriteLine("Customer Name: {0}", customer1.Name);
                Console.WriteLine("Customer Address: {0}", customer1.Address);
                Console.WriteLine("Customer City: {0}", customer1.City);
                Console.WriteLine("Customer Phone: {0}", customer1.Phone);
                Console.WriteLine("Customer CreditLimit: {0}", customer1.CreditLimit);
                Customer customer2 = new Customer(2, "Peter Smith", "Main Road 245", "London", "056999874", 252002);
            }
            catch (InvalidCreditLimit ex) {
                Console.WriteLine(ex.Message);
            }
            
            
            Console.ReadLine();
        }
    }
}






Example:


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
New on Blog
APPROVED BY CLIENTS