Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your team is developing VisageTome, a social media platform to rival the greatest systems of the day. The project is off to a good start,

Your team is developing VisageTome, a social media platform to rival the greatest systems of the day. The project is off to a good start, but your manager now wants you to add notifications to the system; specifically, she wants users to be able to follow one another and receive a message when the followed user's status changes. Add an event to the User class and use it to wire up these notifications.

The system begins by executing a list of commands from a document before allowing the user to input commands.

Complete the //TODO in the following code

VisageTome.cs

using System;

using System.IO;

using System.Collections.Generic;

class VisageTome

{

#region Main

private static Dictionary users = new Dictionary();

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 ");

}

}

else if(command_split[0] == "list")

{

Console.WriteLine("Users:");

foreach(User user in users.Values)

{

Console.WriteLine("\t{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 ");

}

}

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 ");

}

}

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 ");

}

}

else if(command_split[0] == "quit")

{

return false;

}

else

{

Console.WriteLine("Usage: [command_args]");

Console.WriteLine("Valid commands: user list status follow unfollow quit");

}

}

else

{

Console.WriteLine("Usage: [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("\t{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

{

}

}

#endregion

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago