public static WallpaperFranchise GetOrCreate(string franchiseName, WallpaperType type) { if (string.IsNullOrWhiteSpace(franchiseName)) { throw new ArgumentException(nameof(franchiseName)); } foreach (var currentFranchise in WallpaperManager.Franchises) { if (currentFranchise.Name == franchiseName) { return(currentFranchise); } } var newFranchise = new WallpaperFranchise(franchiseName, type); WallpaperManager.Franchises.Add(newFranchise); return(newFranchise); }
/// <summary> /// Gets the franchise with the given name. If it does not exit the user is asked if it should be created. /// </summary> /// <param name="franchiseName">The name of the franchise to get or create.</param> /// <param name="wallpaperFranchiseType">The type of the franchise.</param> /// <param name="wallpaperFranchise">The found or created franchise.</param> /// <returns>If new franchise was created.</returns> private void GetOrCreateFranchise(string franchiseName, WallpaperType wallpaperFranchiseType, bool ignoreMissing, out WallpaperFranchise wallpaperFranchise) { if (ignoreMissing) { wallpaperFranchise = WallpaperFranchise.GetOrCreate(franchiseName, wallpaperFranchiseType); } else { if (WallpaperFranchise.TryGet(franchiseName, out WallpaperFranchise? foundFranchise)) //Check if franchise already exists { if (foundFranchise.Type == wallpaperFranchiseType) { //All okay wallpaperFranchise = foundFranchise; } else { throw new WallpaperBuildExeption($"Given and found {nameof(WallpaperType)} do not match"); } } else { Console.Write($"The wallpaper franchise with the name '{franchiseName}' does not exist. It would belong to the type '{wallpaperFranchiseType.Name}'.\nDo you want to create it?"); if (UserInterface.GetUserInput()) { wallpaperFranchise = new WallpaperFranchise(franchiseName, wallpaperFranchiseType); WallpaperManager.Franchises.Add(wallpaperFranchise); //Add this instances franchise to the list of all franchises. //This step is necessary, as we are creating it here outside of the class. } else { Console.WriteLine(); throw new WallpaperBuildExeption("Franchise not found and not created.", true, wallpaperFranchiseType); } Console.WriteLine(); } } }