locked
need help to get started for online shopping cart checkout application with TDD RRS feed

  • Question

  • User569149469 posted

    Hi,

    I have an application where we have diff products I need to develope

    an checkout for online application

    The initial product catalogue will contain the following items

    prod  Name         Price

    pd1   testprod1   $500.00

    pd2   testProd2  $1099.99

    pd3    testprod3 $149.50

    pd4   testprod4   $30.00

    there are some offers on  the products

    There will be a 3 for 2 deal on pd3 - for example, if you purchase 3
    pd3, you will only pay for 2
     The pd1 will have a bulk discount applied where the price drops to
    $449.99 each if someone buys more than 4
     A pd4 is bundled free of charge with every pd2 sold

    The interface to the Checkout looks like this (shown in C#):

    var co = new Checkout(priceRules);

    co.ScanProduct(item1);

    co.ScanProduct(item2);

    co.Total();

    how to get started on this application using TDD.Any help is highly appreciated.

    Tuesday, January 8, 2019 7:32 AM

Answers

  • User-893317190 posted

    Hi phmaspnet,

    TTD  starts with test instead of implementing your logic of your code.

    For example , prepare a interface.

     public interface IDiscount
        {
            double Discount(int number, double price);
        }

    And prepare three kinds of discount which has no implements.

     public class Pd3Discount : IDiscount
        {
            public int Numerator { get; set; }
            public int Denominator { get; set; }
            
            public Pd3Discount(int numerator, int denominator)
            {
                this.Numerator = numerator;
                this.Denominator = denominator;
            }
            public double Discount(int number, double price)
            {
                return 0;
            }
        }
    
        public class Pd1Discount : IDiscount
        {
            public double DropTo { get; set; }
            public  int DropStart { get; set; }
            public Pd1Discount(double dropTo, int dropStart)
            {
                this.DropTo = dropTo;
                this.DropStart = dropStart;
            }
            public double Discount(int number, double price)
            {
                 return 0;
            }
        }
    
        public class Pd4Discount : IDiscount
        {
            public int Numerator { get; set; }
            public int Denominator { get; set; }
    
            public int FreeCount { get;private set; }
            public Pd4Discount(int numerator, int denominator)
            {
                this.Numerator = numerator;
                this.Denominator = denominator;
            }
            public double Discount(int number, double price)
            {
                return 0;
            }
        }
    }

    Then you could start to make a test.

    To simplify , I only test Pd4Discount.

    Right click your solution , click add new project , input test and then choose UnitTestProject.

    Then you could see a unit test class.

    [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
              
            }
        }

    Write your fist unit method. Mine is 

     public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {// first step , prepare data
                Pd4Discount pd4Discount = new Pd4Discount(1, 1);
    // second step action pd4Discount.Discount(5, 30);
    // third step assert Assert.AreEqual(pd4Discount.FreeCount, 5); } }

    Then , right click in blank, choose run tests.

    The result is failure.

    The implement your method.

      public double Discount(int number, double price)
            {
                this.FreeCount = (int)(number / Denominator * Numerator);
                return number * price;
            }

    Restart the test.

    The result will be a success.

    So in the process, you start a test at first and then write the code.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 9, 2019 6:11 AM

All replies

  • User1120430333 posted

    Test Driven Design is nothing that you think you can just pickup on a run with it.  No, there are several elements of TDD and unit testing  that you must have a firm understanding and grasp of before you can develop a solution using TDD. On top of that, you are new to programming too? 

    Man, I had to sit in over 20 hours of courses for TDD that were mandatory that the company I worked for at the time demanded that each developer take.

    Since you are new to Object Oriented Programming and ASP .NET is an Object Oriented Programming platform, maybe you should learn the basics of Object Oriented Programming before you go any further. Maybe, you should learn the basics of a program language such as C#,  JavaScript and even how to use HTML before you leap off the bridge head first. 

    Seriously, you have no clue on the ramifications of you developing a Web solution that is essentially just going to be poorly architected and it's going to be hack bait, becuase you don't know how to program effectively and you don't know how to architect a solution. 

    I am not trying to be smart here, but it is truly frightening to see your post. 

    Tuesday, January 8, 2019 9:19 AM
  • User569149469 posted

    Hi,

    I have understanding of MVC but TDD is new to me

    Tuesday, January 8, 2019 11:44 AM
  • User1120430333 posted

    Hi,

    I have understanding of MVC but TDD is new to me

    It's more to it than just using MVC. MVC is only one element in developing and architecting a solid programmed solution.  Experienced developers can hardly use TDD, let alone someone that is inexperienced. 

    Tuesday, January 8, 2019 5:27 PM
  • User569149469 posted

    hi,

    forget abt tdd generally how to create checkout with different options on products.Can u help create a interface for it.

    Tuesday, January 8, 2019 11:32 PM
  • User1120430333 posted

    hi,

    forget abt tdd generally how to create checkout with different options on products.Can u help create a interface for it.

    It's been done before with a developer posting example code to give you a roadmap. There should be other code examples you can find using Bing or Google.

    https://www.c-sharpcorner.com/article/creating-shopping-cart-application-from-scratch-in-mvc-part2/

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-8

    http://www.webdevelopmenthelp.net/2016/10/asp-net-mvc-shopping-cart-part1.html

    Wednesday, January 9, 2019 12:10 AM
  • User475983607 posted
    Sounds like homework or an interview question. Can you at least explain your design approach? Share your code and explain the expected results and actual results.
    Wednesday, January 9, 2019 12:52 AM
  • User-893317190 posted

    Hi phmaspnet,

    TTD  starts with test instead of implementing your logic of your code.

    For example , prepare a interface.

     public interface IDiscount
        {
            double Discount(int number, double price);
        }

    And prepare three kinds of discount which has no implements.

     public class Pd3Discount : IDiscount
        {
            public int Numerator { get; set; }
            public int Denominator { get; set; }
            
            public Pd3Discount(int numerator, int denominator)
            {
                this.Numerator = numerator;
                this.Denominator = denominator;
            }
            public double Discount(int number, double price)
            {
                return 0;
            }
        }
    
        public class Pd1Discount : IDiscount
        {
            public double DropTo { get; set; }
            public  int DropStart { get; set; }
            public Pd1Discount(double dropTo, int dropStart)
            {
                this.DropTo = dropTo;
                this.DropStart = dropStart;
            }
            public double Discount(int number, double price)
            {
                 return 0;
            }
        }
    
        public class Pd4Discount : IDiscount
        {
            public int Numerator { get; set; }
            public int Denominator { get; set; }
    
            public int FreeCount { get;private set; }
            public Pd4Discount(int numerator, int denominator)
            {
                this.Numerator = numerator;
                this.Denominator = denominator;
            }
            public double Discount(int number, double price)
            {
                return 0;
            }
        }
    }

    Then you could start to make a test.

    To simplify , I only test Pd4Discount.

    Right click your solution , click add new project , input test and then choose UnitTestProject.

    Then you could see a unit test class.

    [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
              
            }
        }

    Write your fist unit method. Mine is 

     public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {// first step , prepare data
                Pd4Discount pd4Discount = new Pd4Discount(1, 1);
    // second step action pd4Discount.Discount(5, 30);
    // third step assert Assert.AreEqual(pd4Discount.FreeCount, 5); } }

    Then , right click in blank, choose run tests.

    The result is failure.

    The implement your method.

      public double Discount(int number, double price)
            {
                this.FreeCount = (int)(number / Denominator * Numerator);
                return number * price;
            }

    Restart the test.

    The result will be a success.

    So in the process, you start a test at first and then write the code.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 9, 2019 6:11 AM
  • User569149469 posted

    Hi,

    How to use this in checkout 

    Thursday, January 10, 2019 4:52 AM
  • User-893317190 posted

    Hi phmaspnet,

    It depends on the  scenario of your application,this is only a simple sample of how to use unit test , not a realization of functionality for checkout.

    It could be simple or hard according to the checkout functionality of your application.

    Best regards,

    Ackerly Xu

    Thursday, January 10, 2019 7:18 AM
  • User569149469 posted

    Hi,

    Based on the approach suggested I created like this

     public interface ICalculateDiscount
        {
            double Discount(int number, double price);
        }
    
     public class BuyOneGetOneDeal:ICalculateDiscount
        {
            public int Numerator { get; set; }
            public int Denominator { get; set; }
    
            public int FreeCount { get; private set; }
            public BuyOneGetOneDeal(int numerator, int denominator)
            {
                this.Numerator = numerator;
                this.Denominator = denominator;
            }
            public double Discount(int number, double price)
            {
                this.FreeCount = (int)(number / Denominator * Numerator);
                return number * price;
            }
        }
      public class Buy3for2Deal:ICalculateDiscount
        {
            public int Numerator { get; set; }
            public int Denominator { get; set; }
    
            public Buy3for2Deal(int numerator, int denominator)
            {
                this.Numerator = numerator;
                this.Denominator = denominator;
            }
            public double Discount(int number, double price)
            {
                return (int)(number / Denominator * Numerator) * price;
            }
        }  public class BulkBuyDeal:ICalculateDiscount
        {
            public double DropTo { get; set; }
            public int DropStart { get; set; }
            public BulkBuyDeal(double dropTo, int dropStart)
            {
                this.DropTo = dropTo;
                this.DropStart = dropStart;
            }
            public double Discount(int number, double price)
            {
               
    
                if (number>=DropStart)
                {
                    return number * DropTo;
                }
    
                return number * price;
            }
        }
    
    is my approach correct
    

    Thursday, January 10, 2019 8:33 PM
  • User-893317190 posted

    Hi phmaspnet,

    I could't say absolutely right or wrong.

    The interface is used to work with different ways of discount.

    If you want to add more ways of discount , you should add more Discount classes.

    If your system becomes complex , maybe you have to rewrite your interface to be compatible with more  kinds of discount.

    As to the use of the interface , there is no problem.

    Best regards,

    Ackerly Xu

    Friday, January 11, 2019 1:10 AM