Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using System; using System.IO; using System.Collections.Generic; class VisageTom

ID: 3920187 • Letter: U

Question

using System;
using System.IO;
using System.Collections.Generic;

class VisageTome
{
    #region Main
    private static Dictionary<string, User> users = new Dictionary<string, User>();
    static void Main()
    {
        Console.WriteLine("Welcome to VisageTome!");

        Console.WriteLine("Executing from file...");
        foreach (string line in File.ReadAllLines("commands.txt"))
        {
            Console.WriteLine(" > {0}", line);
            ProcessCommand(line);
        }

        Console.WriteLine(" Executing from user...");
        bool continue_user_input = true;
        while (continue_user_input)
        {
            Console.Write(" > ");
            string command = Console.ReadLine();
            continue_user_input = ProcessCommand(command);
        }
    }

    static bool ProcessCommand(string command)
    {
        string[] command_split = command.Split(' ');
        if (command_split.Length >= 1)
        {
            if (command_split[0] == "user")
            {
                if (command_split.Length >= 2)
                {
                    string new_user = command_split[1];
                    if (users.ContainsKey(new_user))
                    {
                        Console.WriteLine("User {0} already exists.", new_user);
                    }
                    else
                    {
                        users.Add(new_user, new User(new_user));
                        Console.WriteLine("User {0} added.", new_user);
                    }
                }
                else
                {
                    Console.WriteLine("Usage: user <username>");
                }
            }
            else if (command_split[0] == "list")
            {
                Console.WriteLine("Users:");
                foreach (User user in users.Values)
                {
                    Console.WriteLine(" {0}: {1}", user.Name, user.Status);
                }
            }
            else if (command_split[0] == "status")
            {
                if (command_split.Length >= 3)
                {
                    string user = command_split[1];
                    if (users.ContainsKey(user))
                    {
                        string status = command.Replace("status " + user + " ", "");
                        Console.WriteLine("User {0} status changing to "{1}".", user, status);
                        users[user].Status = status;
                    }
                    else
                    {
                        Console.WriteLine("User {0} does not exist.", user);
                    }
                }
                else
                {
                    Console.WriteLine("Usage: status <username> <new status>");
                }
            }
            else if (command_split[0] == "follow")
            {
                if (command_split.Length >= 3)
                {
                    string follower = command_split[1];
                    string target = command_split[2];
                    if (users.ContainsKey(follower) && users.ContainsKey(target))
                    {
                        users[follower].FollowUser(users[target]);
                        Console.WriteLine("User {0} now following user {1}.", follower, target);
                    }
                    else
                    {
                        Console.WriteLine("User {0} and/or user {1} not found.", follower, target);
                    }
                }
                else
                {
                    Console.WriteLine("Usage: follow <follower> <target>");
                }
            }
            else if (command_split[0] == "unfollow")
            {
                if (command_split.Length >= 3)
                {
                    string follower = command_split[1];
                    string target = command_split[2];
                    if (users.ContainsKey(follower) && users.ContainsKey(target))
                    {
                        users[follower].UnfollowUser(users[target]);
                        Console.WriteLine("User {0} no longer following user {1}.", follower, target);
                    }
                    else
                    {
                        Console.WriteLine("User {0} and/or user {1} not found.", follower, target);
                    }
                }
                else
                {
                    Console.WriteLine("Usage: unfollow <follower> <target>");
                }
            }
            else if (command_split[0] == "quit")
            {
                return false;
            }
            else
            {
                Console.WriteLine("Usage: <command> [command_args]");
                Console.WriteLine("Valid commands: user list status follow unfollow quit");
            }
        }
        else
        {
            Console.WriteLine("Usage: <command> [command_args]");
            Console.WriteLine("Valid commands: user status follow unfollow quit");
        }
        return true;
    }
    #endregion

    #region User Class
    class User
    {
        private string name;
        public string Name { get { return name; } }
        private string status;
        public string Status
        {
            get { return status; }
            set
            {
                if (value != status)
                {
                    status = value;
                    //TODO: Invoke an event that notifies subscribers that this user's status has changed

                }
            }
        }
        //TODO: Declare an EventHandler delegate to handle subscribers

        public User(string name)
        {
            this.name = name;
            status = "(No status yet)";
        }
        public void FollowUser(User other)
        {
            //TODO: Let this user subscribe to the status changed event of the other user;
            //      be sure to keep the subscriber from subscribing multiple times

        }
        public void UnfollowUser(User other)
        {
            //TODO: Let this user unsubscribe from the status changed event of the other user
        }
        private void ReceiveStatusChanged(object sender, StatusChangedEventArgs e)
        {
            //In a full application, this might involve sending an email or a pop-up to this user
            Console.WriteLine(" {0} has been notified that {1}'s status has just changed to "{2}".", Name, e.Name, e.NewStatus);
        }
        //TODO: Properly finish the StatusChangedEventArgs class (see ReceiveStatusChanged for a use example)
        public class StatusChangedEventArgs : EventArgs
        {
            public string Name;
            public string NewStatus;
        }
    }
    #endregion
}

Fill in the "TODO"

Explanation / Answer

// Please find updated user class code below.

// Find all changes in bold to compare.

public class User
{
private string name;
public string Name { get { return name; } }

private string status;
public string Status
{
get { return status; }
set
{
if (value != status)
{
status = value;

// Raise status change event, this will notify to all users who has initialize the event.
StatusChanged?.Invoke(new StatusChangedEventArgs(Name, value));

}
}
}
// Event declaration
public event Action<StatusChangedEventArgs> StatusChanged;

public User(string name)
{
this.name = name;
status = "(No status yet)";
}
public void FollowUser(User other)
{

// Subscribe
other.StatusChanged += ReceiveStatusChanged;

}

public void UnfollowUser(User other)
{

// Unsubscribe
other.StatusChanged -= ReceiveStatusChanged;

}
private void ReceiveStatusChanged(StatusChangedEventArgs e)
{
//In a full application, this might involve sending an email or a pop-up to this user
Console.WriteLine(" {0} has been notified that {1}'s status has just changed to "{2}".", Name, e.Name, e.NewStatus);
}
  
public class StatusChangedEventArgs : EventArgs
{
public string Name;
public string NewStatus;

public StatusChangedEventArgs(string name, string status)
{
Name = name;
NewStatus = status;
}

}
}