Facade design pattern

Trol00311

New member
Joined
Dec 3, 2021
Messages
1
Programming Experience
Beginner
hello i have a problem with the task.
Here is the content of the task:

The account creation process is as follows:
1. validating email address
2.checking if the email is free (email is a unique user identifier)
3.Adding a user to the database (a list for the purpose of the task)
4.sending an e-mail to the user confirming the creation of an account

We will start with the implementation of the missing code fragments (TODO, NotImplementedException), then we will create a method to delete the user.

Removing a user should consist of:

Removing a user should consist of:
1.checking if the user exists (if not, then terminate the process with throwing an exception)
2.removing the user from the database (a list for the purpose of the task)
3.sending an e-mail to the user confirming that the account has been deleted
To prove the operation of the new method, print the state of the 'users' list in the console before executing the method, then call it for an existing user and print the state of the list again.

For example:

Before delete: 10 Invoke delete operations for john.doe@gmail.com After delete: 9

and code:

C#:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace WzorzecFasada
{

    interface IUserService
    {
        void CreateUser(string email);
    }

    static class EmailNotification
    {
        public static void SendEmail(string to, string subject)
        {
            Console.WriteLine("Sending an email");
        }
    }

    class UserRepository
    {
        public readonly List<string> users = new List<string>
        {
            "john.doe@gmail.com", "sylvester.stallone@gmail.com"
        };

        public bool IsEmailFree(string email)
        {
        
          int a = 0;
            
            //whether user available
            foreach (var x in UserRepository.users)
            {
              if(IsEmailFree.email==x)
              a++;
            }
            if(a!=0)
            {
              throw new NotImplementedException();
            }
        }

        public void AddUser(string email)
        {
            throw new NotImplementedException();
            //Add user to list
            UserRepository.users.Add(email);
        }
    }

    static class Validators
    {
        public static bool IsValidEmail(string email)
        {
            return Regex.IsMatch(email,
                    @"^[^@\s]+@[^@\s]+\.[^@\s]+$",
                    RegexOptions.IgnoreCase);
        }
    }

    class UserService : IUserService
    {
        private readonly UserRepository userRepository = new UserRepository();
        public void CreateUser(string email)
        {
            if (!Validators.IsValidEmail(email))
            {
                throw new ArgumentException("Błędny email");
            }

            // TODO: add check if the email is free, if not throw an exception, if so, continue with the function

            userRepository.AddUser(email);
            EmailNotification.SendEmail(email, "Welcome to our service");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUserService userService = new UserService();
            userService.CreateUser("someemail@gmail.com");
        }
    }

}

I tried to solve it, but to no avail...
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,245
Location
Chesapeake, VA
Programming Experience
10+
What specifically are having an issue with? Let's tackle that first.

The trick with programming is solving one small problem at a time. Trying to do it all in one big bang just makes things too complex. (And yes, I do find it ironic that the facade pattern is supposed to hide complexity. ;) )
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,466
Location
Sydney, Australia
Programming Experience
10+
You have at least 7 separate steps there, and each of those can likely be broken down into smaller steps. Ignore everything else and concentrate on this first:
1. validating email address
Can you do that? If not, what's the problem? If you had to validate an email address manually, what would you do? If you don't know, find out first. You can write code to do something if you don't know what that something is. Etc.
 

Latest posts

Top Bottom