The code provided is a class library vs an exectuable app. You will need units tests to verify your refactored code works.

computer science

Description

Advanced .Net Development with C# 

Homework Assignment # 3



Single Responsibility Principle (SRP)

Refractor the order class (order.cs) to comply with SRP. Project is provided in HW3EX1B4.zip

The code provided is a class library vs an exectuable app. You will need units tests to verify your refactored code works. Create a separate project (HW3EX1.Tests) and write unit tests for your all classes in your refectored solution.

Screencast code walkthrough

Talk me through how the original code violated SRP. There are multiple reasons.

Talk me through your strategy to make the code SRP compliant.

Talk me through your unit tests and show me that they pass.


Hints:

Cash Transactions don’t need credit card processing

Point of Sale (POS) transactions don’t need inventory reservations

POS transactions don’t need email notifications

Any change in notifications, credit card processing, or inventory management will affect Order as well as the Web and POS implementations.


Open Closed Principle (OCP)

Refractor the cart class (cart.cs) to comply with OCP. Project is provided in HW3EX2B4.zip

The code provided is a class library vs an exectuable app. You will need units tests to verify your refactored code works. Create a separate project (HW3EX2.Tests) and write unit tests for your all classes in your refectored solution

Screencast code walkthrough

Talk me through how the original code violated OCP. There are multiple reasons.

Talk me through your strategy to make the code OCPP compliant.

Talk me through your unit tests and show me that they pass.


Hints:

Adding new rules require changed to the calculator every time

Each change can introduce bugs and requires re-testing etc.

We ant to avoid introducing changes that cascade through many modules in our application

Writing new classes is less likely to introduce problems

Nothing depends on new classes

New classes have no legacy coupling to make them hard to design or test


Liskov Substitution Principle (LSP)

States that subtypes(child classes) must be substitutable for base types (parent classes)

This unit test fails  public void TwentyFor4X5RectangleFromSquare() in the attached code file (HW3EX3Code.txt).




AreaCalculator, Rectangle, and Square classes are below the test methods.

Rework the classes and make them honor LSP and pass all unit tests.

You may not change any of the code in the unit tests.


Hints:

Non-substitutable code breaks polymorphism

Client code expects child classes to work in place of their base classes

“fixing” substitutability problems by added if-then or swith statements quickly becomes a maintenance nightmare (and violates OCP).

Your solution should pass the following additional unit tests


       [TestMethod]

        public void TwentyFor4X5ShapeFromRectangle()

        {

            Shape myShape = new Rectangle(){Height = 4, Width = 5};

            Assert.AreEqual(20, myShape.Area());

        }


        [TestMethod]

        public void TwentyFor4X5ShapeFromRectangleAnd9For3X3Square()

        {

            var shapes = new List<Shape>

                             {

                                 new Rectangle {Height = 4, Width = 5},

                                 new Square {SideLength = 3}

                             };

            var areas = new List<int>();

            foreach (Shape shape in shapes)

            {

                areas.Add(shape.Area());

            }

            Assert.AreEqual(20, areas[0]);

            Assert.AreEqual(9, areas[1]);

        }

Instruction Files

Related Questions in computer science category