Home > Software engineering >  С# How to set parent`s fields and properties without a lot of overloads of constructors?
С# How to set parent`s fields and properties without a lot of overloads of constructors?

Time:06-20

I have parent class Product and child class DairyProduct and i want to set all fields and properies of parent class in child class, but i do not want to do a lot of new constructors. How to set properties (name, price and weight) in DairyProduct without a new constructor?

Also codes for example:

Parent class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Task1
{
    public class Product
    {
        private string _name;
        public string Name { get => _name; set => _name = value; }

        private float _price;
        public float Price
        {
            get => _price;

            set => _price = value < 0 ? throw new ArgumentException() : _price = value; 
        }

        private float _weight;
        public float Weight
        {
            get { return _weight; }

            set { _weight = value < 0 ? throw new ArgumentException() : _weight = value; }
        }

        public Product() : this(null, default, default) {}

        public Product(string name, float price, float weight) 
        {
            Name = name;
            Price = price;
            Weight = weight;
        }

        public virtual void Discount(float percentage) 
        {
            if (percentage <= 100) Price = Price * (100 - percentage) / 100;
            else throw new ArgumentException();
        
        }

        public override string ToString() 
        {
            return string.Format("Name: {0}; Price: {1}; Weight: {2};", Name, Price, Weight);
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(Product))
            {
                return false;
            }
            else 
            {
                Product p = (Product)obj;
                return p.Name == Name && p.Price == Price && p.Weight == Weight; 
            }
        }
    }
}

Child class

using System;

namespace Task1
{
    class DairyProduct : Product
    {
        public readonly DateTime ExpirationDate;
        private int _daysToExpiration;
        private bool _isFresh;

        public DairyProduct()
        {
            _daysToExpiration = 90;
            ExpirationDate = DateTime.Now.AddDays(_daysToExpiration);
            _isFresh = true;
        }
        public DairyProduct(int daysToExpiration)
        {
            ExpirationDate = DateTime.Now.AddDays(daysToExpiration);
            _isFresh = true;
        }

        public DairyProduct(DateTime experationDate, int daysToExpiration) 
        {
            _daysToExpiration = daysToExpiration;
            ExpirationDate = experationDate;
            CheckExpirationDate();
        }

        public void CheckExpirationDate() => _isFresh = (ExpirationDate - DateTime.Now).Days > 0;

        public override void Discount(float percentage)
        {
            CheckExpirationDate();
            if (_isFresh)
            {
                float discontPersentage = (float)(1 - ((ExpirationDate - DateTime.Now).Days) / _daysToExpiration)   percentage / 100;
                if (discontPersentage >= 1) 
                { 
                    Price = 0;
                }
                else
                {
                    Price *= discontPersentage;
                }
            }
            else 
            {
                Price = 0;
            }
        }

        public override string ToString()
        {
            return string.Format("Name: {0}; Price: {1}; Weight: {2}; Expiration date: {3}", Name, Price, Weight, ExpirationDate);
        }
    }
}

CodePudding user response:

You can include the parent properties with the child constructor parameters. Then call the base() method to initialise the parent properties.

    public DairyProduct(string name, float price, float weight) :
                                             base(name, price, weight)
    {
        _daysToExpiration = 90;
        ExpirationDate = DateTime.Now.AddDays(_daysToExpiration);
        _isFresh = true;
    }

    public DairyProduct(string name, float price, 
                        float weight, int daysToExpiration) : 
                                           base(name, price, weight)
    {
        ExpirationDate = DateTime.Now.AddDays(daysToExpiration);
        _isFresh = true;
    }

    public DairyProduct(string name, float price, float weight,                       
                        DateTime experationDate, int daysToExpiration) : 
                            base(name, price, weight)
    {
        _daysToExpiration = daysToExpiration;
        ExpirationDate = experationDate;
        CheckExpirationDate();
    }
  • Related