Unit 12 - Case Study part 3 Flashcards
RoomKind could be declared as a new enumeration type in Java as follows: // user-defined enumeration type enum RoomKind { SINGLE, DOUBLE, TWIN, FAMILYOF3, FAMILYOF4, FAMILYOF5 } This new type can then be used to declare variables, just like any other type, by using an ordinary Java variable declaration.
Declare a public instance variable of type RoomKind in Java.
A possible solution is:
// variable of type RoomKind
RoomKind kind;
Explain a possible application of the Quantity pattern in the definition of the user-defined type Money
Money can be seen as a quantity with a value (the amount) and a unit (the currency). Hence, Money can be represented as the class shown in Figure 4, where we have assumed that both attributes are private, with amount of type Integer, and currency of type Currency (an enumeration of, say, £, $ and E).
Give a Java definition of Money as described in Exercise 2. Note that java.util defines a class Currency.
public class Money { BigInteger amount; Currency currency; } We followed Fowler’s implementation using BigInteger (used for high precision calculations).
Give a Java implementation of the operation multiply in type Money.
/** * Returns a Money object whose currency is the same as
* this object’s currency and whose amount is the
* product of amount and this object’s amount.
*/
public Money multiply(BigInteger amount) {
return new Money(this.amount.multiply(amount), this.currency);
}
Give the Java declaration of roomType in class Room, as a private instance variable.
The Java declaration for roomType in class Room would be (assuming we have defined RoomType): // association end with multiplicity 'one' RoomType roomType;
Consider the association from Hotel to RoomType in Figure 3. What do you know about objects linked by the association Hotel to RoomType?
The association from Hotel to RoomType expresses the fact that each Hotel object has to be able to hold the object identities of many RoomType objects. Other associations with multiplicity ‘many’ have been qualified, to indicate that for each value of a qualifier attribute there is at most one object at the other end; however, they are still associations between one object at one end and many at the other end.
What does Figure 3 tell us about the objects linked by the association ReserverPayer to Reservation?
The association from ReserverPayer to Reservation expresses the fact that each ReserverPayer object has to be able to hold the object identities of many Reservation objects, but for each ReserverPayer object and a reservation number there is at most one Reservation object.
Teacher and Student have a many to many relationship.
(a) What does it say about object references?
(b) How would you implement it in Java? (
c) Draw an object diagram to illustrate your implementation.
(a) Each Teacher object must hold the references to their Student objects; and each Student object must hold the references to their Teacher objects.
(b) We can declare a variable teachers in Student and a variable students in Teacher; both variables are declared as generic collections of interface type Set and can be assigned, respectively, HashSet to teacher and HashSet to student, as follows:
// in class Teacher
Set students = new HashSet();
// in class Student
Set teachers = new HashSet();
(c) An example of the implementation in part (b) is shown in Figure 10 below.
Give a Java declaration to implement the association, shown in Figure 3, between Hotel and RoomType.
// in class Hotel Set roomTypes;
Give the Java declaration of an association class to implement the association HowMany between Reservation and RoomType, in Figure 3.
We renamed the identifier attribute number of Reservation as reservationNumber to avoid conflict with number of HowMany.
public class HowMany { int number; int reservationNumber; RoomKind kind; ...
Consider the qualified association between HotelChain and ReserverPayer in Figure 3.
What does this association tell us?
The association expresses the fact that ReserverPayer objects are identified in the system through their id (assigned to them the first time they register and used to represent their value identity. The HotelChain object is linked to all ReserverPayer objects via this association, and will perform a mapping between the value and object identities of ReserverPayer objects in order to be able to reference them.
Give Java declarations to implement the following qualified associations:
- between HotelChain and Hotel;
- between Hotel and Reservation;
- between Hotel and Room;
- between ReserverPayer and Reservation.
- Map hotels; declared within the HotelChain class;
- Map reservations; declared within the Hotel class;
- Map rooms; declared within the Hotel class;
- Map myReservations; declared within the ReserverPayer class;
Write the constructor for class Reservation. Note: we use the abstract class Calendar defined in java.util; it converts between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR etc. A Date object represents a specific instant in time with millisecond precision. getInstance (TimeZone zone) and getTime() are methods of class Calendar that create a calendar using the specified time zone, and get the calendar’s current time, respectively.
/**
* Creates a new instance of Reservation with attributes initialised
*
* @param startDate the start Date
* @param endDate the end Date
* @param roomType the RoomType
* @param reserverPayer the ReserverPayer
*/
public Reservation(Date startDate, Date endDate, RoomType roomType, ReserverPayer reserverPayer) {
// increment class variable numberOfReservations
// and use it to initialise number numberOfReservations++;
number = numberOfReservations;
// initialise other variables
this.startDate = startDate;
this.endDate = endDate;
this.roomType = roomType;
// set reservation date to current time and date reservationDate = Calendar.getInstance(TimeZone.getDefault()).getTime();
// set the reserverPayer
this.reserverPayer = reserverPayer; }
Write the constructor for class Hotel.
/** * Creates a new instance of Hotel * * @param name the name of the new hotel */ public Hotel(String name) { this.name = name; reservations = new HashMap(); rooms = new HashMap(); roomTypes = new HashSet(); }
Write the Java method for the following operation.
context Room::createGuest(name : Name, address : Address) : Guest
In class Room:
/**
* Create a guest and add this to the room
*
* @param guestName the name of the guest
* @param guestAddress the address of the guest
*
* @return the new Guest object
*/
public Guest createGuest(String guestName, String []guestAddress) {
Guest guest;
// invoke constructor, create link and return the new instance guest = new Guest(guestName, guestAddress); this.guests.add(guest);
return guest;
}