Unit 12 - Case Study part 3 Flashcards

1
Q
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

A possible solution is:
// variable of type RoomKind
RoomKind kind;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain a possible application of the Quantity pattern in the definition of the user-defined type Money

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give a Java definition of Money as described in Exercise 2. Note that java.util defines a class Currency.

A
public class Money {
     BigInteger amount;
     Currency currency;
     } 
We followed Fowler’s implementation using BigInteger (used for high precision calculations).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give a Java implementation of the operation multiply in type Money.

A

/** * 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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give the Java declaration of roomType in class Room, as a private instance variable.

A
The Java declaration for roomType in class Room would be (assuming we have defined RoomType): // association end with multiplicity 'one'
RoomType roomType;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Consider the association from Hotel to RoomType in Figure 3. What do you know about objects linked by the association Hotel to RoomType?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does Figure 3 tell us about the objects linked by the association ReserverPayer to Reservation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

(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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give a Java declaration to implement the association, shown in Figure 3, between Hotel and RoomType.

A
// in class Hotel
Set roomTypes;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give the Java declaration of an association class to implement the association HowMany between Reservation and RoomType, in Figure 3.

A

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; ...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Consider the qualified association between HotelChain and ReserverPayer in Figure 3.
What does this association tell us?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.
A
  • 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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

/**
* 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; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write the constructor for class Hotel.

A
/** * 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();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write the Java method for the following operation.

context Room::createGuest(name : Name, address : Address) : Guest

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write the Java method for the following operation.

context Room::createGuest(name : Name, address : Address) : Guest

A

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; }
17
Q

Checking availability.

How do you express this new navigability in Java?

A
The following instance variable declaration would need to be added to class Room. Set roomReservations; Because a Room needs to know about all Reservations, we have opted to use a Set rather than a Map. Had this been a qualified association whereby a specific reservation could be identified from its number, this would no longer be the case. The Room constructor would be modified as follows. 
/** * Creates a new instance of Room with specified room number and type 
** @param number the room number 
* @param roomType the room type
*/
public Room(int number, RoomType roomType) {
this.number = number;
this.roomType = roomType;
guests = new HashSet();
roomReservations = new HashSet();
}
18
Q

Write
on paper the Java code for setAllocatedRoom(Room) and allocatedTo(Reservation) defined as:

context Reservation::setAllocatedRoom(aRoom : Room) context Room::allocatedTo(aReservation : Reservation)

A

/** * Set the allocated room to this reservation
*
* @param room the room to allocate to this reservation
*/
public void setAllocatedRoom(Room room) {
allocatedRoom = room;
}
/**
* Allocates the room by adding a reservation
*
* @param reservation the reservation to allocate the room to
*/
public void allocatedTo(Reservation reservation) {
roomReservations.add(reservation);
}

19
Q

Write on paper the Java code for availableRooms as specified above. Use for guidance the code written for the operation available, noting the differences in returned type and how to construct the returned value.

A

/**
* Return a list of available rooms that fit the proposed reservation
* dates for the specified room type
*
* @param startDate proposed reservation start date
* @param endDate proposed reservation end date
* @param roomType the room type required for the reservation
*
* @return the set of rooms of the required type
* available between the specified dates
*/
public List availableRooms(Date startDate, Date endDate, RoomType roomType) {

// initialise an empty set of available rooms 
List allAvailableRooms = new ArrayList(); 
// get the collection of all rooms of hotel 
Collection allRooms = rooms.values(); 
//create an iterator to search through all rooms Iterator roomSearcher = allRooms.iterator(); 
// search all rooms while still rooms to search while (roomSearcher.hasNext()) { 
// set next room as current
Room currentRoom = roomSearcher.next();
// initially room is not known to be available
boolean availableRoom = false;
//only proceed if room is of correct type
if (currentRoom.isRoomType(roomType)) {
// get all the reservations recorded for this room Collection allRoomReservations = currentRoom.getRoomReservations(); 
// create iterator to search all reservations for a room Iterator reservationSearcher = allRoomReservations.iterator(); 
// presume available till proven otherwise availableRoom = checkAvailability(startDate, endDate, currentRoom); }
// if room is available if (availableRoom) { 
// add to set of available rooms allAvailableRooms.add(currentRoom); } } 
// return the set of available rooms 
return allAvailableRooms; 
}