Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs-142:facespace [2015/06/02 18:44] (current)
cs142ta created
Line 1: Line 1:
 +=Facespace=
 +== Problem == 
 +* You plan to build Facespace, a new social media app
 +* You need a program to keep track of comments
 +* Your app will allow 
 +* users to make a comment
 +* users to edit a comment
 +* users to like a comment
 +* What data type(s) would we use to represent a comment?
 +* Build a Comment class and write a program to test it
 +== Solution ==
 +=== Source.cpp ===
 +<code cpp>
 +#include <​iostream>​
 +#include <​string>​
 +#include <​vector>​
 +#include "​Comment.h"​ // This tells the program what a Comment is
  
 +using namespace std;
 +
 +const int ADD_COMMENT_OPTION = 1;
 +const int EDIT_COMMENT_OPTION = 2;
 +const int LIKE_COMMENT_OPTION = 3;
 +
 +// Remember, we can separate declaration and implementation of static functions
 +int prompt_for_option();​
 +int prompt_for_comment_index();​
 +void add_comment(vector<​Comment>​ & comments);
 +void edit_comment(vector<​Comment>​ & comments);
 +void like_comment(vector<​Comment>​ & comments);
 +
 +int main()
 +{
 + // We store a vector of Comment objects, each of which has several data members
 + vector<​Comment>​ comments;
 +
 + // We want the program to run forever
 + while (true)
 + {
 + // display comments everytime before displaying menu
 + for (int i = 0; i < comments.size();​ i++)
 + {
 + cout << i << ". " << comments[i].to_string() << endl;
 + }
 +
 + // display menu and prompt for user's option
 + int user_option = prompt_for_option();​
 +
 + if (user_option == ADD_COMMENT_OPTION)
 + {
 + cout << "ADD COMMENT"​ << endl;
 + add_comment(comments);​
 +
 + }
 + else if (user_option == EDIT_COMMENT_OPTION)
 + {
 + cout << "EDIT COMMENT"​ << endl;
 + edit_comment(comments);​
 +
 + }
 + else if (user_option == LIKE_COMMENT_OPTION)
 + {
 + cout << "LIKE COMMENT"​ << endl;
 + like_comment(comments);​
 + }
 + cout << endl;
 + }
 + system("​pause"​);​
 + return 0;
 +}
 +
 +/*
 +Print menu and prompt for an option (no input validation)
 +@return user's option as an integer
 +*/
 +int prompt_for_option()
 +{
 + cout << "​*************MENU*****************"​ << endl
 + << ADD_COMMENT_OPTION << ". Add a comment"​ << endl
 + << EDIT_COMMENT_OPTION << ". Edit a comment"​ << endl
 + << LIKE_COMMENT_OPTION << ". Like a comment"​ << endl
 + << "​Option:";​
 +
 + int input;
 + cin >> input;
 +
 + cout << "​******************************"​ << endl;
 + return input;
 +}
 +
 +/*
 +Helper function to prompt for a comment index 
 +@return index of the comment selected by the user
 +*/
 +int prompt_for_comment_index()
 +{
 + cout << "​Comment index: ";
 + int index;
 + cin >> index;
 +
 + return index;
 +}
 +
 +/*
 +Prompt for and add a comment
 +@param comments list of comments to add to
 +*/
 +void add_comment(vector<​Comment>​ & comments)
 +{
 + cout << "​Comment:​ ";
 + cin.sync();​ // Might have a '​\n'​ char in the buffer from the menu option
 + string comment_body;​
 + getline(cin,​ comment_body);​
 +
 + cout << "Name: ";
 + string name;
 + getline(cin,​ name);
 +
 + Comment new_comment(comment_body,​ name);
 +
 + cout << "​Adding comment..."​ << endl;
 + cout << new_comment.to_string() << endl;
 +
 + comments.push_back(new_comment);​
 +}
 +
 +/*
 +Prompts for a comment index and for the new content of the comment
 +Updates the specified comment
 +@param comments list of comments that could be edited
 +*/
 +void edit_comment(vector<​Comment>​ & comments)
 +{
 + int comment_index = prompt_for_comment_index();​
 + cout << "New comment: ";
 + string new_comment;​
 + cin.sync();//​ Might have a '​\n'​ char in the buffer from the menu option
 + getline(cin,​ new_comment);​
 +
 + comments[comment_index].edit_comment(new_comment);​
 +}
 +
 +/*
 +Prompts for a comment index and increments the specified comments like count
 +@param comments list of comments that could be liked
 +*/
 +void like_comment(vector<​Comment>​ & comments)
 +{
 + int comment_index = prompt_for_comment_index();​
 + comments[comment_index].like_comment();​
 +}
 +
 +</​code>​
 +=== Comment.h ===
 +<code cpp>
 +#include <​string>​
 +
 +using namespace std;
 +
 +#ifndef _COMMENT_H
 +#define _COMMENT_H
 +
 +class Comment
 +{
 +public:
 + // Constructors
 + // Comment(); // default constructor
 +
 + Comment(string _comment_body,​ string _commenter_name);​
 +
 + // Members functions
 + // to_string
 + string to_string() const;
 +
 + // edit_comment
 + void edit_comment(string new_comment);​
 +
 + // like_comment
 + void like_comment();​
 +
 +private:
 + // Data Members
 + string comment_body;​
 + string commenter_name;​
 + int likes_count;​
 +};
 +
 +
 +#endif
 +</​code>​
 +=== Comment.cpp ===
 +<code cpp>
 +#include <​string>​
 +#include <​sstream>​
 +#include "​Comment.h"​
 +
 +using namespace std;
 +
 +Comment::​Comment(string _comment_body,​ string _commenter_name)
 +{
 + comment_body = _comment_body;​
 + commenter_name = _commenter_name;​
 + likes_count = 0;
 +}
 +
 +string Comment::​to_string() const
 +{
 + ostringstream strm;
 +
 + strm << "​\""​ << comment_body << "​\""​ << endl
 + << "From " << commenter_name << endl
 + << likes_count << " likes" << endl;
 +
 + return strm.str();
 +}
 +
 +void Comment::​edit_comment(string new_comment)
 +{
 + comment_body = new_comment;​
 +}
 +
 +void Comment::​like_comment()
 +{
 + likes_count++;​
 +}
 +</​code>​
cs-142/facespace.txt ยท Last modified: 2015/06/02 18:44 by cs142ta
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0