Esempio n. 1
0
        private static void MenuSelection()
        {
            APIService api           = new APIService();
            int        menuSelection = -1;

            while (menuSelection != 0)
            {
                Console.WriteLine("");
                Console.WriteLine("Menu:");
                Console.WriteLine("1: List all auctions");
                Console.WriteLine("2: List details for specific auction");
                Console.WriteLine("3: Find auctions with a specified term in the title");
                Console.WriteLine("4: Find auctions below a specified price");
                Console.WriteLine("5: Exit");
                Console.WriteLine("---------");
                Console.Write("Please choose an option: ");

                if (!int.TryParse(Console.ReadLine(), out menuSelection))
                {
                    Console.WriteLine("Invalid input. Please enter only a number.");
                }
                else if (menuSelection == 1)
                {
                    List <Auction> auctions = api.GetAllAuctions();
                    PrintAuctions(auctions);
                }
                else if (menuSelection == 2)
                {
                    Console.Write("Please enter an auction id to get the details: ");
                    string input = Console.ReadLine();
                    if (!int.TryParse(input, out int auctionId))
                    {
                        Console.WriteLine("Invalid input. Please enter only a number.");
                    }
                    else if (auctionId > 0)
                    {
                        Auction auction = api.GetDetailsForAuction(auctionId);
                        PrintAuction(auction);
                    }
                }
                else if (menuSelection == 3)
                {
                    Console.Write("Please enter a title to search for: ");
                    string searchTitle = Console.ReadLine();
                    if (string.IsNullOrWhiteSpace(searchTitle))
                    {
                        Console.WriteLine("Invalid input. Please enter some text.");
                    }
                    else
                    {
                        List <Auction> auctions = api.GetAuctionsSearchTitle(searchTitle);
                        PrintAuctions(auctions);
                    }
                }
                else if (menuSelection == 4)
                {
                    Console.Write("Please enter a max price to search for: ");
                    string input = Console.ReadLine();
                    if (!double.TryParse(input, out double searchPrice))
                    {
                        Console.WriteLine("Invalid input. Please enter only a number.");
                    }
                    else
                    {
                        List <Auction> auctions = api.GetAuctionsSearchPrice(searchPrice);
                        PrintAuctions(auctions);
                    }
                }
                else
                {
                    Console.WriteLine("Goodbye!");
                    Environment.Exit(0);
                }
            }
        }
Esempio n. 2
0
        public void MenuSelection()
        {
            APIService api           = new APIService();
            int        menuSelection = -1;

            while (menuSelection != 0)
            {
                Console.WriteLine("");
                Console.WriteLine("Welcome to Online Auctions! Please make a selection: ");
                Console.WriteLine("1: List all auctions");
                Console.WriteLine("2: List details for specific auction");
                Console.WriteLine("3: Find auctions with a specific term in the title");
                Console.WriteLine("4: Find auctions below a specified price");
                Console.WriteLine("5: Create a new auction");
                Console.WriteLine("6: Modify an auction");
                Console.WriteLine("7: Delete an auction");
                Console.WriteLine("0: Exit");
                Console.WriteLine("---------");
                Console.Write("Please choose an option: ");

                try
                {
                    if (!int.TryParse(Console.ReadLine(), out menuSelection))
                    {
                        Console.WriteLine("Invalid input. Please enter only a number.");
                    }
                    else if (menuSelection == 1)
                    {
                        List <Auction> auctions = api.GetAllAuctions();
                        if (auctions != null)
                        {
                            PrintAuctions(auctions);
                        }
                    }
                    else if (menuSelection == 2)
                    {
                        List <Auction> auctions = api.GetAllAuctions();
                        if (auctions != null && auctions.Count > 0)
                        {
                            int auctionId = PromptForAuctionID(auctions, "get the details");
                            if (auctionId != 0)
                            {
                                Auction auction = api.GetDetailsForAuction(auctionId);
                                if (auction != null)
                                {
                                    PrintAuction(auction);
                                }
                            }
                        }
                    }
                    else if (menuSelection == 3)
                    {
                        string searchTitle = PromptForSearchTitle();
                        if (!string.IsNullOrWhiteSpace(searchTitle))
                        {
                            List <Auction> auctions = api.GetAuctionsSearchTitle(searchTitle);
                            if (auctions != null)
                            {
                                PrintAuctions(auctions);
                            }
                            else
                            {
                                Console.WriteLine("No auction found for search title: " + searchTitle);
                            }
                        }
                    }
                    else if (menuSelection == 4)
                    {
                        double maxPrice = PromptForMaxPrice();
                        if (maxPrice > 0)
                        {
                            List <Auction> auctions = api.GetAuctionsSearchPrice(maxPrice);
                            if (auctions != null)
                            {
                                PrintAuctions(auctions);
                            }
                            else
                            {
                                Console.WriteLine("No auctions found under max price: " + maxPrice.ToString("C"));
                            }
                        }
                    }
                    else if (menuSelection == 5)
                    {
                        string  newAuctionString = PromptForAuctionData();
                        Auction auctionToAdd     = new Auction(newAuctionString);
                        if (auctionToAdd.IsValid)
                        {
                            Auction addedAuction = api.AddAuction(auctionToAdd);
                            if (addedAuction != null)
                            {
                                Console.WriteLine("Auction successfully added.");
                            }
                            else
                            {
                                Console.WriteLine("Auction not added.");
                            }
                        }
                    }
                    else if (menuSelection == 6)
                    {
                        // Update an existing auction
                        List <Auction> auctions = api.GetAllAuctions();
                        if (auctions != null)
                        {
                            int     auctionId  = PromptForAuctionID(auctions, "update");
                            Auction oldAuction = api.GetDetailsForAuction(auctionId);
                            if (oldAuction != null)
                            {
                                string  updatedAuctionString = PromptForAuctionData(oldAuction);
                                Auction auctionToUpdate      = new Auction(updatedAuctionString);
                                if (auctionToUpdate.IsValid)
                                {
                                    Auction updatedAuction = api.UpdateAuction(auctionToUpdate);
                                    if (updatedAuction != null)
                                    {
                                        Console.WriteLine("Auction successfully updated.");
                                    }
                                    else
                                    {
                                        Console.WriteLine("Auction not updated.");
                                    }
                                }
                            }
                        }
                    }
                    else if (menuSelection == 7)
                    {
                        // Delete auction
                        List <Auction> auctions = api.GetAllAuctions();
                        if (auctions != null)
                        {
                            int  auctionId     = PromptForAuctionID(auctions, "delete");
                            bool deleteSuccess = api.DeleteAuction(auctionId);
                            if (deleteSuccess)
                            {
                                Console.WriteLine("Auction successfully deleted.");
                            }
                            else
                            {
                                Console.WriteLine("Auction not deleted.");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Goodbye!");
                        Environment.Exit(0);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }