public void DisplayCustomer(int id, bool newLine = false, int tabIndents = 0)
        {
            string indentation = new string(' ', tabIndents * 2);
            var    data        = StoreManagerApplication.GetData <Customer>(id) as CustomerData;
            var    address     = (StoreManagerApplication.GetData <Address>(data.AddressId) as AddressData).ToString();
            var    prefix      = $"{(newLine ? "\n" : "")}{indentation}";
            var    items       = new List <string>
            {
                //   Name
                data.Name,
                //   Email
                data.Email,
                //   PhoneNumber
                data.PhoneNumber,
                //   AddressId -> Address
                address,
                //   BirthDate
                data.BirthDate.ToString()
            };

            Console.Write($"{prefix}{string.Join(prefix, items)}");
            //   DefaultStoreLocationId -> OperatingLocation&store (if exists)
            if (data.DefaultStoreLocationId.HasValue)
            {
                DisplayOperatingLocation(data.DefaultStoreLocationId.Value);
            }
        }
        protected OrderData CreateOrderData()
        {
            int customerId          = -1;
            int operatingLocationId = -1;
            Dictionary <int, int> productsRequested = new Dictionary <int, int>();

            // Get the customer id
            customerId = PromptForCreateOrExist <Customer>(
                () => {
                // Create a new customer
                var data = CreateCustomerData();
                return(StoreManagerApplication.Create <Customer>(data));
            },
                // Get the customer id
                () => PromptForId <Customer>()
                );

            // Get the store that owns the location
            int storeId = PromptForCreateOrExist <Store>(
                () => {
                var data = CreateStoreData();
                return(StoreManagerApplication.Create <Store>(data));
            },
                () => PromptForId <Store>()
                );

            var store = StoreManagerApplication.GetData <Store>(storeId) as StoreData;
            // Prompt for a store, then look through the locations it has
            var options = store.OperatingLocationIds.Select(id => {
                var data    = StoreManagerApplication.GetData <OperatingLocation>(id) as OperatingLocationData;
                var address = StoreManagerApplication.GetData <Address>(data.AddressId) as AddressData;
                return($"{store.Name} - {address}");
            }).ToArray();
            // Show the available locations that can be chosen from
            int selectedOption = CUI.PromptForMenuSelection(options, false);

            operatingLocationId = store.OperatingLocationIds[selectedOption];

            // Get the products the user wants
            UntilItIsDone(() => {
                // Add products and the inventory they have
                int productId = PromptForCreateOrExist <Product>(
                    () => {
                    var data = CreateProductData();
                    return(StoreManagerApplication.Create <Product>(data));
                },
                    () => PromptForId <Product>()
                    );

                int threshold = store.Inventory[productId].Item2 ?? store.Inventory[productId].Item1;

                // Get the count
                int count = CUI.PromptRange("Enter the count of said product", 0, threshold);
                productsRequested[productId] = count;

                return(!CUI.PromptForBool("Add another product?", "yes", "no"));
            });

            return(new OrderData(customerId, operatingLocationId, productsRequested));
        }
        public void DisplayAddress(int id, bool newLine = false, int tabIndents = 0)
        {
            string indentation = new string(' ', tabIndents * 2);
            string address     = (StoreManagerApplication.GetData <Address>(id) as AddressData).ToString();

            Console.Write($"{(newLine ? "\n" : "")}{indentation}{address}");
        }
        protected int PromptForCreateOrExist <T>(Func <int> creationFunction, Func <int> existingFunction)
            where T : SEntity
        {
            int  result;
            bool itemExists = StoreManagerApplication.Any <T>();
            bool createItem = true;

            if (itemExists)
            {
                createItem = CUI.PromptForBool($"Create a new {_typeNames[typeof(T)]} or use one that is existing?", "create", "existing");
            }
            else
            {
                Console.WriteLine($"No {_typeNames[typeof(T)]}s exist; please create a {_typeNames[typeof(T)]}.");
            }

            if (createItem)
            {
                // create the item
                result = creationFunction();
            }
            else
            {
                // just get the id
                result = existingFunction();
            }

            return(result);
        }
        private void PlaceOrderToStoreLocation()
        {
            var data = CreateOrderData();

            _ = StoreManagerApplication.Create <Order>(data);
            Console.WriteLine($"Order placed successfully.");
        }
        private void AddCustomer()
        {
            var data = CreateCustomerData();

            _ = StoreManagerApplication.Create <Customer>(data);
            Console.WriteLine($"Customer with name: '{data.LastName}, {data.FirstName}' created successfully.");
        }
        public void DisplayProduct(int id, int count = 1, bool newLine = false, int tabIndents = 0)
        {
            var    data        = StoreManagerApplication.GetData <Product>(id) as ProductData;
            string indentation = new string(' ', tabIndents * 2);
            string discount    = data.Discount.HasValue ? $" ({data.Discount.Value}% off, making it {data.Price * (100 - data.Discount) / 100})" : "";

            Console.Write($"{(newLine ? "\n" : "")}{indentation}{data.Name} - ${data.Price * count} (x{count}) {discount}");
        }
 public void AnyVeroExists()
 {
     try {
         var customerIds = StoreManagerApplication.GetCustomerIdsByName("Vero");
         Assert.True(customerIds.Any(), "Nobody with the name 'Vero' was found in the database");
     } catch (Exception) {
         Assert.True(false);
     }
 }
 public void CreateTestCustomer(string firstName, string lastName, string email, string phoneNumber, int addressId, int year, int month, int day, int?defaultStoreLocation)
 {
     try {
         CustomerData data = new CustomerData(firstName, lastName, email, phoneNumber, addressId, new DateTime(year, month, day), defaultStoreLocation);
         StoreManagerApplication.Create <Customer>(data);
     } catch (Exception) {
         Assert.True(false);
     }
 }
