]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java
added some not really helpful class descriptions + removed types that got duplicated...
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / pizzeria / gui / tablemodels / TablesOverviewModel.java
1 package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import at.ac.tuwien.sbc.valesriegler.common.TableModel;
7 import at.ac.tuwien.sbc.valesriegler.types.Table;
8
9 public class TablesOverviewModel extends TableModel<Table> {
10         private static final String TABLE_ID = "ID";
11         private static final String STATUS = "Is free";
12         private static final String GROUP_ID = "Group ID";
13         private static final String[] COLUMNS = new String[] { TABLE_ID, STATUS, GROUP_ID };
14         private int idCounter = 1;
15
16         @Override
17         protected String[] getColumns() {
18                 return COLUMNS;
19         }
20
21         @Override
22         public Object getValueAt(int rowIndex, int columnIndex) {
23                 Table table = items.get(rowIndex);
24                 String wantedColumn = COLUMNS[columnIndex];
25                 switch (wantedColumn) {
26                 case TABLE_ID:
27                         return table.getId();
28                 case STATUS:
29                         return table.isFree();
30                 case GROUP_ID:
31                         return table.getGroupId();
32                 default:
33                         throw new RuntimeException(UNHANDLEDCOLUMN);
34                 }
35         }
36
37         public int getNumberOfFreeTables() {
38                 return getIdsOfFreeTables().size();
39         }
40
41         public List<Integer> getIdsOfFreeTables() {
42                 List<Integer> ids = new ArrayList<>();
43
44                 synchronized (items) {
45                         for (Table table : items) {
46                                 if (table.isFree()) {
47                                         ids.add(table.getId());
48                                 }
49                         }
50                 }
51
52                 return ids;
53         }
54
55         public List<Table> createFreeTables(int numberOfTables) {
56                 synchronized (items) {
57                         List<Table> tables = new ArrayList<Table>();
58                         for (int i = 0; i < numberOfTables; i++) {
59                                 Table table = createFreeTable();
60                                 tables.add(table);
61                         }
62                         addItems(tables);
63
64                         return tables;
65                 }
66         }
67
68         private Table createFreeTable() {
69                 return new Table();
70         }
71
72 }