IST311 – Homework 3 - Spring 2018

Write a C# app to manage customer's reviews (similar to Amazon.com recommendations attached to each product they sell).

A review consists of a score in the form of a number, a comment, and a date (see class diagram below). The number represents the number of 'stars' given to the product. The highest possible rating is five stars, and the lowest is one star. The comment is any text supplied by the user. The review date is provided by the system when the review object is created.

Your application must implement the view suggested by the figure below.

5 / → / Review
5 Excellent… / Review
5 Best ever / Review
Great!!!
4
3 / → / Review
3 Average q…
2
1 / → / Review
1 Horrible / Review
1 Don't waste…

On the left side, we have a generic list indexed by the number of stars. For instance, entry 5 holds all the five-stars reviews, and so on. On the left side, we have the actual reviewssupplied by the users. This structure must be implemented as follows

List<List<Review> starList = new List<List<Review>();

Observe that this definition tells us that each cell in starListis a list of reviews.

Your program must do the following.

  1. Define the Reviewclass as indicated in Figure 1.
  2. Define statListas indicated above.
  3. Initialize each cell of starListwith a new List<Review>that is ready to accept data.
  4. Implement a method to read data from the keyboard (you need to capture stars and comment). With this data, you create a new Reviewobject and place it into the corresponding list entry according to its star ranking. That is, a three stars review goes on the third cell with other three-star reviews. See Figure 2 below for a sample of an input sequence.
  5. Once the user has finished entering data, you print the contents of startListindicating the percentage of reviews in each category. See Figure 3 below.


Figure 1. Review class /
Figure 2. A sample of reviews including stars and comments

Figure 3. Printing contents of starList

Hints: The expression starList[5].ElementAt(2) allows you to access the third five-stars review (5 Great!!!). The statementstarList.Add(new List<Review>()) can be used to initialize each of the five cells of starList with an empty review list. The statementstarList[stars].Add(r) adds a new review r to the corresponding stars group (1..5).

SOLUTION

classProgram

{

privatestaticint n = 6; //stars (0 is not used!)

privatestaticdoubletotalReviews = 0;

staticvoid Main(string[] args)

{

//GOAL: This app implements a 'FIVE-Starts' rating system

List<List<Review> starList = new List<List<Review>();

for (inti = 0; i < n; i++)

{

//initialize each of the five cells of starList with an empty review list

starList.Add(new List<Review>());

}

GetDataPopulateRating(starList);

ShowList(starList);

Console.Read();

}//Main

privatestaticvoidGetDataPopulateRating(List<List<Review> starList)

{

while (true)

{

Console.WriteLine("Enter number of stars 1..5 [type 0 to exit]");

int stars = Convert.ToInt32(Console.ReadLine());

if (stars == 0) return;

Console.WriteLine("Enter comment [then ENTER]");

string comment = Console.ReadLine();

Review r = newReview(comment, stars);

starList[stars].Add(r);

totalReviews++;

}

}

privatestaticvoidShowList(List<List<Review> starList)

{

for (inti = n-1 ; i >0 ; i--)

{

Console.WriteLine("\n\nSTARS {0} \t Percentage: {1:N2}%",

i , 100 * starList[i].Count/totalReviews);

for (int j = 0; j < starList[i].Count; j++)

{

Console.WriteLine("\t\t {0} ", starList[i].ElementAt(j));

}

}

}

}