/* --- CSU213 Fall 2006 Lecture Notes --------- Copyright 2006 Viera K. Proulx Lecture 2: September 7, 2006 Part 1: Defining Unions of Data Traveling on the T in Boston ---------------------------- */ /* We wish to represent train stations for the subway and for the commuter lines. Each station has a name and the name of the line that serves it. A subway stations also has a price it costs to get on the train. For commuter trains the price depends also on the place of exit, and so we do not need that information. However, a station on the commuter line may be skipped by the express trains we need to know whether this is the case. Examples: Harvard station on the Red line costs \$1.25 to enter Kenmore station on the Green line costs \$1.25 to enter Riverside station on the Green line costs \$2.50 to enter Back Bay station on the Framingham line is an express stop West Newton stop on the Framingham line is not an express stop Wellesely Hills on the Worcester line is not an express stop So, the HtDP data definition would be: ;; IStation is one of ;; -- T Stop ;; -- Commuter Station ;; T Stop is (make-tstop String String Number) (define-struct tstop (name line price)) ;; Commuter Station is (make-commstation String String Boolean) (define-struct commstation (name line express)) (define harvard (make-tstop "Harvard" "red" 1.25)) (define kenmore (make-tstop "Kenmore" "green" 1.25)) (define riverside (make-tstop "Riverside" "green" 2.50)) (define backbay (make-commstation "Back Bay" "Framingham" true)) (define wnewton (make-commstation "West Newton" "Framingham" false)) (define wellhills (make-commstation "Wellesley Hills" "Worcester" false)) */ /* +----------+ | IStation | +----------+ +----------+ | / \ --- | ----------------------- | | +--------------+ +-----------------+ | TStop | | CommStation | +--------------+ +-----------------+ | String name | | String name | | String line | | String line | | double price | | boolean express | +--------------+ +-----------------+ */ // to represent a train station interface IStation { } // to represent a subway station class TStop implements IStation { String name; String line; double price; TStop(String name, String line, double price) { this.name = name; this.line = line; this.price = price; } } // to represent a stop on a commuter line class CommStation implements IStation { String name; String line; boolean express; CommStation(String name, String line, boolean express) { this.name = name; this.line = line; this.express = express; } } class ExamplesIStation{ ExamplesIStation() {} /* Harvard station on the Red line costs \$1.25 to enter Kenmore station on the Green line costs \$1.25 to enter Riverside station on the Green line costs \$2.50 to enter Back Bay station on the Framingham line is an express stop West Newton stop on the Framingham line is not an express stop Wellesely Hills on the Worcester line is not an express stop */ IStation harvard = new TStop("Harvard", "red", 1.25); IStation kenmore = new TStop("Kenmore", "green", 1.25); IStation riverside = new TStop("Riverside", "green", 2.50); IStation backbay = new CommStation("Back Bay", "Framingham", true); IStation wnewton = new CommStation("West Newton", "Framingham", false); IStation wellhills = new CommStation("Wellesley Hills", "Worcester", false); } /* We would also like to include bus lines -- including the express bus lines. Think of what is needed. */