Example #1
0
        public void ResupplyAndReorderReadAndWrite()
        {
            string          path    = "../../../SimplyWriteData.json";
            JsonFilePersist persist = new JsonFilePersist(path);
            CStore          store   = persist.ReadStoreData();

            List <CProduct> supply = new List <CProduct> {
                new CProduct("111", "Banana", "Produce", 0.5, 10),
                new CProduct("222", "orange", "Produce", 0.88, 10),
                new CProduct("333", "Rocket", "Transport", 1000000, 15)
            };

            store.AddProducts(supply);
            CCustomer       customer = new CCustomer("127137147", "Adam", "Savage", "4801111111");
            List <CProduct> p        = new List <CProduct> {
                new CProduct("111", "Banana", "Produce", 0.5, 1),
                new CProduct("222", "orange", "Produce", 0.88, 1)
            };
            COrder order = new COrder(store, customer, DateTime.Today, 100, p);

            customer.PlaceOrder(store, order);

            persist.WriteStoreData(store);
            foreach (var pair in store.Inventory)
            {
                Assert.Equal(15, pair.Value.Quantity);
            }
        }
Example #2
0
        // helper classes refactored
        private static void InventorySetup(IStoreRepository repo, string storeLoc, CStore store)
        {
            List <CProduct> inventory = repo.GetInventoryOfAStore(storeLoc);

            store.CleanInventory();
            store.AddProducts(inventory);
            Console.WriteLine("Initial inventory set up done");
        }
        public ActionResult Checkout()
        {
            JsonFilePersist persist  = new JsonFilePersist();
            List <CProduct> products = persist.ReadProductsTempData(TempData.Peek("Cart").ToString());

            if (products == null)
            {
                return(RedirectToAction("CheckCart"));
            }

            // orderid , store, customer, ordertime, totalcost
            string    orderID   = Guid.NewGuid().ToString().Substring(0, 10);
            string    storeLoc  = TempData.Peek("storeLoc").ToString();
            CStore    store     = _storeRepo.GetOneStore(storeLoc);
            string    email     = TempData.Peek("User").ToString();
            CCustomer cCustomer = _storeRepo.GetOneCustomerByEmail(email);
            // recalculate price in case customers change quantity in carts
            double totalCost = store.CalculateTotalPrice(products);

            COrder newOrder = new COrder(orderID, store, cCustomer, totalCost);

            // check against inventory
            var inventory = _storeRepo.GetInventoryOfOneStore(storeLoc);

            store.CleanInventory();
            store.AddProducts(inventory);

            bool isSuccessful = false;

            if (store.CalculateThreshhold(products))
            {
                // quantity limits
                newOrder.ProductList = products;
                isSuccessful         = true;
            }
            else
            {
                ModelState.AddModelError("", "Quantity set exceeds the maximum allowed");
                _logger.LogError("Quantity set exceeds the maximum allowed");
                return(RedirectToAction("CheckCart"));
            }
            if (isSuccessful)
            {
                if (!store.CheckInventory(newOrder))
                {
                    ModelState.AddModelError("", "Not enough left in the inventory");
                    _logger.LogError("Not enough left in the inventory");
                    return(RedirectToAction("CheckCart"));
                }
                else
                {
                    store.UpdateInventory(newOrder);
                    _storeRepo.CustomerPlaceOneOrder(newOrder, store, totalCost);
                }
            }

            // clean cart
            StreamWriter sw = new StreamWriter("../../SimplyWriteData.json");

            sw.WriteLine(string.Empty);
            sw.Close();
            TempData.Remove("Cart");
            TempData.Remove("Total");
            return(View());
        }