Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

MassPikeTolls BlueJ Project

Click here to download a BlueJ project for MassPikeTolls.


MassPikeTolls Source Code

The Java source code for MassPikeTolls is below. Click on a file name to download it.


MassPikeTolls.java

/*
 * Example MassPikeTolls: program to determine whether a vehicle will
 * pay a toll on a given segment of the ticket-controlled portion of 
 * the Massachusetts Turnpike.  Before the "Big Dig", there were tolls
 * for all vehicles along the entire road, but western Mass. was exempted
 * from car tolls.  The ticket-controlled portion extends from Exit 1 in 
 * Stockbridge to Exit 15 at I-95 outside of Boston.  Trucks always pay
 * tolls, but cars pay tolls only on the part between exits 6 and 15.
 * Given an entry interchange, and exit interchange, and a vehicle type
 * (motorcycle, car, or truck), determine whether there will be a toll
 * on the entire distance travelled, part of the distance travelled, or
 * if there will be no toll.
 * 
 * Note: we pretend interchange 10A for MA 146 does not exist, so we can
 * use int values to hold interchange numbers.
 *
 * Note 2: we also ignore the fact that the Mass Pike reinstated car
 * tolls on the western portion in 2013.  Oh well.
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 * Updated for Fall 2013
 *
 * $Id: MassPikeTolls.java 2228 2013-10-24 05:42:15Z terescoj $
 */

import javax.swing.JOptionPane;

public class MassPikeTolls {

    public static void main(String[] args) {

        // we start out by gathering the information we need as inputs
        String input;

        // get the entry interchange
        input = JOptionPane.showInputDialog("At what interchange did you enter the Mass Pike (1-15)?");
        int entryInterchange = Integer.parseInt(input);

        // make sure it's valid
        if (entryInterchange < 1 || entryInterchange > 15) {
            JOptionPane.showMessageDialog(null, "Entry interchange must be in the range 1-15!", 
                "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        // get the exit interchange
        input = JOptionPane.showInputDialog("At what interchange did you exit the Mass Pike (1-15)?");
        int exitInterchange = Integer.parseInt(input);

        // make sure this one is valid as well
        if (exitInterchange < 1 || exitInterchange > 15) {
            JOptionPane.showMessageDialog(null, "Exit interchange must be in the range 1-15!", 
                "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        // one more check on the input: no one should be allowed to enter
        // and exit at the same interchange
        if (entryInterchange == exitInterchange) {
            JOptionPane.showMessageDialog(null, "Entry interchange and exit interchange must be different\n(No U Turns!)", 
                "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
        
        input = JOptionPane.showInputDialog("Were you in a motorcycle, a car, or a truck?");
        boolean isTruck = false;
        // now make sure we got a valid response and set a boolean variable to true
        // only if it was a truck.
        if (input.equals("truck")) {
            isTruck = true;
        }
        else if (input.equals("car")) {
            isTruck = false;
        }
        else if (input.equals("motorcycle")) {
            isTruck = false;
        }
        else {
            JOptionPane.showMessageDialog(null, "Invalid vehicle type!", 
                "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
        
        // now we report on whether there is a toll based on this input
        if (isTruck || ((entryInterchange >= 6) && (exitInterchange >= 6))) {
            JOptionPane.showMessageDialog(null, "You paid a toll on the entire trip...");
        }
        else if ((entryInterchange <= 6) && (exitInterchange <= 6)) {
            JOptionPane.showMessageDialog(null, "No tolls for you in western Mass!");
        }
        else {
            JOptionPane.showMessageDialog(null, "You paid a toll, but only for travels east of Exit 6");
        }
    }
}