1
I am a beginner doing an internship. They give me a bidding program to implement.

简而言之,应用程序首先添加项目(ItemName、ItemPrice、OwnerName),然后投标应用程序启动。当一个项目被弹出时,一个 5 秒的计时器开始等待任何投标人。如果竞标动作完成,则等待另一个竞标者的计时器再增加 10 秒。当计时器结束时,该物品要么卖给出价最高的人,要么根本不卖。

我创建了一个项目工厂。一个菜单,可帮助用户随时添加项目并开始投标。

我遇到的问题如下:

每当从项目列表中弹出项目时,我都想使用 while 循环。我可以使用的唯一 while 循环是:while(!Time.Elapsed());

这当然是错误的。

我被要求尝试并行任务。会有帮助吗?我该如何使用它?

程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace ConsoleApplication3
{
    class Program
    {
        private static System.Timers.Timer bidTimer = new System.Timers.Timer(5000.0);
        int sold = 0;
        private static int bidPrice = 0;
        private static int lower =0;

        static void Main(string[] args)
        {

            displayMenu();

            // bid.Elapsed += bid_Elapsed;
        }

        // public class ItemP
        //{
        // public delegate void NewBidHandler(int newPrice);

        // public event NewBidHandler NewBid;

        // private bool OnNewBid()
        // {
        //    bool _success = true;

        // if (NewBid != null)

        //    if (item.Price < bidPrice)
        //   {
        //        _success = true;
        //       NewBid(bidPrice);
        //    }
        //   else
        //{
        //        _success = false;
        //     }


        //  return _success;

        //}



        // static void bid_Elapsed(object sender, ElapsedEventArgs e) // new bid (inserted price > initial price)
        // {// 2 events , first one new bid the second one sold!

        ////     throw new NotImplementedException();
        // }

        public static void displayMenu()
        {

            Console.WriteLine("-----------------------");
            Console.WriteLine("1- Add Item");
            Console.WriteLine("2-Start Bidding");
            Console.WriteLine("3- Exit");
            Console.WriteLine("-----------------------");
            var choice = getChoice();
            processChoice(choice);
        }


        public static int getChoice()
        {
            Console.WriteLine("Please Enter Selection :");
            int choice = int.Parse(Console.ReadLine());
            return choice;
        }

        public static void processChoice(int choice)
        {
            switch (choice)
            {
                case 1:

                    Console.WriteLine("Enter Item Name: ");
                    string itemName = Console.ReadLine();// Reading string name                        


                    double price;
                    Console.WriteLine("Enter Item Price: ");// Reading double price
                    if (!double.TryParse(Console.ReadLine(), out price))
                    {
                        Console.WriteLine("Error");
                    }
                    Console.WriteLine("Enter Item Owner Name: ");
                    string ownerName = Console.ReadLine();// Reading string ownerName                        

                    Factory.AddItem(itemName, price, ownerName);// adding the item (name , price , ownerName)
                    break;



                case 2:
                    {

                        foreach (var item in Factory.ItemList)
                        {
                            //Timer = 5sec;
                            Task NewBid = new Task(() =>
                                    {
                                        Console.WriteLine("Place your bids");
                                        int bidPrice = int.Parse(Console.ReadLine());
                                        if (bidPrice > item.Price)
                                        {
                                            bidTimer.Interval += 10000.0;
                                            item.Price = bidPrice;

                                        }
                                        else
                                        {
                                            Console.WriteLine("Bid is lower than original price");// no bid less than price will be taken
                                            lower = 1;
                                        }
                                    });
                            Task Timer = new Task(() => bidTimer.Start());


                            var cancelTokenSource = new CancellationTokenSource();
                            var cancelToken = cancelTokenSource.Token;
                            var biddingTask = Timer.RunSynchronously(NewBid , Timer);


                            //var biddingTask = Task.Run(
                            //        () =>
                            //        {
                            //            Console.WriteLine("Place your bids");
                            //            int bidPrice = int.Parse(Console.ReadLine());
                            //            if (bidPrice > item.Price)
                            //            {
                            //                bidTimer.Interval += 10000.0;
                            //                item.Price = bidPrice;

                            //            }
                            //            else
                            //            {
                            //                Console.WriteLine("Bid is lower than original price");// no bid less than price will be taken
                            //                lower = 1;
                            //            }
                            //        },
                            //        cancelToken
                            //    );
                            //bidTimer.Elapsed +=
                            //    (o, e) =>
                            //    {
                            //        cancelTokenSource.Cancel();
                            //    };

                            Console.WriteLine("Final price: {0}", item.Price);


                        }
                        Console.WriteLine("No more bids"); // when list is empty
                    }





                    break;








                case 3:
                    {

                        Console.ReadLine();
                        break;
                    };
                default:
                    Console.WriteLine("Wrong selection" +
                    Environment.NewLine + "Press any key for exit");
                    Console.ReadKey();
                    break;


            }

            displayMenu();
        }
    }




}

工厂:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    public class Factory
    {
        private static List<Item> _itemList;
        public static List<Item> ItemList { get{ return _itemList;} set{ _itemList = value;} }

        static Factory()
        {
            _itemList = new List<Item>();
        }

        public static void AddItem(string name, double price, string ownerName)
        {
            Item it = new Item() { ItemName = name , Price = price , OwnerName = ownerName };
            _itemList.Add(it);
            _itemList.OrderByDescending(i => i.Price);

        }


    }
}

物品:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    public class Item
    {
        public String ItemName { get; set; }
        public  Double Price { get; set; }
        public String OwnerName { get; set; }

    }
}

先感谢您

4

0 回答 0