Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleDel 5 - Diverse (10%)
Oppgave a)

Er CapacityListener et funksjonelt grensesnitt? Begrunn svaret!

Expand
titleLF

CapacityListener-grensesnitt er (teknisk sett) funksjonelt, siden det bare har én (abstrakt) metode (og kan derfor implementeres med lambda-syntaksen). Dette kreves i et svar som får poeng i det hele tatt. I tillegg bør andre argumenter (for at grensesnittet ikke er funksjonelt) trekkes inn, f.eks. at metoden typisk ikke er implementasjonens primære funksjon og at en ikke tenker på den som en matematisk funksjon som kun er avhengig av argumentene.

 

Oppgave b)

Skriv (om) en av isOccupied eller getCapacity i Diner slik at den bruker Stream-teknikken og Java 8 sin funksjonssyntaks (hvis du ikke har gjort det fra før, da!).

Expand
titleLF
Code Block
public boolean isOccupied(Table table) {
       return seatings.stream().anyMatch(s -> s.getTable() == table);
}
 
public int getCapacity(boolean includeOccupied) {
       Stream<Table> stream = tables.stream();
       if (! includeOccupied) {
              stream = stream.filter(t -> ! isOccupied(t));
       }
       return stream.mapToInt(Table::getCapacity).sum();
}
Oppgave c)

Forklar med tekst og/eller kode hvordan du vil teste isOccupied-metoden til Diner i en separat DinerTest-klasse, spesielt hvilke metoder i Diner du vil bruke. Angi om Diner evt. må endres for å være mer testbar.

Expand
titleLF
Man må rigge opp en Diner med minst to bord og én Seating, og så sjekke at isOccupied returnerer riktig verdi for hvert bord. En kan også fjerne en Seating og sjekke isOccupied etterpå. Opprigging er enklere og testen mer spisset om en får mulighet til å legge inn (og fjerne) Seating-objekter uten å måtte bruke addSeating(Group) (evt. removeSeating(tableNum), så derfor kan en legge til en pakke-privat addSeating(Seating)-metode (evt. removeSeating(Seating)) i Diner.
Expand
titleAppendix 1: Provided code (fragments)
Code Block
// part 1
 
/**
 * This class ...
 */
 A group (of people) dining together, and should be seated at the same table.
 * We currently only need to handle the size. 
 */
public class Group {
 
       /**
       * Initializes this Group with the provided guest count
       */
       public Group(int guestCount) {
              ...
       }
}
 
/**
 * A table with a certain maximum capacity.
 */
public class Table {
 
       /**
       * Initializes this Table with the provided capacity.
       * The table is also assigned a unique number.
       * @param capacity
       */
       public Table(int capacity) {
              ...
       }
 
       /**
       * @return the table number
       */
       public int getNum() {
              ...
       }
}
 
/**
 * Represents the fact that a Group is seated at and occupies a Table
 */
public class Seating {
 
       /**
       * Initializes this Seating ...
       */
       public Seating(...) {
              ...
       }
}

 
// part 2
 
/**
 * A place where groups of guests can buy a meal
 */
public class Diner {
 
       /**
       * Tells whether a Table is occupied.
       * @param table the Table to check
       * @return true if anyone is sitting at the provided Table
       */
       public boolean isOccupied(Table table) {
              ...
       }
      
       /**
       * Computes the guest capacity,
        * either the remaining (includeOccupied == false) or total (includeOccupied == true).
       * @param includeOccupied controls whether to include tables that are occupied.
       * @return the guest capacity
       */
       public int getCapacity(boolean includeOccupied) {
              ...
       }
      
       /**
       * Adds a table to this Diner
       * @param table
       */
       public void addTable(Table table) {
              ...
       }
 
       /**
       * Removes a Table from this Diner.
       * If the table is occupied an IllegalArgumentException exception should be thrown.
       * @param table
       * @throws IllegalArgumentException
       */
       public void removeTable(Table table) {
              ...
       }
      
       /**
       * Merges two tables, i.e. replaces two tables with one table.
       * lostCapacity is the difference between the old capacity and the new.
       * This number is typically positive, since seats are lost when moving two tables
        * close to each other.
       * @param table1
       * @param table2
       * @param lostCapacity
       * @throws IllegalArgumentException if any of the tables are occupied
       */
       public void mergeTables(Table table1, Table table2, int lostCapacity) {
              ...
       }
 
       /**
       * Splits a table into two, i.e. replaces one tables with two tables.
       * The two capacities are the capacities of the two new tables.
       * @param table
       * @param capacity1
       * @param capacity2
       * @throws IllegalArgumentException if the table is occupied
       */
       public void splitTable(Table table, int capacity1, int capacity2) {
              ...
       }
 
       /**
       * Tells whether a table has the provided capacity,
       * i.e. if that number of new guests can be seated there.
       * Note that a table cannot be shared among different groups.
       * @param table
       * @param capacity
       * @return true of capacity number of guests can be seated here, false otherwise.
       */
       public boolean hasCapacity(Table table, int capacity) {
              ...
       }
 
       /**
       * Returns the tables that has the provided capacity.
       * The tables should be sorted with the one with the least capacity (but enough) first.
       * @param capacity
       * @return the tables that has the provided capacity
       */
       public Collection<Table> findAvailableTables(int capacity) {
              ...
       }
      
       /**
       * Finds a suitable, existing table for the provided group, and creates
       * (but doesn't add) a corresponding Seating.
       * The chosen table should be the one with the least capacity.
       * @param group the group to be seated
       * @return the newly created Seating
       */
       public Seating createSeating(Group group) {
              ...
       }
      
       /**
       * Creates and adds a Seating for the provided group, using the createSeating method.
       * @param group
       * @return true if a Seating was created and added, false otherwise.
       */
       public boolean addSeating(Group group) {
              ...
       }
      
       /**
       * Removes the seating for the provided table (number), if one exists
       * @param tableNum the number of the table to be removed
       */
       public void removeSeating(int tableNum) {
              ...
       }
}
 

// part 3
 
public class SimpleTable ... Table {
 
       public SimpleTable(int capacity) {
              ...
       }
 
       ...
}
 
/**
 * A table that consists of two other tables.
 */
public class CompositeTable ... Table {
      
       public CompositeTable(Table table1, Table table2, int lostCapacity) {
              ...
       }
 
       ...
}
 
// part 4
 
/**
 * Interface for listening to changes in Diner capacity
 */
public interface CapacityListener {
       /**
       * Called to inform that a Diner has changed capacity
       * @param diner
       */
       public void capacityChanged(Diner diner);
}
 
/**
 * Handles guests arriving at and departing from a Diner.
 */
public class GuestManager ... {
 
       public GuestManager(Diner diner) {
              ...
       }
      
       /**
       * Handles arriving groups, by either seating them immediately
       * (if possible) or putting them in queue. Those enqueued will
       * be seated when the Diner's (change in) capacity allows.
       * @param group
       */
       public void groupArrived(Group group) {
              ...
       }
 
       /**
       * Handles departing groups, by removing their seating.
       * @param tableNum the table where the group was seated
       */
       public void groupDeparted(int tableNum) {
              ...
       }
 
       ...
}