-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewsfeed code
42 lines (35 loc) · 1.51 KB
/
Newsfeed code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Newsfeed {
String[] trendingArticles;
int[] views;
double[] ratings;
public Newsfeed(String[] initialArticles, int[] initialViews, double[] initialRatings){
trendingArticles = initialArticles;
views = initialViews;
ratings = initialRatings;
}
public String getTopArticle(){
return trendingArticles[0];
}
public void viewArticle(int articleNumber){
views[articleNumber] = views[articleNumber] + 1;
System.out.println("The article '" + trendingArticles[articleNumber] + "' has now been viewed " + views[articleNumber] + " times!");
}
public void changeRating(int articleNumber, double newRating){
if (newRating > 5 || newRating < 0) {
System.out.println("The rating must be between 0 and 5 stars!");
} else {
ratings[articleNumber] = newRating;
System.out.println("The article '" + trendingArticles[articleNumber] + "' is now rated " + ratings[articleNumber] + " stars!");
}
}
public static void main(String[] args){
String[] robotArticles = {"Oil News", "Innovative Motors", "Humans: Exterminate Or Not?", "Organic Eye Implants", "Path Finding in an Unknown World"};
int[] robotViewers = {87, 32, 13, 11, 7};
double[] robotRatings = {2.5, 3.2, 5.0, 1.7, 4.3};
Newsfeed robotTimes = new Newsfeed(robotArticles, robotViewers, robotRatings);
robotTimes.viewArticle(2);
robotTimes.viewArticle(2);
System.out.println("The top article is " + robotTimes.getTopArticle());
robotTimes.changeRating(3, 5);
}
}