-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInventoryTablePanel.java
125 lines (103 loc) · 4.27 KB
/
InventoryTablePanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class InventoryTablePanel extends JPanel {
private JComboBox<String> timeWindowBox;
private JComboBox<String> ingredientTypeBox;
private JTable usageTable;
private DefaultTableModel tableModel;
private DBConnection connect;
/**
* Constructor for the inventory data panel
* @author Timur Takhtarov
* @param connection the connection API
* @param ingredients the list of ingredients to populate the dropdown
*/
public InventoryTablePanel(DBConnection connect, ArrayList<String> ingredients) {
this.connect = connect;
// Set layout
setLayout(new BorderLayout());
String[] timeWindows = {"Last Week", "Last Month", "Last Year"};
timeWindowBox = new JComboBox<>(timeWindows);
ingredientTypeBox = new JComboBox<>(ingredients.toArray(new String[0]));
JPanel selectionPanel = new JPanel();
selectionPanel.setLayout(new FlowLayout());
selectionPanel.add(new JLabel("Select Time Window: "));
selectionPanel.add(timeWindowBox);
selectionPanel.add(new JLabel("Select Ingredient Type: "));
selectionPanel.add(ingredientTypeBox);
// Table for displaying usage
String[] columnNames = {"Day", "Amount Used"};
tableModel = new DefaultTableModel(columnNames, 0);
usageTable = new JTable(tableModel);
JScrollPane tableScrollPane = new JScrollPane(usageTable);
// Add components to the panel
add(selectionPanel, BorderLayout.NORTH);
add(tableScrollPane, BorderLayout.CENTER);
timeWindowBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateTable();
}
});
ingredientTypeBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateTable();
}
});
// Initial table population
updateTable();
}
private void updateTable() {
// Clear existing rows
tableModel.setRowCount(0);
// Get the selected time window and ingredient type
String selectedTimeWindow = (String) timeWindowBox.getSelectedItem();
String selectedIngredientType = (String) ingredientTypeBox.getSelectedItem();
// Calculate the start and end dates based on the selected time window
Date[] dateRange = getTimeRange(selectedTimeWindow);
Date startDate = dateRange[0];
Date endDate = dateRange[1];
// Array to hold the data populated by the DBConnection method
ArrayList<HashMap<String, Object>> usageData = new ArrayList<>();
connect.getIngredientInTimeframe(startDate, endDate, selectedIngredientType, usageData);
// Populate the table with the fetched data
if (usageData != null) {
for (HashMap<String, Object> dailyUsage : usageData) {
String day = (dailyUsage.get("date").toString());
Integer amount = (Integer) dailyUsage.get("amount");
tableModel.addRow(new Object[]{day, amount});
}
}
}
private Date[] getTimeRange(String timeWindow) {
Calendar calendar = Calendar.getInstance();
Date endDate = calendar.getTime(); // End date is today
Date startDate = null;
switch (timeWindow) {
case "Last Week":
calendar.add(Calendar.WEEK_OF_YEAR, -1);
startDate = calendar.getTime();
break;
case "Last Month":
calendar.add(Calendar.MONTH, -1);
startDate = calendar.getTime();
break;
case "Last Year":
calendar.add(Calendar.YEAR, -1);
startDate = calendar.getTime();
break;
default:
startDate = endDate; // Just in case something goes wrong
}
return new Date[]{startDate, endDate};
}
}