Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 24 additions & 5 deletions src/main/java/edu/bristol/IMDBRating.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@

package edu.bristol;

import java.text.DecimalFormat;

public class IMDBRating
{
private float currentAverage = 0;
private int ratingCount = 0;

public float addNewRating(int newRating)
{
int previousTotal = (int) (currentAverage * ratingCount);
int newTotal = previousTotal + newRating;
ratingCount++;
currentAverage = newTotal / ratingCount;
return currentAverage;
if (isValidRating(newRating)) {
int previousTotal = (int)(currentAverage * ratingCount);
float newTotal = previousTotal + newRating;
ratingCount++;
currentAverage = toTwoDecimal(newTotal / ratingCount);
System.out.println("current average is " + currentAverage);
return currentAverage;
}
else
return Float.NaN;
}
public boolean isValidRating(int rateNumber)
{
if (rateNumber < 0 || rateNumber > 10) return false;
else return true;
}
public float toTwoDecimal(float originalNumber)
{
DecimalFormat df = new DecimalFormat("0.00");
float newNumber = Float.parseFloat(df.format(originalNumber));
return newNumber;
}
}
15 changes: 15 additions & 0 deletions src/test/java/edu/bristol/IMDBRatingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,20 @@ public void testAverageRating()

averageRating = rater.addNewRating(4);
assertTrue(averageRating == 3.0, "Adding 2nd rating: average should be 3.0");

averageRating = rater.addNewRating(6);
assertTrue(averageRating == 4.0, "Adding 3rd rating: average should be 4.0");

averageRating = rater.addNewRating(9);
assertTrue(averageRating == 5.25, "Adding 4th rating: average should be 5.25");

averageRating = rater.addNewRating(4);
assertTrue(averageRating == 5, "Adding 5th rating: average should be 5");

averageRating = rater.addNewRating(8);
assertTrue(averageRating == 5.5, "Adding 6th rating: average should be 5.5");

averageRating = rater.addNewRating(10);
assertTrue(averageRating == 6.14, "Adding 7th rating: average should be 6.14 ");
}
}
Binary file added target/classes/edu/bristol/IMDBRating.class
Binary file not shown.
Binary file not shown.