Beispiel #1
0
        public Discount getStoreDiscount(int id, int storeId)
        {
            //SqlConnection connection = Connector.getInstance().getSQLConnection();
            var discountEntry = connection.Query <DiscountEntry>("SELECT * FROM [dbo].[Discount] WHERE id=@id AND storeId=@storeId", new { id, storeId = storeId }).First();

            DiscountEntry d          = (DiscountEntry)discountEntry;
            int           discountId = d.getId();


            DiscountComponentEntry component = (DiscountComponentEntry)connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE id=@id", new { id = discountId }).First();

            connection.Close();
            bool isPartOfComplex = false;

            if (component.getIsPartOfComplex() == 1)
            {
                isPartOfComplex = true;
            }
            if (d.getType() == "Visible")
            {
                VisibleDiscount v = new VisibleDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getVisibleType(), component.getStoreId());
                return(v);
            }
            else
            {
                ReliantDiscount r = null;
                if (d.getReliantType() == "totalAmount")
                {
                    r = new ReliantDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getTotalAmount(), component.getStoreId());
                }
                return(r);
            }
        }
Beispiel #2
0
        public void removeDiscount(int discountID)
        {
            foreach (DiscountComponent d in discountList)
            {
                if (d.getId() == discountID)
                {
                    //
                    discountList.Remove(d);
                    if (d is ReliantDiscount)
                    {
                        ReliantDiscount r = (ReliantDiscount)d;

                        if (r.getProduct() != null)
                        {
                            r.getProduct().removeReliantDiscount();
                        }
                    }
                    if (d is VisibleDiscount)
                    {
                        VisibleDiscount v = (VisibleDiscount)d;

                        if (v.getProduct() != null)
                        {
                            v.getProduct().removeDiscount();
                        }
                    }
                    if (d is DiscountComposite)
                    {
                        removeChildren((DiscountComposite)d);
                    }
                    break;
                }
            }
        }
Beispiel #3
0
        private void removeChildren(DiscountComposite d)
        {
            foreach (DiscountComponent child in d.getChildren())
            {
                if (child is DiscountComposite)
                {
                    removeChildren((DiscountComposite)child);
                }
                else
                {
                    if (child is ReliantDiscount)
                    {
                        ReliantDiscount r = (ReliantDiscount)child;

                        if (r.getProduct() != null)
                        {
                            r.getProduct().removeReliantDiscount();
                        }
                    }
                    if (child is VisibleDiscount)
                    {
                        VisibleDiscount v = (VisibleDiscount)child;

                        if (v.getProduct() != null)
                        {
                            v.getProduct().removeDiscount();
                        }
                    }
                }
            }
        }
Beispiel #4
0
        public void addStoreVisibleDiscount(double percentage, string duration)
        {
            VisibleDiscount v = new VisibleDiscount(percentage, duration, "StoreVisibleDiscount", store.getStoreID());

            DBDiscount.getInstance().addDiscount(v);
            store.addDiscount(v);
        }
Beispiel #5
0
 public void setDiscount(VisibleDiscount discount)
 {
     this.discount = discount;
     // this.discountID = discount.getId();
     //DBProduct.getInstance().update(this);
     //store.addDiscount(discount);
 }
Beispiel #6
0
        public void addProductVisibleDiscount(Product product, double percentage, string duration)
        {
            Store           store    = product.getStore();
            VisibleDiscount discount = new VisibleDiscount(percentage, duration, "ProductVisibleDiscount", store.getStoreID());

            store.addDiscount(discount);
            discount.setProduct(product);
            product.setDiscount(discount);
            DBDiscount.getInstance().addDiscount(discount);
        }
