예제 #1
0
        public override void Execute()
        {
            if (!HasEnoughArguments(2))
            {
                return;
            }

            string  username         = Command[0];
            string  productIdString  = Command[1];
            int     amountToPurchase = 1;
            Product product;
            User    user;

            if (Command.Length == 3 && !int.TryParse(Command[2], out amountToPurchase))
            {
                barcodeCli.DisplayGeneralError($"{Command[2]} is not a valid amount.");
                return;
            }

            try
            {
                user = barcodeSystem.GetUserByUsername(username);
                if (uint.TryParse(productIdString, out uint productId))
                {
                    try
                    {
                        product = barcodeSystem.GetProductById(productId);
                        try
                        {
                            BuyTransaction transaction = barcodeSystem.BuyProduct(user, product, amountToPurchase);

                            this.transaction = transaction;
                            Succeeded        = transaction.Succeeded;

                            barcodeCli.DisplayUserBuysProduct(transaction);
                        }
                        catch (InsufficientCreditsException)
                        {
                            barcodeCli.DisplayInsufficientCash(user, product);
                        }
                    }
                    catch (ProductNotFoundException)
                    {
                        barcodeCli.DisplayProductNotFound(productIdString);
                    }
                }
            }
            catch (UserNotFoundException)
            {
                barcodeCli.DisplayUserNotFound(username);
            }

            base.Execute();
        }
예제 #2
0
        public override void Execute()
        {
            try
            {
                List <ICommand> commands = commandsEntered.Where(command => command.Succeeded).ToList();
                barcodeCli.DisplayCommandLog(commands);
            }
            catch (Exception e)
            {
                barcodeCli.DisplayGeneralError(e.ToString());
            }

            base.Execute();
        }
예제 #3
0
        public BarcodeSystem AddProductDataStore(IDataStore <Product> productDataStore)
        {
            this.productDataStore = productDataStore;

            try
            {
                IEnumerable <Product> productStore = this.productDataStore.ReadData();
                Products = productStore.ToList();
            }
            catch
            {
                barcodeCli.DisplayGeneralError("Product data store not loaded." +
                                               "\n" +
                                               "Please make sure that you have provided the correct data." +
                                               "\n" +
                                               "Could not find:" +
                                               "\n" +
                                               $"{this.productDataStore.fullFilePath}");

                barcodeCli.AwaitKeyPress().Close();
            }

            return(this);
        }
예제 #4
0
        public override void Execute()
        {
            try
            {
                CommandToUndo = commandsExecuted.FindLast(command => !command.Undone && command.Succeeded);

                CommandToUndo?.Undo();
                Succeeded = true;

                barcodeCli.DisplayUndoCommand(CommandToUndo);
            }
            catch
            {
                barcodeCli.DisplayGeneralError("There is no command to undo.");
            }

            base.Execute();
        }