示例#1
0
        public void StartUI()
        {
            while (true)
            {
                //Variables
                #region
                int selectedId;

                int    birthYear;
                int    birthMonth;
                int    birthDay;
                int    soldYear;
                int    soldMonth;
                int    soldDay;
                int    previousOwnerId;
                double price;

                Pet   pet   = new Pet();
                Owner owner = new Owner();
                #endregion

                switch (ShowMenu())
                {
                case 1:
                    //Creation of pet
                    #region
                    Console.Write("\n\tName of the pet: ");
                    pet.name = Console.ReadLine();
                    Console.Write("\n\tType of the pet: ");
                    pet.type = Console.ReadLine();

                    Console.Write("\n\tBirth year of the pet: ");
                    while (!int.TryParse(Console.ReadLine(), out birthYear) || birthYear < 2000 || birthYear > DateTime.Now.Year)
                    {
                        Console.WriteLine("\nSelect a number between 2000 and {0}: ", DateTime.Now.Year);
                    }

                    Console.Write("\n\tBirth month of the pet: ");
                    while (!int.TryParse(Console.ReadLine(), out birthMonth) || birthMonth < 1 || birthMonth > 12)
                    {
                        Console.WriteLine("\nSelect a number between 1 and 12");
                    }

                    Console.Write("\n\tBirth day of the pet: ");
                    while (!int.TryParse(Console.ReadLine(), out birthDay) || birthDay < 1 || birthDay > 31)
                    {
                        Console.WriteLine("\nSelect a number between 1 and 31: ");
                    }
                    pet.birthDate = new DateTime(birthYear, birthMonth, birthDay);

                    Console.Write("\n\tPet sold year: ");
                    while (!int.TryParse(Console.ReadLine(), out soldYear) || soldYear < 2000 || soldYear > DateTime.Now.Year)
                    {
                        Console.WriteLine("\nSelect a number between 2000 and {0}: ", DateTime.Now.Year);
                    }

                    Console.Write("\n\tPet sold month: ");
                    while (!int.TryParse(Console.ReadLine(), out soldMonth) || soldMonth < 1 || soldMonth > 12)
                    {
                        Console.WriteLine("\nSelect a number between 1 and 12");
                    }

                    Console.Write("\n\tPet sold day: ");
                    while (!int.TryParse(Console.ReadLine(), out soldDay) || soldDay < 1 || soldDay > 31)
                    {
                        Console.WriteLine("\nSelect a number between 1 and 31: ");
                    }
                    pet.soldDate = new DateTime(soldYear, soldMonth, soldDay);

                    Console.Write("\n\tColor of pet: ");
                    pet.color = Console.ReadLine();

                    Console.Write("\n\tId of previous owner between 1 and {0}: ", _petShopService.GetOwners().Count);
                    while (!int.TryParse(Console.ReadLine(), out previousOwnerId) || previousOwnerId < 1 || previousOwnerId > _petShopService.GetOwners().Count)
                    {
                        Console.WriteLine("\nSelect a number between 1 and {0}: ", _petShopService.GetOwners().Count);
                    }

                    Console.Write("\n\tPrice: ");
                    while (!double.TryParse(Console.ReadLine(), out price))
                    {
                        Console.WriteLine("\nSelect a number: ");
                    }
                    pet.price = price;

                    _petShopService.CreatePet(pet, previousOwnerId);

                    break;

                    #endregion
                case 2:
                    //Reading of pets in DB
                    #region
                    foreach (var item in _petShopService.GetPets())
                    {
                        Console.Write("\n\tId: " + item.id);
                        Console.Write("\tName: " + item.name);
                        Console.Write("\tType: " + item.type);
                        Console.Write("\tBirthdate: " + item.birthDate);
                        Console.Write("\tSolddate: " + item.soldDate);
                        Console.Write("\tColor: " + item.color);
                        Console.Write("\tPrevious owner: " + item.previousOwner.id);
                        Console.Write("\tPrice: " + item.price);
                    }

                    Console.WriteLine("\n\nPress Enter to return to menu");
                    Console.ReadLine();

                    break;

                    #endregion
                case 3:
                    //Search for pets by type
                    #region
                    Console.Write("\nWhat type of pet do you want to search for: ");
                    foreach (var item in _petShopService.GetPetsByType(Console.ReadLine()))
                    {
                        Console.Write("\n\tId: " + item.id);
                        Console.Write("\tName: " + item.name);
                        Console.Write("\tType: " + item.type);
                        Console.Write("\tBirthdate: " + item.birthDate);
                        Console.Write("\tSolddate: " + item.soldDate);
                        Console.Write("\tColor: " + item.color);
                        Console.Write("\tPrevious owner: " + item.color);
                        Console.Write("\tPrice: " + item.price);
                    }

                    Console.WriteLine("\n\nPress Enter to return to menu");
                    Console.ReadLine();

                    break;

                    #endregion
                case 4:
                    //Get 5 cheapest pets
                    #region
                    foreach (var item in _petShopService.Get5CheapestPets())
                    {
                        Console.Write("\n\tId: " + item.id);
                        Console.Write("\tName: " + item.name);
                        Console.Write("\tType: " + item.type);
                        Console.Write("\tBirthdate: " + item.birthDate);
                        Console.Write("\tSolddate: " + item.soldDate);
                        Console.Write("\tColor: " + item.color);
                        Console.Write("\tPrevious owner: " + item.color);
                        Console.Write("\tPrice: " + item.price);
                    }

                    Console.WriteLine("\n\nPress Enter to return to menu");
                    Console.ReadLine();

                    break;

                    #endregion
                case 5:
                    //Get pets by price
                    #region
                    foreach (var item in _petShopService.GetPetsByPrice())
                    {
                        Console.Write("\n\tId: " + item.id);
                        Console.Write("\tName: " + item.name);
                        Console.Write("\tType: " + item.type);
                        Console.Write("\tBirthdate: " + item.birthDate);
                        Console.Write("\tSolddate: " + item.soldDate);
                        Console.Write("\tColor: " + item.color);
                        Console.Write("\tPrevious owner: " + item.color);
                        Console.Write("\tPrice: " + item.price);
                    }

                    Console.WriteLine("\n\nPress Enter to return to menu");
                    Console.ReadLine();

                    break;

                    #endregion
                case 6:
                    //Update pet
                    #region
                    Console.Write("\nSelect pet id between 1 and {0}, or type any ohter key to cancel: ", _petShopService.GetPets().Count);
                    if (int.TryParse(Console.ReadLine(), out selectedId) || selectedId < 0 || selectedId > _petShopService.GetPets().Count)
                    {
                        Console.Write("\n\tNew name of the pet: ");
                        pet.name = Console.ReadLine();
                        Console.Write("\n\tNew type of the pet: ");
                        pet.type = Console.ReadLine();

                        Console.Write("\n\tNew birth year of the pet: ");

                        while (!int.TryParse(Console.ReadLine(), out birthYear) || birthYear < 2000 || birthYear > DateTime.Now.Year)
                        {
                            Console.WriteLine("\nSelect a number between 2000 and {0}: ", DateTime.Now.Year);
                        }

                        Console.Write("\n\tNew birth month of the pet: ");

                        while (!int.TryParse(Console.ReadLine(), out birthMonth) || birthMonth < 1 || birthMonth > 12)
                        {
                            Console.WriteLine("\nSelect a number between 1 and 12");
                        }

                        Console.Write("\n\tNew birth date of the pet: ");

                        while (!int.TryParse(Console.ReadLine(), out birthDay) || birthDay < 1 || birthDay > 31)
                        {
                            Console.WriteLine("\nSelect a number between 1 and 31: ");
                        }
                        pet.birthDate = new DateTime(birthYear, birthMonth, birthDay);

                        Console.Write("\n\tNew pet sold year: ");

                        while (!int.TryParse(Console.ReadLine(), out soldYear) || soldYear < 2000 || soldYear > DateTime.Now.Year)
                        {
                            Console.WriteLine("\nSelect a number between 2000 and {0}: ", DateTime.Now.Year);
                        }

                        Console.Write("\n\tNew pet sold month: ");

                        while (!int.TryParse(Console.ReadLine(), out soldMonth) || soldMonth < 1 || soldMonth > 12)
                        {
                            Console.WriteLine("\nSelect a number between 1 and 12");
                        }

                        Console.Write("\n\tNew pet sold date: ");

                        while (!int.TryParse(Console.ReadLine(), out soldDay) || soldDay < 1 || soldDay > 31)
                        {
                            Console.WriteLine("\nSelect a number between 1 and 31: ");
                        }
                        pet.soldDate = new DateTime(soldYear, soldMonth, soldDay);

                        Console.Write("\n\tNew color of pet: ");
                        pet.color = Console.ReadLine();

                        Console.Write("\n\tNew id of previous owner between 1 and {0}: ", _petShopService.GetOwners().Count);
                        while (!int.TryParse(Console.ReadLine(), out previousOwnerId) || previousOwnerId < 1 || previousOwnerId > _petShopService.GetOwners().Count)
                        {
                            Console.WriteLine("\nSelect a number between 1 and {0}: ", _petShopService.GetOwners().Count);
                        }

                        Console.Write("\n\tNew price: ");
                        while (!double.TryParse(Console.ReadLine(), out price))
                        {
                            Console.WriteLine("\nSelect a number: ");
                        }
                        pet.price = price;

                        _petShopService.UpdatePet(selectedId, pet, previousOwnerId);
                    }
                    else
                    {
                        break;
                    }

                    break;

                    #endregion
                case 7:
                    //Deletion of pet
                    #region
                    Console.Write("\nSelect pet id between 1 and {0}, or type any ohter key to cancel: ", _petShopService.GetPets().Count);
                    if (int.TryParse(Console.ReadLine(), out selectedId) || selectedId < 0 || selectedId > _petShopService.GetPets().Count)
                    {
                        _petShopService.DeletePet(selectedId);
                    }
                    else
                    {
                        break;
                    }

                    break;

                    #endregion
                case 8:
                    //Creation of owner
                    #region
                    Console.Write("\n\tOwners firstname: ");
                    owner.firstName = Console.ReadLine();
                    Console.Write("\n\tOwners lastname: ");
                    owner.lastName = Console.ReadLine();
                    Console.Write("\n\tOwners address: ");
                    owner.address = Console.ReadLine();
                    Console.Write("\n\tOwners phonenumber: ");
                    owner.phoneNumber = Console.ReadLine();
                    Console.Write("\n\tOwners email: ");
                    owner.email = Console.ReadLine();

                    _petShopService.CreateOwner(owner);

                    break;

                    #endregion
                case 9:
                    //Reading of owners in DB
                    #region
                    foreach (var item in _petShopService.GetOwners())
                    {
                        Console.Write("\n\tId: " + item.id);
                        Console.Write("\tFirstname: " + item.firstName);
                        Console.Write("\tLastName: " + item.lastName);
                        Console.Write("\tAddress: " + item.address);
                        Console.Write("\tPhonenumber: " + item.phoneNumber);
                        Console.Write("\tEmail: " + item.email);
                    }

                    Console.WriteLine("\n\nPress Enter to return to menu");
                    Console.ReadLine();

                    break;

                    #endregion
                case 10:
                    //Update owner
                    #region
                    Console.Write("\nSelect owner id between 1 and {0}, or type any ohter key to cancel: ", _petShopService.GetOwners().Count);
                    if (int.TryParse(Console.ReadLine(), out selectedId) || selectedId < 0 || selectedId > _petShopService.GetOwners().Count)
                    {
                        Console.Write("\n\tOwners new firstname: ");
                        owner.firstName = Console.ReadLine();
                        Console.Write("\n\tOwners new lastname: ");
                        owner.lastName = Console.ReadLine();
                        Console.Write("\n\tOwners new address: ");
                        owner.address = Console.ReadLine();
                        Console.Write("\n\tOwners new phonenumber: ");
                        owner.phoneNumber = Console.ReadLine();
                        Console.Write("\n\tOwners new email: ");
                        owner.email = Console.ReadLine();
                    }
                    else
                    {
                        break;
                    }

                    _petShopService.UpdateOwner(selectedId, owner);

                    break;

                    #endregion
                case 11:
                    //Deletion of owner
                    #region
                    Console.Write("\nSelect owner id between 1 and {0}, or type any ohter key to cancel: ", _petShopService.GetOwners().Count);
                    if (int.TryParse(Console.ReadLine(), out selectedId) || selectedId < 0 || selectedId > _petShopService.GetOwners().Count)
                    {
                        _petShopService.DeleteOwner(selectedId);
                    }
                    else
                    {
                        break;
                    }

                    break;

                    #endregion
                case 12:
                    //Exit
                    Environment.Exit(0);

                    break;
                }
            }
        }
