SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package eksamen2017;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8.  
  9. public class Diner {
  10.     Collection <Table> tables = new ArrayList<>();
  11.     Collection <Seating> seatings = new ArrayList<>();
  12.    
  13.     public Diner () {
  14.        
  15.     }
  16.     public void addTables(Table...newTables) {
  17.         tables.addAll(Arrays.asList(newTables));
  18.     }
  19.     public void addSeating(Seating...newSeatings) {
  20.         seatings.addAll(Arrays.asList(newSeatings));
  21.     }
  22.     public void removeTables(Table...removedTables) {
  23.         tables.removeAll(Arrays.asList(removedTables));
  24.     }  
  25.    
  26.  
  27.  
  28.     /**
  29.     * Tells whether a Table is occupied.
  30.     * @param table the Table to check
  31.     * @return true if anyone is sitting at the provided Table
  32.     */
  33.     public boolean isOccupied(Table table) {
  34.            for (Seating seatings : seatings) {
  35.                if( seatings.getTable() == table) {
  36.                    return true;
  37.                }
  38.            }
  39.            return false;
  40.     }
  41.    
  42.     /**
  43.     * Computes the guest capacity,
  44.      * either the remaining (includeOccupied == false) or total (includeOccupied == true).
  45.     * @param includeOccupied controls whether to include tables that are occupied.
  46.     * @return the guest capacity
  47.     */
  48.     public int getCapacity(boolean includeOccupied) {
  49.         int capacity = 0;  
  50.         if (includeOccupied) {
  51.                for(Table tables : tables) {
  52.                    capacity += tables.getCapacity();
  53.                }
  54.                return capacity;
  55.            }
  56.         for (Table tables : tables) {
  57.             if(!(isOccupied(tables))) {
  58.                 capacity += tables.getCapacity();
  59.             }
  60.         }
  61.         return capacity;
  62.     }
  63.    
  64.     /**
  65.     * Adds a table to this Diner
  66.     * @param table
  67.     */
  68.     public void addTable(Table table) {
  69.            
  70.     }
  71.  
  72.     /**
  73.     * Removes a Table from this Diner.
  74.     * If the table is occupied an IllegalArgumentException exception should be thrown.
  75.     * @param table
  76.     * @throws IllegalArgumentException
  77.     */
  78.     public void removeTable(Table table) {
  79.            ...
  80.     }
  81.    
  82.     /**
  83.     * Merges two tables, i.e. replaces two tables with one table.
  84.     * lostCapacity is the difference between the old capacity and the new.
  85.     * This number is typically positive, since seats are lost when moving two tables
  86.      * close to each other.
  87.     * @param table1
  88.     * @param table2
  89.     * @param lostCapacity
  90.     * @throws IllegalArgumentException if any of the tables are occupied
  91.     */
  92.     public void mergeTables(Table table1, Table table2, int lostCapacity) {
  93.            if (isOccupied(table1) || isOccupied(table2)) {
  94.                throw new IllegalArgumentException("er opptatt");
  95.            }
  96.            int newCapacity = table1.getCapacity() + table2.getCapacity() - lostCapacity;          
  97.            removeTables(table1,table2);
  98.            addTables(new Table(newCapacity));          
  99.            
  100.            
  101.     }
  102.  
  103.     /**
  104.     * Splits a table into two, i.e. replaces one tables with two tables.
  105.     * The two capacities are the capacities of the two new tables.
  106.     * @param table
  107.     * @param capacity1
  108.     * @param capacity2
  109.     * @throws IllegalArgumentException if the table is occupied
  110.     */
  111.     public void splitTable(Table table, int capacity1, int capacity2) {
  112.          if (isOccupied(table)) {
  113.            throw new IllegalArgumentException("er opptatt");
  114.          }
  115.        
  116.          removeTables(table);
  117.          addTables(new Table(capacity1),new Table(capacity2));
  118.     }
  119.  
  120.     /**
  121.     * Tells whether a table has the provided capacity,
  122.     * i.e. if that number of new guests can be seated there.
  123.     * Note that a table cannot be shared among different groups.
  124.     * @param table
  125.     * @param capacity
  126.     * @return true of capacity number of guests can be seated here, false otherwise.
  127.     */
  128.     public boolean hasCapacity(Table table, int capacity) {
  129.            return table.getCapacity() >= capacity;
  130.     }
  131.  
  132.     /**
  133.     * Returns the tables that has the provided capacity.
  134.     * The tables should be sorted with the one with the least capacity (but enough) first.
  135.     * @param capacity
  136.     * @return the tables that has the provided capacity
  137.     */
  138.     public Collection<Table> findAvailableTables(int capacity) {
  139.         Collection <Table> avaiableTables = new ArrayList<>();  
  140.         for (Table tables : tables) {
  141.                if (hasCapacity(tables, capacity)) {
  142.                    avaiableTables.add(tables);
  143.                }
  144.            }
  145.         Collections.sort(avaiableTables,new SortByCapacity());
  146.         return avaiableTables;
  147.     }
  148.    
  149.     /**
  150.     * Finds a suitable, existing table for the provided group, and creates
  151.     * (but doesn't add) a corresponding Seating.
  152.     * The chosen table should be the one with the least capacity.
  153.     * @param group the group to be seated
  154.     * @return the newly created Seating
  155.     */
  156.     public Seating createSeating(Group group) {
  157.            ...
  158.     }
  159.    
  160.     /**
  161.     * Creates and adds a Seating for the provided group, using the createSeating method.
  162.     * @param group
  163.     * @return true if a Seating was created and added, false otherwise.
  164.     */
  165.     public boolean addSeating(Group group) {
  166.            ...
  167.     }
  168.    
  169.     /**
  170.     * Removes the seating for the provided table (number), if one exists
  171.     * @param tableNum the number of the table to be removed
  172.     */
  173.     public void removeSeating(int tableNum) {
  174.            ...
  175.     }
  176.  
  177.  
  178.  
  179.     @Override
  180.     public int compare(Table o1, Table o2) {
  181.         // TODO Auto-generated method stub
  182.         return 0;
  183.     }
  184. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top