diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 9a536f5..e0991c0 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,6 +7,7 @@ + diff --git a/.idea/misc.xml b/.idea/misc.xml index d31b37a..732146c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,5 +8,5 @@ - + \ No newline at end of file diff --git a/src/main/java/edu/bristol/IMDBRating.java b/src/main/java/edu/bristol/IMDBRating.java index 6021d63..dee3e78 100644 --- a/src/main/java/edu/bristol/IMDBRating.java +++ b/src/main/java/edu/bristol/IMDBRating.java @@ -1,5 +1,8 @@ + package edu.bristol; +import java.text.DecimalFormat; + public class IMDBRating { private float currentAverage = 0; @@ -7,10 +10,26 @@ public class IMDBRating 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; } } diff --git a/src/test/java/edu/bristol/IMDBRatingTest.java b/src/test/java/edu/bristol/IMDBRatingTest.java index 76a9e9c..0b1f4f7 100644 --- a/src/test/java/edu/bristol/IMDBRatingTest.java +++ b/src/test/java/edu/bristol/IMDBRatingTest.java @@ -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 "); } } diff --git a/target/classes/edu/bristol/IMDBRating.class b/target/classes/edu/bristol/IMDBRating.class new file mode 100644 index 0000000..9270ad4 Binary files /dev/null and b/target/classes/edu/bristol/IMDBRating.class differ diff --git a/target/test-classes/edu/bristol/IMDBRatingTest.class b/target/test-classes/edu/bristol/IMDBRatingTest.class new file mode 100644 index 0000000..07643c4 Binary files /dev/null and b/target/test-classes/edu/bristol/IMDBRatingTest.class differ