Test-Driven Development Exercise
Task 1 Solution
So how did we TDD task 1? Click on the tabs below to find out...
Here is the first in a series of videos showing how we applied TDD to the problem at hand. The video is about 13 minutes long.
public class Account {
private final int numberOfLines;
private final double monthlyRate;
private final double additionalLineRate; public Account(int numberOfLines, double monthlyRate, double additionalLineRate) {
this.numberOfLines = numberOfLines;
this.monthlyRate = monthlyRate;
this.additionalLineRate = additionalLineRate;
} public double computeBill() {
return monthlyRate + (numberOfLines - 1) * additionalLineRate;
}
}
package com.wildwestwireless;import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;public class AccountTest {
private static final double GOLD_ADDITIONAL_LINE_RATE = 14.50;
private static final double GOLD_MONTHLY_RATE = 49.95;
private static final int ONE_LINE = 1;
private static final int TWO_LINES = 2;
private static final int THREE_LINES = 3;
private static final double SILVER_MONTHLY_RATE = 29.95;
private static final double SILVER_ADDITIONAL_LINE_RATE = 21.50;
private static final double PENNY_PRECISION = 0.01; @Test
public void goldAccounts() {
assertEquals(49.95, goldAccountWith(ONE_LINE).computeBill(), PENNY_PRECISION);
assertEquals(64.45, goldAccountWith(TWO_LINES).computeBill(), PENNY_PRECISION);
} @Test
public void silverAccounts() {
assertEquals(29.95, silverAccountWith(ONE_LINE).computeBill(), PENNY_PRECISION);
assertEquals(72.95, silverAccountWith(THREE_LINES).computeBill(), PENNY_PRECISION);
} private Account silverAccountWith(int numberOfLines) {
return new Account(numberOfLines, SILVER_MONTHLY_RATE, SILVER_ADDITIONAL_LINE_RATE);
} private Account goldAccountWith(int numberOfLines) {
return new Account(numberOfLines, GOLD_MONTHLY_RATE, GOLD_ADDITIONAL_LINE_RATE);
}
}
Click on the link below to download the source code for this task.
WildWestWireless code, end of Task 1Java Compatibility Note: These exercises are tested
with JDK 17, a long-term support version released in 2021.
While many exercises work with earlier Java versions, some
may require JDK 17 due to library dependencies. If you
encounter issues, please use JDK 17. Should problems persist,
contact us for support.
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |