/* --- CSU213 Spring 2005 Lecture Notes --------- Copyright 2005 Viera K. Proulx Lecture 3: On Your Own */ /* Goals: - formalize the design recipe for classes - practice the design recipe on moderately complex problem DESIGN RECIPE FOR CLASSES 1. Analyze the problem, identify the available information, make examples of the information, identify the data to represent the information 2. Write the data definition in the form of class diagrams. If the information consists of several parts, define a class that has one field for each part. If one of the fields represents itself a more complex information, think of using another class to represent that information - 'containment'. If the information represents 'one of' pieces of data, think of union - an abstract class for the overall information and subclasses for each alternative. Use the containment arrow and the union arrow as needed. 3. Write the Java code to define the classes, including the constructors. 4. Translate the examples from part 1 into definitions of instances of these classes. Make sure you have at least one example per class definition. Note, that we cannot construct instances of abstract classes. Practice Example: Consider the information a travel agent may need to make reservations for your trip over the Spring Break. You may need a flight, a hotel room, and a car rental. Explore what information may be required, so that the agent can suggest an itinerary. Make examples of possible travel scenaria. Scenario 1: Tom wants to travel from Boston Logan airport to Denver, rent a car there, and go to ski in Aspen, leaving Boston on February 28th, returning on March 6th. He plans to go alone. Scenarion 2: Aya wants to travel with a friend from Boston Logan airport to Miami, rent a car there, and stay in a hotel leaving on February 26th and returning on March 4th. Information needed: Flight(s) - there and back Hotel Car Flight: airports - from and to, date Hotel: dates for check-in and check-out, location Car: dates when picked up and returned, which airport Date: year, month, date Traveler: name - at least Trip: Traveler, Flight out, Flight back, Hotel, Car */ /* +-----------------+ | Trip | +-----------------+ | Person traveler | | Flight out | | Flight back | | Hotel hotel | | CarRental car | +-----------------+ */ // to represent a trip class Trip { Person traveler; Flight out; Flight back; Hotel hotel; CarRental car; Trip(Person traveler, Flight out, Flight back, Hotel hotel, CarRental car) { this.traveler = traveler; this.out = out; this.back = back; this.hotel = hotel; this.car = car; } } /* +-------------+ | Person | +-------------+ | String name | | int age | +-------------+ */ // to represent a person class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } } /* +--------------------+ | Flight | +--------------------+ | Date day | | String depart | | String destination | +--------------------+ */ // to represent a flight plan class Flight { Date day; String depart; String destination; Flight(Date day, String depart, String destination) { this.day = day; this.depart = depart; this.destination = destination; } } /* +---------------+ | Hotel | +---------------+ | Date checkin | | Date checkout | | String city | | int guests | +---------------+ */ // to represent a hotel reservation class Hotel { Date checkin; Date checkout; String city; int guests; Hotel(Date checkin, Date checkout, String city, int guests) { this.checkin = checkin; this.checkout = checkout; this.city = city; this.guests = guests; } } /* +----------------+ | CarRental | +----------------+ | Date pickup | | Date dropoff | | String airport | +----------------+ */ // to represent a car rental class CarRental { Date pickup; Date dropoff; String airport; CarRental(Date pickup, Date dropoff, String airport) { this.pickup = pickup; this.dropoff = dropoff; this.airport = airport; } } /* +-----------+ | Date | +-----------+ | int year | | int month | | int day | +-----------+ */ // to represent a date class Date { int year; int month; int day; Date(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } } /* Composite class diagram: +-----------------+ | Trip | +-----------------+ | Person traveler |-------------------------+ | Flight out |-----------+ | | Flight back |--------+ | v | Hotel hotel |-----+ | | +-------------+ | CarRental car |--+ | | | | Person | +-----------------+ | | | | +-------------+ | | | | | String name | | | | | | int age | +----------- + | | | +-------------+ | | | | | | | +----------------------+ | | +----------------------+ | | +-------+ | | | | | | v v v v +----------------+ +---------------+ +--------------------+ | CarRental | | Hotel | | Flight | +----------------+ +---------------+ +--------------------+ | Date pickup |---+ | Date checkin |---+ | Date day |--+ | Date dropoff |-+ | | Date checkout |-+ | | String depart | | | String airport | | | | String city | | | | String destination | | +----------------+ | | | int guests | | | +--------------------+ | | | +---------------+ | | | | +---+ +--------------+ | | +---+ | | +--------------+ | | | | | +---------------------------------------+ | | | | | v v v v v +-----------+ | Date | +-----------+ | int year | | int month | | int day | +-----------+ */ class Examples{ Examples() {} Date feb26 = new Date(2005, 2, 26); Date feb28 = new Date(2005, 2, 28); Date mar04 = new Date(2005, 3, 4); Date mar06 = new Date(2005, 3, 6); CarRental carForTom = new CarRental(this.feb28, this.mar06, "Denver"); CarRental carForAya = new CarRental(this.feb26, this.mar04, "Miami"); Hotel hotelForTom = new Hotel(this.feb28, this.mar06, "Aspen", 1); Hotel hotelForAya = new Hotel(this.feb26, this.mar04, "Miami", 2); Flight outForTom = new Flight(this.feb28, "Logan", "Stapelton"); Flight backForTom = new Flight(this.mar06, "Stapelton", "Logan"); Flight outForAya = new Flight(this.feb26, "Logan", "Miami"); Flight backForAya = new Flight(this.mar04, "Miami", "Logan"); Person tom = new Person("Tom", 19); Person aya = new Person("Aya", 18); Trip tripForTom = new Trip(this.tom, this.outForTom, this.backForTom, this.hotelForTom, this.carForTom); Trip tripForAya = new Trip(this.aya, this.outForAya, this.backForAya, this.hotelForAya, this.carForAya); } /* Think of variations for these definitions: -- make hotel, flight, and car a 'reservation item' all having the traveler information. -- make the trip specify the dates, to avoid repeating the same information three times. Discuss the benefits and drawbacks of these options. */