Beispiel #7
0
 public Discount getProductDiscount(int storeId, int productId)
 {
     try
     {
         //SqlConnection connection = Connector.getInstance().getSQLConnection();
         lock (connection)
         {
             connection.Open();
             var discountEntry = connection.Query <DiscountEntry>("SELECT * FROM [dbo].[Discount] WHERE storeId=@storeId AND productId=@productId", new { storeId = storeId, productId = productId }).First();
             connection.Close();
             DiscountEntry     d   = (DiscountEntry)discountEntry;
             DiscountComponent dis = getDiscountByID(d.getId());
             if (dis != null)
             {
                 return((Discount)dis);
             }
             int discountId = d.getId();
             DiscountComponentEntry component = (DiscountComponentEntry)connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE id=@id", new { id = discountId }).First();
             bool isPartOfComplex             = false;
             if (component.getIsPartOfComplex() == 1)
             {
                 isPartOfComplex = true;
             }
             if (d.getType() == "Visible")
             {
                 VisibleDiscount v = new VisibleDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getVisibleType(), component.getStoreId());
                 Product         p = DBProduct.getInstance().getProductByID(d.getProductId());
                 v.setProduct(p);
                 discounts.AddFirst(v);
                 connection.Close();
                 return(v);
             }
             else
             {
                 int             productID = d.getProductId();
                 ReliantDiscount r         = null;
                 if (d.getReliantType() == "sameProduct")
                 {
                     Product p = DBProduct.getInstance().getProductByID(productID);
                     r = new ReliantDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getNumOfProducts(), p, component.getStoreId());
                     discounts.AddFirst(r);
                 }
                 return(r);
             }
         }
     }
     catch (Exception)
     {
         if (connection.State != ConnectionState.Closed)
         {
             connection.Close();
         }
         return(null);
     }
 }
Beispiel #8
0
 public void removeDiscount()
 {
     if (discount == null)
     {
         throw new DoesntExistException("Error: Discount does not exist so it cannot be removed");
     }
     else
     {
         discount = null;
     }
 }
Beispiel #9
0
 public Product(string productName, string productCategory, int price, int rank, int quantityLeft, Store store)
 {
     this.productID       = DBProduct.getNextProductID();
     this.productName     = productName;
     this.productCategory = productCategory;
     this.price           = price;
     this.rank            = rank;
     this.quantityLeft    = quantityLeft;
     this.store           = store;
     //this.numberOfRanking = 0;
     this.discount = null;
 }
Beispiel #10
0
        public void init()
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    var products = connection.Query <Product>("SELECT * FROM [dbo].[Product]");


                    if (products.Count() == 0)
                    {
                        connection.Close();
                        return;
                    }

                    foreach (Product product in products)
                    {
                        // if(product.discountID != -1)
                        //    product.discount = DBDiscount.getInstance().getDiscount(product.discountID);
                        productList.AddFirst(product);
                        Discount d = DBDiscount.getInstance().getProductDiscount(product.getStore().getStoreID(), product.getProductID());
                        if (d != null)
                        {
                            if (d is VisibleDiscount)
                            {
                                VisibleDiscount v = (VisibleDiscount)d;
                                product.setDiscount(v);
                            }
                            if (d is ReliantDiscount)
                            {
                                ReliantDiscount r = (ReliantDiscount)d;
                                product.setReliantDiscountSameProduct(r);
                            }
                        }
                        if (product.getProductID() > nextProductID)
                        {
                            nextProductID = product.getProductID();
                        }
                    }

                    connection.Close();
                }
            }
            catch (Exception e)
            {
                connection.Close();
                throw e;
            }

            nextProductID++;
        }
Beispiel #11
0
 //added
 public Product(int productID, string productName, string productCategory, int price, int rank, int quantityLeft, int storeID)
 {
     this.productID       = productID;
     this.productName     = productName;
     this.productCategory = productCategory;
     this.price           = price;
     this.rank            = rank;
     this.storeID         = storeID;
     this.quantityLeft    = quantityLeft;
     this.discount        = null;
     //this.discountID = discountID;
 }
Beispiel #12
0
        private void addVisibleDiscount(VisibleDiscount v)
        {
            //SqlConnection connection = Connector.getInstance().getSQLConnection();
            int    id          = v.getId();
            string type        = "Visible";
            string reliantType = "-1";
            string visibleType;
            int    productId;
            int    storeId = v.getStoreId();

            if (v.getProduct() == null)
            {
                visibleType = "StoreVisibleDiscount";
                productId   = -1;
            }
            else
            {
                visibleType = "ProductVisibleDiscount";
                productId   = v.getProduct().getProductID();
            }
            //not reliantdiscount
            int    numOfProducts = -1;
            int    totalAmount   = -1;
            string sql           = "INSERT INTO [dbo].[Discount] (id, type, reliantType, visibleType, productId, storeId, numOfProducts, totalAmount)" +
                                   " VALUES (@id, @type, @reliantType, @visibleType, @productId, @storeId, @numOfProducts, @totalAmount)";

            connection.Execute(sql, new
            {
                id,
                type,
                reliantType,
                visibleType,
                productId,
                storeId,
                numOfProducts,
                totalAmount
            });
        }
