Answer to Question #180517 in C# for Miley

Question #180517

Create a Product-Inventory software (No need of user Input or any other validations)

Product: (Override Equals or implement IEquatable<T>)

1.       Id

2.       Price

3.       IsDefective

Inventory:

1.        Dictionary containing all the products and their respective quantities in the inventory

2.       Total value of the inventory

Methods

1.       Add Product

2.       Remove Product

3.       Update Product Quantity

 

On change of Product’s Price, Inventory total value should get updated

If a Product becomes defective, remove it from the inventory.

(Handle them through EventHandler)



1
Expert's answer
2021-04-18T13:52:22-0400
    public class PriceChangedEventArgs : EventArgs {
        public float Difference { get; private set; }
        public PriceChangedEventArgs(float difference) {
            Difference = difference;
        }
    }

    public class Product {
        public event EventHandler ChangeDefectiveness;
        public event EventHandler<PriceChangedEventArgs> ChangePrice;

        int id;
        float price;
        bool isDefective;

        public int GetId() {
            return id;
        }

        public float GetPrice() {
            return price;
        }

        public void SetPrice(float value) {
            PriceChangedEventArgs e = new PriceChangedEventArgs (value - price);
            price = value;
            ChangePrice (this, e);
        }

        public bool GetIsDefective() {
            return isDefective;
        }

        public void SetIsDefective(bool value) {
            isDefective = value;
            ChangeDefectiveness (this, null);
        }

        public Product(int id, float price, bool isDefective) {
            this.id = id;
            this.price = price;
            this.isDefective = isDefective;
        }

        public override bool Equals(Object obj)
        {
            if ((obj == null) || !this.GetType().Equals(obj.GetType()))
                return false;
            else {
                Product p = (Product) obj;
                return (this.id == p.GetId()) && (this.price == p.GetPrice())
                    && (this.isDefective == p.GetIsDefective());
            }
        }

        public override int GetHashCode()
        {
            return this.id;
        }

        public override string ToString ()
        {
            return $"Product: {id} costs {price}";
        }
    }

    public class Inventory {
        public float Total { get; set; }
        public Dictionary<Product,int> Products { get; set; }

        public Inventory() {
            Total = 0;
            Products = new Dictionary<Product,int> ();
        }

        public void AddProduct (Product p) {
            if (!p.GetIsDefective()) {
                if (Products.ContainsKey (p))
                    Products [p] += 1;
                else
                    Products.Add (p, 1);
                Total += p.GetPrice ();
                p.ChangeDefectiveness += new EventHandler (OnChangeDefectiveness);
                p.ChangePrice += new EventHandler<PriceChangedEventArgs> (OnChangePrice);
            }
        }

        public void RemoveProduct (Product p) {
            if (Products.ContainsKey (p)) {
                Total -= Products [p] * p.GetPrice ();
                Products.Remove (p);
            }
        }

        public void UpdateProductQuantity (Product p, int amount) {
            if (Products.ContainsKey (p)) {
                Total += (amount - Products [p]) * p.GetPrice ();
                Products [p] = amount;
            }
        }

        private void OnChangeDefectiveness (object sender, EventArgs e) {
            Product p = (Product)sender;
            RemoveProduct (p);
        }

        private void OnChangePrice (object sender, PriceChangedEventArgs e) {
            Product p = (Product)sender;
            Total += Products [p] * e.Difference;
        }
    }

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