示例#2
0
 public ActionResult <List <Pet> > Get()
 {
     return(_petShopService.GetPets());
 }
示例#3
0
        public void StartUI()
        {
            Console.WriteLine("Welcome To PetShop");
            Console.WriteLine("1: Create a pet");
            Console.WriteLine("2: List pets");
            Console.WriteLine("3: Edit pet");
            Console.WriteLine("4: Delete Pet");
            Console.WriteLine("5: Search pet by type");
            Console.WriteLine("6: Sort pets by price from low to high");
            Console.WriteLine("7: List the 5 cheapest pets");

            Console.WriteLine("\n0: Exit");

            bool isNumber = int.TryParse(Console.ReadLine(), out int number);

            if (isNumber)
            {
                while (number > 0)
                {
                    switch (number)
                    {
                    case 1:
                        Console.WriteLine("Name of the pet?");
                        string petName = Console.ReadLine();
                        Console.WriteLine("What type is the pet?");
                        string petType = Console.ReadLine();
                        Console.WriteLine("What date was the pet born? (YYYY-MM-DD)");
                        DateTime petBirthday = ValidDateTime();
                        Console.WriteLine("What date was the pet sold? (YYYY-MM-DD)");
                        DateTime petSoldDate = ValidDateTime();
                        Console.WriteLine("What color is the pet?");
                        string petColer = Console.ReadLine();
                        Console.WriteLine("Who was the previous owner of the pet?");
                        string previousOwner = Console.ReadLine();
                        Console.WriteLine("what is the price of the pet?");
                        double petPrice = ValidDouble();

                        Pet newPet = _petShopService.newPet(petName, petType, petBirthday, petSoldDate, petColer, previousOwner, petPrice);
                        _petShopService.CreatePet(newPet);
                        Console.WriteLine("Pet has been created!");

                        StartUI();
                        break;

                    case 2:
                        Console.WriteLine("Pet list\n");
                        var pets = _petShopService.GetPets();
                        ListPets(pets);

                        StartUI();
                        break;

                    case 3:
                        var petIdToEdit = PrintFindPetID();
                        var petToEdit   = _petShopService.FindPetByID(petIdToEdit);
                        Console.WriteLine($"Pet Choosen to update: {petToEdit.PetName}");
                        Console.WriteLine("Pet pame");
                        var newName = Console.ReadLine();
                        Console.WriteLine("Pet type");
                        var newtype = Console.ReadLine();
                        Console.WriteLine("Pet birthday");
                        var newBirthday = ValidDateTime();
                        Console.WriteLine("Pet salesdate");
                        var newSoldDay = ValidDateTime();
                        Console.WriteLine("Pet Coler");
                        var newColor = Console.ReadLine();
                        Console.WriteLine("Previous Owner");
                        var newPreviousOwner = Console.ReadLine();
                        Console.WriteLine("Pet Price");
                        var newPrice = ValidDouble();

                        _petShopService.UpdatePet(new Pet()
                        {
                            PetId         = petIdToEdit,
                            PetName       = newName,
                            PetType       = newtype,
                            Birthday      = newBirthday,
                            SoldDate      = newSoldDay,
                            PetColor      = newColor,
                            PreviousOwner = newPreviousOwner,
                            PetPrice      = newPrice
                        });


                        StartUI();
                        break;

                    case 4:
                        var petIdToDelete = PrintFindPetID();
                        _petShopService.DeletePet(petIdToDelete);

                        StartUI();
                        break;

                    case 5:
                        Console.WriteLine("Type of pet to search");
                        var        searchedType = Console.ReadLine();
                        List <Pet> listOfPetsWithSearchedType = _petShopService.FindPetsByType(searchedType);
                        ListPets(listOfPetsWithSearchedType);

                        StartUI();
                        break;

                    case 6:
                        List <Pet> sortedByPrice = _petShopService.GetListSortedByPrice();
                        ListPets(sortedByPrice);

                        StartUI();
                        break;

                    case 7:
                        List <Pet> sortedByPrice5 = _petShopService.GetListSortedByPrice();
                        List5Pets(sortedByPrice5);

                        StartUI();
                        break;

                    default:
                        break;
                    }
                    break;
                }
            }
            else
            {
                Console.WriteLine("Please input a valide number from the option menu");
                StartUI();
            }

            void ListPets(List <Pet> pets)
            {
                Console.WriteLine("\nList of Movies");
                foreach (var pet in pets)
                {
                    Console.WriteLine($" Pet ID: {pet.PetId} \n " +
                                      $"Pet Name: {pet.PetName} \n " +
                                      $"Pet Type: {pet.PetType} \n " +
                                      $"Pet Birthday: {pet.Birthday} \n " +
                                      $"Pet Sold: {pet.SoldDate} \n " +
                                      $"Pet color: {pet.PetColor} \n " +
                                      $"Previous Owner: {pet.PreviousOwner} \n " +
                                      $"Pet Price: {pet.PetPrice} kr \n");
                }
                Console.WriteLine("\n");
            }

            void List5Pets(List <Pet> pets)
            {
                for (int i = 0; i < 5; i++)
                {
                    var pet = pets[i];
                    Console.WriteLine($" Pet ID: {pet.PetId} \n " +
                                      $"Pet Name: {pet.PetName} \n " +
                                      $"Pet Type: {pet.PetType} \n " +
                                      $"Pet Birthday: {pet.Birthday} \n " +
                                      $"Pet Sold: {pet.SoldDate} \n " +
                                      $"Pet color: {pet.PetColor} \n " +
                                      $"Previous Owner: {pet.PreviousOwner} \n " +
                                      $"Pet Price: {pet.PetPrice} kr \n");
                }
            }

            int PrintFindPetID()
            {
                Console.WriteLine("Type a Pet ID : ");
                int id;

                while (!int.TryParse(Console.ReadLine(), out id))
                {
                    Console.WriteLine("Not a valid ID");
                }
                return(id);
            }

            DateTime ValidDateTime()
            {
                DateTime datetime;

                while (!DateTime.TryParse(Console.ReadLine(), out datetime))
                {
                    Console.WriteLine("Not a valid date, try again (YYYY-MM-DD)");
                }
                return(datetime);
            }

            double ValidDouble()
            {
                double price;

                while (!double.TryParse(Console.ReadLine(), out price))
                {
                    Console.WriteLine("price must be a number without any text");
                }
                return(price);
            }
        }