Beispiel #13
0
        public double getActualTotalPrice()
        {
            productsActualPrice = new Dictionary <Product, double>();
            fillActualPriceDic();
            double sum = 0;
            LinkedList <DiscountComponent> discounts = store.getDiscounts();

            foreach (DiscountComponent dis in discounts)
            {
                if (dis is DiscountComposite)
                {
                    if (dis.checkCondition(productList, productsActualPrice) && dis.checkDate())
                    {
                        dis.setComplexCondition(true, productList, productsActualPrice);
                    }
                    else
                    {
                        if (!dis.checkDate())
                        {
                            // discount is invalid - date has passed
                            store.removeDiscount(dis.getId());
                        }
                        dis.setComplexCondition(false, productList, productsActualPrice);
                    }
                }
            }
            foreach (KeyValuePair <Product, int> entry in productList)
            {
                Product p           = entry.Key;
                double  actualPrice = p.getActualPrice(entry.Value);
                sum += (entry.Value * actualPrice);
            }

            foreach (DiscountComponent dis in discounts)
            {
                if (dis is ReliantDiscount)
                {
                    ReliantDiscount r = (ReliantDiscount)dis;
                    if (!r.getIsPartOfComplex())
                    {
                        if (r.isTotalAmountDiscount() && sum >= r.getTotalAmount())
                        {
                            sum = sum * (1 - r.getPercentage());
                        }
                    }
                }
            }

            foreach (DiscountComponent dis in discounts)
            {
                if (dis is VisibleDiscount)
                {
                    VisibleDiscount v = (VisibleDiscount)dis;
                    if (!v.getIsPartOfComplex() && v.isStoreVisibleDiscount())
                    {
                        if (v.isStoreVisibleDiscount())
                        {
                            sum = sum * (1 - v.getPercentage());
                        }
                    }
                }
            }

            return(sum);
        }
Beispiel #14
0
 public void setDiscount(VisibleDiscount discount)
 {
     this.discount = discount;
 }
Beispiel #15
0
        public void addDiscount(DiscountComponent d)
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    string sql = "INSERT INTO [dbo].[DiscountComponent] (id, percentage, duration, type, storeId, isPartOfComplex)" +
                                 " VALUES (@id,@percentage, @duration, @type, @storeId, @isPartOfComplex)";
                    int isPartOfComplex;
                    if (d.getIsPartOfComplex())
                    {
                        isPartOfComplex = 1;
                    }
                    else
                    {
                        isPartOfComplex = 0;
                    }
                    if (d is Discount)
                    {
                        connection.Execute(sql, new
                        {
                            id         = d.getId(),
                            percentage = d.getPercentage(),
                            duration   = d.getDuration(),
                            type       = "Discount",
                            storeId    = d.getStoreId(),
                            isPartOfComplex
                        });
                        if (d is VisibleDiscount)
                        {
                            VisibleDiscount v = (VisibleDiscount)d;
                            addVisibleDiscount(v);
                        }
                        if (d is ReliantDiscount)
                        {
                            ReliantDiscount r = (ReliantDiscount)d;
                            addReliantDiscount(r);
                        }
                    }
                    if (d is DiscountComposite)
                    {
                        DiscountComposite composite = (DiscountComposite)d;
                        connection.Execute(sql, new
                        {
                            id         = d.getId(),
                            percentage = d.getPercentage(),
                            duration   = d.getDuration(),
                            type       = "Composite",
                            storeId    = d.getStoreId(),
                            isPartOfComplex
                        });
                        foreach (DiscountComponent child in composite.getChildren())
                        {
                            string sql2 = "INSERT INTO [dbo].[DiscountComposite] (id, childid, type)" +
                                          " VALUES (@id, @childid, @type)";

                            connection.Execute(sql2, new
                            {
                                id      = d.getId(),
                                childid = child.getId(),
                                type    = composite.getType()
                            });
                        }
                    }
                    connection.Close();
                    discounts.AddFirst(d);
                }
            }

            catch (Exception e)
            {
                connection.Close();
                throw e;
            }
        }
Beispiel #16
0
        public void addProductVisibleDiscount(Product product, int percentage, string duration)
        {
            VisibleDiscount discount = new VisibleDiscount(percentage, duration);

            product.setDiscount(discount);
        }
Beispiel #17
0
 public void setVisibleDiscount(VisibleDiscount v)
 {
     this.discount = v;
 }
Beispiel #18
0
        public void addStoreVisibleDiscount(int percentage, string duration)
        {
            VisibleDiscount v = new VisibleDiscount(percentage, duration);

            store.addDiscount(v);
        }