Question: 5 2 0 Toll Bridge Write a program that calculates and returns the toll on the 5 2 0 bridge on weekends and holidays with

520 Toll Bridge
Write a program that calculates and returns the toll on the 520 bridge on weekends and holidays with a Good To Go! Pass. The program takes three values: hour, minute and am/pm and prints out the toll price. Start coding with TollBridge520.java provided. Keep input and output statements without change, add logic that calculated the toll based on the following table:
Weekends and Holidays**
Good To Go! Pass Toll
Midnight to 4:59 am
$1.25
5 am to 7:59 am
$1.40
8 am to 10:59 am
$2.05
11 am to 5:59 pm
$2.65
6 pm to 8:59 pm
$2.05
9 pm to 10:59 pm
$1.40
11 pm to 11:59 pm
$1.25
Sample run #1:
Hours (1-12)=>12
Minutes (0-59)=>15
am or pm (lower case only)=> am
Timestamp: 12:15 am
Charge: $1.25
Input validation
1) Hours value must be in range [1,12]. If the hours value is invalid, an error message containing word ERROR in all caps must appear and the program must quit right away. See sample program run below:
Sample run #2:
Hours (1-12)=>0
ERROR: Hours value is out of [1,12] range! Quitting...
2) Minutes value must be in range [0,59]. If the minutes value is invalid, an error message containing word ERROR in all caps must appear and the program must quit right away. See sample program run below:
Sample run #3:
Hours (1-12)=>1
Minutes (0-59)=>60
ERROR: Minutes value is out of [0,59] range! Quitting...
3)am or pm must be correctly spelled in lower case letters. If am/pm string is invalid, an error message containing word ERROR in all caps must appear and the program must quit right away. See sample program run below:
Sample run #4:
Hours (1-12)=>1
Minutes (0-59)=>55
am or pm (lower case only)=> Am
ERROR: Invalid string. Must be either "am" or "pm". Quitting...
The following chart may be helpful if you are more used to military time and not very confident when converting it into the standard one.
Test cases
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.regex.Pattern;
public class TestBridgeToll520{
public static boolean tests(PrintWriter outputStream)
{
int count =0;
int expectedCount =19;
outputStream.print("\r
----Tests for BridgeToll520--------------------------------------------------------\r
");
// Test 1//
outputStream.print("\r
____Test 01____________________________________________________________________________\r
");
String sep = System.lineSeparator();
String userInput = String.format("0%s1%sam", sep, sep);
String expectedOutput ="(?si).*?ERROR.*?";
outputStream.print("\r
Input:\r
");
outputStream.println(userInput);
outputStream.print("\r
Expected output must fit RegEx:\r
");
outputStream.println("\""+expectedOutput+"\"");
PrintStream standard = System.out;
ByteArrayInputStream bais = new ByteArrayInputStream(userInput.getBytes());
System.setIn(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
System.setOut(printStream);
BridgeToll520.main(null);
String actual = baos.toString();
printStream.close();
outputStream.print("\r
Actual Output:\r
");
outputStream.println("\""+actual+"\"");
outputStream.println();
if(Pattern.matches(expectedOutput, actual))
{
outputStream.printf("%-80s%-10s\r
", "BridgeToll520 TEST 01- invalid hours value", "PASSED");
count++;
}
else
{
outputStream.printf("%-80s%-10s\r
", "BridgeToll520 TEST 01- invalid hours value", "FAILED");
}
// Test 2//
outputStream.print("\r
____Test 02____________________________________________________________________________\r
");
userInput = String.format("13%s1%sam", sep, sep);
expectedOutput ="(?si).*?ERROR.*?";
outputStream.print("\r
Input:\r
");
outputStream.println(userInput);
outputStream.print("\r
Expected output must fit RegEx:\r
");
outputStream.println("\""+expectedOutput+"\"");
bais = new ByteArrayInputStream(userInput.getBytes());
System.setIn(bais);
baos = new ByteArrayOutputStream();
printStream = new PrintStream(baos);
System.setOut(printStream);
BridgeToll520.main(null);
actual = baos.toString();
printStream.close();
outputStream.print("\r
Actual Output:\r
");
outputStream.println("\""+actual+"\"");
outputStream.println();
if(Pattern.matches(expectedOutput, actual))
{
outputStream.printf("%-80s%-10s\r
", "BridgeToll520 TEST 02- invalid hours value", "PASSED");

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!