Пример #10
0
        internal static DbOrderProduct ToDbOrderProduct(KeyValuePair <int, int> pair)
        {
            var product = ProductDbSetInterfacer.ToDbProduct(
                StoreManagerApplication.Get <Product>(pair.Key)
                );

            return(new DbOrderProduct
            {
                Product = product,
                Count = pair.Value
            });
        }
        protected int PromptForId <T>()
            where T : SEntity
        {
            int id = -1;

            UntilItIsDone(() => {
                id = CUI.PromptRange($"Please enter the ID for the {_typeNames[typeof(T)]}", 0, StoreManagerApplication.MaxId <T>());
                return(StoreManagerApplication.IdExists <T>(id));
            });

            return(id);
        }
        internal static DbStoreInventory ToDbStoreInventory(KeyValuePair <int, Tuple <int, int?> > pair)
        {
            var product = ProductDbSetInterfacer.ToDbProduct(
                StoreManagerApplication.Get <Product>(pair.Key)
                );

            return(new DbStoreInventory
            {
                Product = product,
                Count = pair.Value.Item1,
                Threshold = pair.Value.Item2
            });
        }
 public ApplicationInterfaceBase(IStorageRepository storage = null, ISerializer serializer = null, IConfigurationOptions configurationOptions = null)
 {
     StoreManagerApplication.Initialize(storage, serializer, configurationOptions);
     _typeNames = new Dictionary <Type, string>
     {
         { typeof(Customer), "customer" },
         { typeof(Store), "store" },
         { typeof(Order), "order" },
         { typeof(Address), "address" },
         { typeof(OperatingLocation), "operating location" },
         { typeof(Product), "product" }
     };
 }
        protected StoreData CreateStoreData()
        {
            string     storeName            = CUI.PromptForInput("Enter the store name", false);
            List <int> operatingLocationIds = new List <int>();
            List <int> customerIds          = new List <int>();
            Dictionary <int, Tuple <int, int?> > inventory = new Dictionary <int, Tuple <int, int?> >();

            int locationId;

            UntilItIsDone(() => {
                // Add operating locations
                locationId = PromptForCreateOrExist <OperatingLocation>(
                    () => {
                    // Create a new operating location
                    var data = CreateOperatingLocationData();
                    return(StoreManagerApplication.Create <OperatingLocation>(data));
                },
                    // Prompt for an operating location
                    () => PromptForId <OperatingLocation>()
                    );

                operatingLocationIds.Add(locationId);

                return(!CUI.PromptForBool("Add another operating location?", "yes", "no"));
            });

            UntilItIsDone(() => {
                // Add products and the inventory they have
                int productId = PromptForCreateOrExist <Product>(
                    () => {
                    var data = CreateProductData();
                    return(StoreManagerApplication.Create <Product>(data));
                },
                    () => PromptForId <Product>()
                    );

                // Get the count
                int count = CUI.PromptRange("Enter the count of said product", 0, int.MaxValue);

                int?threshold = CUI.PromptForBool("Set a product threshold?", "yes", "no")
                    ? CUI.PromptRange("Enter the product threshold", 1, count) : null;

                inventory[productId] = new Tuple <int, int?>(count, threshold);

                return(!CUI.PromptForBool("Add another product?", "yes", "no"));
            });

            return(new StoreData(storeName, operatingLocationIds, inventory));
        }
        public void DisplayStore(int id, bool newLine = false, int tabIndents = 0)
        {
            string indentation = new string(' ', tabIndents * 2);
            string prefix      = $"{(newLine ? "\n" : "")}{indentation}";
            var    data        = StoreManagerApplication.GetData <Store>(id) as StoreData;
            // Name
            string storeName = data.Name;

            // Operating Locations
            // var operatingLocationAddressIds = data.OperatingLocationIds.ConvertAll(ol => (StoreManagerApplication.Get<OperatingLocation>(ol) as  OperatingLocationData).AddressId);
            // var addresses = operatingLocationAddressIds.ConvertAll(ola => StoreManagerApplication.Get<Address>(ola) as AddressData);
            // addresses.ForEach(ol => );
            Console.Write($"{prefix}{storeName}");
            data.OperatingLocationIds.ForEach(olid => DisplayOperatingLocation(olid, true, tabIndents + 1));
        }
 private void DisplayOrderDetails()
 {
     // get the order id
     // display its data
     if (!StoreManagerApplication.Any <Order>())
     {
         Console.WriteLine("No orders present");
     }
     else
     {
         int orderId = PromptForId <Order>();
         // Display the order
         DisplayOrder(orderId);
     }
 }
        // [Fact] public void Test() { }
        // [Theory] public void Test(int param) { }
        public StoreManagerTests()
        {
            // Get the connection string
            string filepath   = @"C:/Users/Khypr/Desktop/store_manager_configuration.json";
            string json       = File.ReadAllText(filepath);
            string connString = JsonSerializer.Deserialize <string>(json);

            // Set up storage
            IStorageRepository storage = new DatabaseStorageRepository();
            // Set up the configuration options
            IConfigurationOptions configurationOptions = new DatabaseConfigurationOptions(new FileLogger("C:/Users/Khypr/Desktop/StoreManager/store_manager.tests.log"), connString);

            // Initialize the App
            StoreManagerApplication.Initialize(storage, null, configurationOptions);
        }
        protected IData GetData <T>()
            where T : SEntity
        {
            // Get the data associated with the id
            IData result = null;

            UntilItIsDone(() => {
                // Get the id
                int id = PromptForId <T>();
                result = StoreManagerApplication.GetData <T>(id);

                return(result is not null);
            });

            return(result);
        }
        public void DisplayOperatingLocation(int id, bool newLine = false, int tabIndents = 0)
        {
            string indentation = new string(' ', tabIndents * 2);

            // Get the store and address id from the operating location's data
            (int storeId, int addressId) = new Func <Tuple <int, int> >(() => {
                var tempData = StoreManagerApplication.GetData <OperatingLocation>(id) as OperatingLocationData;
                return(new Tuple <int, int>(tempData.StoreId, tempData.AddressId));
            }).Invoke();
            // Get the name of the store
            string storeName = StoreManagerApplication.GetName <Store>(storeId);

            // Get the address as a string
            Console.Write($"{(newLine ? "\n" : "")}{indentation}{storeName} - ");
            DisplayAddress(addressId);
        }
        private void SearchCustomersByName()
        {
            // get the name they wish to search for
            string userInput = CUI.PromptForInput("Enter the name in the format of 'first, last'", false);
            // get a list of the results
            var customerIds = StoreManagerApplication.GetCustomerIdsByName(userInput);

            if (customerIds.Any())
            {
                // display the results
                customerIds.ForEach(cid => DisplayCustomer(cid, true));
            }
            else
            {
                Console.WriteLine($"No customers found with name like '{userInput}'.");
            }
        }
        private void DisplayCustomerOrderHistory()
        {
            // get the customer they're looking for
            int customerId = PromptForId <Customer>();
            // get the orders of the customer they're looking for
            var customerOrders = StoreManagerApplication.GetOrderIdsByCustomerId(customerId);

            if (customerOrders.Any())
            {
                // display all of the order details
                customerOrders.ForEach(co => DisplayOrder(co, true));
            }
            else
            {
                Console.WriteLine($"No orders for '{StoreManagerApplication.GetName<Customer>(customerId)}'");
            }
        }
        public void DisplayOrder(int orderId, bool newLine = false, int tabIndents = 0)
        {
            var    data         = StoreManagerApplication.GetData <Order>(orderId) as OrderData;
            string customerName = StoreManagerApplication.GetName <Customer>(data.CustomerId);
            // Get the addressId and storeId from the operating location
            int storeId = (StoreManagerApplication.GetData <OperatingLocation>(data.OperatingLocationId) as OperatingLocationData).StoreId;
            // get the store that owns the operating location
            string storeName = StoreManagerApplication.GetName <Store>(storeId);

            // Display the order id and display the Customer name
            Console.Write($"Order Details for Order#{orderId} from {storeName}:\n{new string(' ', (tabIndents + 1) * 2)}Customer: {customerName}");
            // Display the Operating Location
            DisplayOperatingLocation(data.OperatingLocationId, true, tabIndents + 1);
            // Display the products requested
            Console.Write($"{new string(' ', (tabIndents + 1) * 2)}Products Requested:");
            foreach (var kv in data.ProductsRequested)
            {
                DisplayProduct(kv.Key, kv.Value, true, tabIndents + 2);
            }
        }
        internal static DbStore ToDbStore(Store store)
        {
            var data = store.Data;
            var operatingLocations = data.OperatingLocationIds.ConvertAll(olid =>
                                                                          OperatingLocationDbSetInterfacer.ToDbOperatingLocation(
                                                                              StoreManagerApplication.Get <OperatingLocation>(olid)
                                                                              )
                                                                          );
            var inventory = data.Inventory.Select(
                kv => ToDbStoreInventory(kv)
                ).ToList();

            return(new DbStore
            {
                StoreId = store.Id,
                Name = data.Name,
                OperatingLocations = operatingLocations,
                StoreInventories = inventory
            });
        }
        protected CustomerData CreateCustomerData()
        {
            string firstName   = CUI.PromptForInput("Enter their first name", false);
            string lastName    = CUI.PromptForInput("Enter their last name", false);
            string email       = CUI.PromptForEmail("Enter their email");
            string phoneNumber = CUI.PromptForPhoneNumber("Enter their phone number");

            int addressId = PromptForCreateOrExist <Address>(
                () => {
                var temp = CreateAddressData();
                return(StoreManagerApplication.Create <Address>(temp));
            },
                () => PromptForId <Address>()
                );

            DateTime birthDate = CUI.PromptForDateTime("Enter a birth date", CUI.TimeFrame.Past, true);
            int?     defaultStoreLocationId = CUI.PromptForBool("Set a default store location?", "yes", "no")
                ? PromptForId <OperatingLocation>() : null;

            return(new CustomerData(firstName, lastName, email, phoneNumber, addressId, birthDate, defaultStoreLocationId));
        }
        private void DisplayStoreOrderHistory()
        {
            // Get all of the stores
            var stores = StoreManagerApplication.GetAllData <Store>()
                         .Select(data => data as StoreData);
            // Get their names
            var storeNames = stores.Select(sd => sd.Name).ToArray();
            // See which one the user wishes to see
            int selectedOption = CUI.PromptForMenuSelection(storeNames, false);
            // Get the store they wish to see
            var selectedStore = stores.ElementAt(selectedOption);

            if (selectedStore.OrderIds.Any())
            {
                // display all of the orders
                selectedStore.OrderIds.ForEach(o => DisplayOrder(o));
            }
            else
            {
                Console.WriteLine($"No orders for '{selectedStore.Name}'");
            }
        }
        protected OperatingLocationData CreateOperatingLocationData()
        {
            // Get the store that owns this location
            int storeId = PromptForCreateOrExist <Store>(
                () => {
                var data = CreateStoreData();
                return(StoreManagerApplication.Create <Store>(data));
            },
                () => PromptForId <Store>()
                );

            // Get the address of this location
            int addressId = PromptForCreateOrExist <Address>(
                () => {
                var data = CreateAddressData();
                return(StoreManagerApplication.Create <Address>(data));
            },
                () => PromptForId <Address>()
                );

            return(new OperatingLocationData(storeId, addressId));
        }
        protected string[] CustomerDataToStringArray(CustomerData data)
        {
            var address = (StoreManagerApplication.GetData <Address>(data.AddressId) as AddressData).ToString();
            var items   = new List <string>
            {
                //   Name
                data.Name,
                //   Email
                data.Email,
                //   PhoneNumber
                data.PhoneNumber,
                //   AddressId -> Address
                address,
                //   BirthDate
                data.BirthDate.ToString()
            };
            var defaultStoreLocation = StoreManagerApplication.GetData <OperatingLocation>(data.DefaultStoreLocationId.Value) as OperatingLocationData;
            var dslStoreName         = StoreManagerApplication.GetName <Store>(defaultStoreLocation.StoreId);
            var dslAddress           = (StoreManagerApplication.GetData <Address>(defaultStoreLocation.AddressId) as AddressData).ToString();

            items.Add($"{dslStoreName} - {dslAddress}");

            return(items.ToArray());
        }
Пример #28
0
        internal static DbOrder ToDbOrder(Order order)
        {
            var data = order.Data;
            // Get the customer
            var customer = CustomerDbSetInterfacer.ToDbCustomer(
                StoreManagerApplication.Get <Customer>(data.CustomerId)
                );
            // Get the Operating Location
            var operatingLocations = OperatingLocationDbSetInterfacer.ToDbOperatingLocation(
                StoreManagerApplication.Get <OperatingLocation>(data.OperatingLocationId)
                );
            // Get the Products Requested
            var productsRequested = data.ProductsRequested.Select(
                pr => ToDbOrderProduct(pr)
                ).ToList();

            return(new DbOrder
            {
                OrderId = order.Id,
                Customer = customer,
                OperatingLocation = null,
                OrderProducts = productsRequested
            });
        }
 public void AnyCustomerExists()
 {
     Assert.True(StoreManagerApplication.Any <Customer>(), "No customers were found in the database.");
 }