Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eksamen2017;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Comparator;
- public class Diner {
- Collection <Table> tables = new ArrayList<>();
- Collection <Seating> seatings = new ArrayList<>();
- public Diner () {
- }
- public void addTables(Table...newTables) {
- tables.addAll(Arrays.asList(newTables));
- }
- public void addSeating(Seating...newSeatings) {
- seatings.addAll(Arrays.asList(newSeatings));
- }
- public void removeTables(Table...removedTables) {
- tables.removeAll(Arrays.asList(removedTables));
- }
- /**
- * 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) {
- for (Seating seatings : seatings) {
- if( seatings.getTable() == table) {
- return true;
- }
- }
- return false;
- }
- /**
- * 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) {
- int capacity = 0;
- if (includeOccupied) {
- for(Table tables : tables) {
- capacity += tables.getCapacity();
- }
- return capacity;
- }
- for (Table tables : tables) {
- if(!(isOccupied(tables))) {
- capacity += tables.getCapacity();
- }
- }
- return capacity;
- }
- /**
- * 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) {
- if (isOccupied(table1) || isOccupied(table2)) {
- throw new IllegalArgumentException("er opptatt");
- }
- int newCapacity = table1.getCapacity() + table2.getCapacity() - lostCapacity;
- removeTables(table1,table2);
- addTables(new Table(newCapacity));
- }
- /**
- * 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) {
- if (isOccupied(table)) {
- throw new IllegalArgumentException("er opptatt");
- }
- removeTables(table);
- addTables(new Table(capacity1),new Table(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) {
- return table.getCapacity() >= 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) {
- Collection <Table> avaiableTables = new ArrayList<>();
- for (Table tables : tables) {
- if (hasCapacity(tables, capacity)) {
- avaiableTables.add(tables);
- }
- }
- Collections.sort(avaiableTables,new SortByCapacity());
- return avaiableTables;
- }
- /**
- * 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) {
- ...
- }
- @Override
- public int compare(Table o1, Table o2) {
- // TODO Auto-generated method stub
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement