Exemplo n.º 1
0
        private void BuyItem()
        {
            Console.WriteLine();
            Console.WriteLine("How many would you like to buy");
            int quantity = SetQuantity();

            // enforces cargo capacity.
            if (MyShip.GetCargoWeight() <= (MyShip.GetCargoCapacity() - (150 * quantity)) && quantity > 0 && quantity <= 10)
            {
                Console.Clear();
                // displays credit change and prompts user for confirmation.
                Console.WriteLine($"This transaction will leave you with {MyShip.GetCredits() - (175 * quantity)} credits. Proceed?");
                Console.WriteLine();
                Console.WriteLine("1 = Yes, 2 = No");
                Console.WriteLine();

                try
                {
                    int option = Int32.Parse(Console.ReadLine());

                    if (option == 1)
                    {
                        // deducts credits
                        MyShip.ChangeCredits(-175 * quantity);
                        // first parameter selects location dependent item
                        MyShip.ChangeItem(MyShip.GetItemID(), quantity);
                        // adds weight to cargo
                        MyShip.ChangeWeight(150 * quantity);

                        Console.Clear();
                        Console.WriteLine($"Item purchased. Current credits = {MyShip.GetCredits()}.");
                    }
                    else
                    {
                        Console.Clear();
                    }
                }
                catch (Exception)
                {
                    MainError();
                }
            }
            else
            {
                Console.Clear();
                WeightError();
            }
        }