0

I have subcategories defined under Vehicle Class as :- Bike Car truck bus

Further, there are different variants under each four categories

  1. Bike :- sport, standard
  2. Car :- sport, standard, electric 3.Truck :- Mini Truck , Power Truck
  3. Bus:- Mini Bus, AC Bus

We have to calculate the price of each variants defined in each category.

Example 1 : With Constructor overloading

 public interface ivehicle
    {
        public void calculateprice();
    }

    public class calculatprice : ivehicle
    {
        public void calculateprice() { 
        
        }
    }

    public class bike_1
    {
        public readonly ivehicle vehicle;
        public bike_1(string model) {
            if (model == "STANDARD")
            {
                Console.WriteLine("THe price of standard is 1 lac");
            }
            else if (model == "SPORTS")
            {
                Console.WriteLine("The price of sports is 2 lacs");
            }
        
        }
    }

Example 2:- With using overiding function and inheritance

    public class vehicle_3
    {
       
    }

    public class bike_3: vehicle_3
    
    {
        public virtual void Calculateprice_3() { }

    }

    public class sports_3 :bike_3
    {

        public override void Calculateprice_3()
        {
            base.Calculateprice_3();
            Console.WriteLine("The price of Sports is 2 lacs");
        }

    }

    public class standard_3 : bike_3
    {
        public override void Calculateprice_3()
        {
            base.Calculateprice_3();
            Console.WriteLine("The price of standard is 1 lacs");
        }
    }

Example 3:- With interface

    public class vehicle_4
    {

    }

    public interface ivehicle_4
    {
        void CalculatePrice_4();
    }

    public class bike_4 :ivehicle_4

    {
        public virtual void CalculatePrice_4() { }

    }
    public class sports_4 : bike_4
    {

        public override void CalculatePrice_4()
        {
            base.CalculatePrice_4();
            Console.WriteLine("The price of Sports is 2 lacs");
        }

    }

    public class standard_4 : bike_4
    {
        public override void CalculatePrice_4()
        {
            base.CalculatePrice_4();
            Console.WriteLine("The price of standard is 1 lacs");
        }
    }
  • Please limit to one example, and provide some details on what is going wrong (the output). – ryanwebjackson Dec 27 '22 at 21:04
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 27 '22 at 21:04
  • @ryanwebjackson , I have following statement a. I have Bike, Car, truck, bus (4 entities) as vehicle . b. Then I have following categories defined under each category : Bike :- sport, standard Car :- sport, standard, electric Truck :- Mini Truck , Power Truck Bus:- Mini Bus, AC Bus Problem Statement : I have to calculate the price of Sports Bike, standard bike by implementing dependency injection and dependency inversion principle. Solution:- What should be the best possible structure – jagjot singh Wadali Dec 28 '22 at 06:10
  • That's not a good format for a StackOverflow question, because it is largely opinion based. I will say that in class-based OOP, analyze the "x is a y" relationships to determine what should inherit from what. Interfaces are a formal abstraction are for how you want to interact with class instances, and abstract classes are for hierarchy shared, generic functionality. There is a lot of information on the Web and in books about this. – ryanwebjackson Dec 28 '22 at 15:18

0 Answers0