-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGHRating.java
46 lines (39 loc) · 1.17 KB
/
GHRating.java
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
43
44
45
46
/*
* Module for ratings
* For Globe Hopper Tour Videos
* takes input rating from user
* validates between 0 stars and 5 stars
* computes average and necessary information
*/
public class GHRating {
// private variables for rating
private int count;
private double AVG;
// constructor for specific video
public GHRating() {
this.count = 0;
this.AVG = 0;
}
// central function for validating rating
public boolean addRating(int newRating) {
// if rating is less than 0 stars or greater than 5 stars
if (newRating < 0 || newRating > 5) {
System.out.println("Rating must be between 0 and 5 stars\nCannot be " + newRating); // error message
return false; // rating addition fails
}
// if first entry of video
if (count == 0) {
this.count++;
this.AVG = newRating;
} else {
// additional count made
this.count++;
// new average computed
this.AVG = (AVG * (count - 1) + newRating) / (double)count;
}
// success message
// displays new results
System.out.println("Rating for this Tour is: " + this.AVG + "\n Last rating given is: " + newRating + "\n");
return true; // success
}
}