Answer to Question #240037 in C# for Gayatri

Question #240037

You have created Product class in Lab 1. You need to extend this class and create two derived classes from this class. The derived classes will be DurableProducts (eg: cars, furniture, computers) and NonDurableProducts(Food and Beverages)

The DurableProducts class will have VAT(value added tax) as an additional property. The NonDurableProducts will have ExpiryDate and Discount properties as additional properties.Task 1: Create these two classes by inheriting from the Product class.

Task 2: Override the GetTotalCost () Method in these two classes. For DurableProducts the new total cost will be (Cost + VAT)*Quantity. For NonDurableProducts the new total cost will be (Cost-Discount)*Quantity.Task 3: Create a console application to use these classes. Create a Menu driven application to select the Type of Product. Based on the user selection create the object and accept the details from the user.

Also display the total cost of the Product.


1
Expert's answer
2021-09-21T11:02:24-0400


using System;
using System.Collections.Generic;
namespace App
{
    class Product {
        public string Name { get; set; }
        public double Cost { get; set; }
        public int Quantity { get; set; }
        public virtual  double GetTotalCost() {
            return Cost * Quantity; 
        }
    }


    class DurableProducts: Product
    {
        public double VAT { get; set; }
        public override double GetTotalCost()
        {
            return (Cost + VAT) * Quantity;
        }
    }


    class NonDurableProducts : Product
    {
        public string ExpiryDate { get; set; }
        public double Discount { get; set; }
        public override double GetTotalCost()
        {
            return (Cost - Discount) * Quantity;
        }
    }




    class Program
    {
        static void Main(string[] args)
        {


            int ch = -1;


            while (ch != 6) {
                Console.WriteLine("1. Add product");
                Console.WriteLine("2. Add DurableProducts");
                Console.WriteLine("3. Add NonDurableProducts");
                Console.WriteLine("4. Exit");
                Console.Write("Your choice: ");
                ch = int.Parse(Console.ReadLine());
                switch (ch) { 
                    case 1:
                        Product product = new Product();
                        Console.Write("Enter a product name: ");
                        product.Name = Console.ReadLine();
                        Console.Write("Enter a product cost: ");
                        product.Cost = double.Parse(Console.ReadLine());
                        Console.Write("Enter a product quantity: ");
                        product.Quantity = int.Parse(Console.ReadLine());
                        Console.WriteLine("The total cost is: {0}", product.GetTotalCost());
                        break;
                    case 2:
                        DurableProducts durableProducts = new DurableProducts();
                        Console.Write("Enter a DurableProduct name: ");
                        durableProducts.Name = Console.ReadLine();
                        Console.Write("Enter a DurableProduct cost: ");
                        durableProducts.Cost = double.Parse(Console.ReadLine());
                        Console.Write("Enter a DurableProduct quantity: ");
                        durableProducts.Quantity = int.Parse(Console.ReadLine());
                        Console.Write("Enter a DurableProduct VAT: ");
                        durableProducts.VAT = double.Parse(Console.ReadLine());
                        Console.WriteLine("The total cost is: {0}", durableProducts.GetTotalCost());
                        break;
                    case 3:
                        NonDurableProducts nonDurableProducts = new NonDurableProducts();
                        Console.Write("Enter a NonDurableProducts name: ");
                        nonDurableProducts.Name = Console.ReadLine();
                        Console.Write("Enter a DurableProduct Expiry Date: ");
                        nonDurableProducts.ExpiryDate = Console.ReadLine();
                        Console.Write("Enter a NonDurableProducts cost: ");
                        nonDurableProducts.Cost = double.Parse(Console.ReadLine());
                        Console.Write("Enter a NonDurableProducts quantity: ");
                        nonDurableProducts.Quantity = int.Parse(Console.ReadLine());
                        Console.Write("Enter a NonDurableProducts Discount: ");
                        nonDurableProducts.Discount = double.Parse(Console.ReadLine());
                        Console.WriteLine("The total cost is: {0}", nonDurableProducts.GetTotalCost());
                        break;
                    case 4:
                        break;
                }
            }
                    
            Console.ReadLine();
        }
    }
}

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