コード例 #1
0
ファイル: Story.cs プロジェクト: shadow4896/SpaceGame
        public void Supply(Ship ship)
        {
            bool doneTrading = false;

            // While we're not done trading
            while (!doneTrading)
            {
                Console.Clear();
                Console.WriteLine($"Available capacity: {ship.capacity - ship.productsInHold()}lbs");
                Console.WriteLine("Select from the following choices:\n\n1. Food\n2. Andriod\n3. Weapons\n4. Entertainment\n5. Oil\n6. Done trading");

                // Loop in either case until the user enters valid input
                // Accept an item to buy
                bool done       = false;
                int  chosenItem = 0;
                int  numToBuy   = 0;

                Goods good = new Goods();

                while (!done)
                {
                    var key = Console.ReadKey().Key;
                    Console.WriteLine();

                    done = true;

                    switch (key)
                    {
                    case ConsoleKey.D1:
                        good.name   = "pallets of food";
                        good.cost   = 5;
                        good.weight = 20;
                        break;

                    case ConsoleKey.D2:
                        good.name   = "andriods";
                        good.cost   = 20;
                        good.weight = 50;
                        break;

                    case ConsoleKey.D3:
                        good.name   = "weapon crates?";
                        good.cost   = 100;
                        good.weight = 100;
                        break;

                    case ConsoleKey.D4:
                        good.name   = "entertainment";
                        good.cost   = 10;
                        good.weight = 5;
                        break;

                    case ConsoleKey.D5:
                        good.name   = "barrels of oil";
                        good.cost   = 100;
                        good.weight = 200;
                        break;

                    case ConsoleKey.D6:
                        doneTrading = true;
                        break;

                    default:
                        done = false;
                        break;
                    }

                    if (!done && !doneTrading)
                    {
                        Console.WriteLine("\nInvalid choice. Try again.");
                    }
                }

                if (doneTrading)
                {
                    continue;
                }

                // Accept a # of the chosen item to purchase
                done = false;

                while (!done)
                {
                    Console.Write($"How many {good.name}? ");

                    try
                    {
                        numToBuy = int.Parse(Console.ReadLine());

                        if (good.weight * numToBuy + ship.productsInHold() > ship.capacity)
                        {
                            Console.WriteLine($"Puchasing that many {good.name} will overload your ship. Try buying less.");
                            continue;
                        }

                        done = true;
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Invalid input. Try again.");
                    }
                }

                // We have a valid item to purchase and a valid quantity of that item.
                Console.WriteLine($"Purchasing {numToBuy} of item {chosenItem}");

                // Add the item to the inventory
                ship.hold.Add((good, numToBuy));
            }
        }