Skip to content

Commit

Permalink
Merge pull request #6 from CSCE315-Spring23/add-frequently-together
Browse files Browse the repository at this point in the history
Add frequently together
  • Loading branch information
fool1280 authored Mar 23, 2023
2 parents e342705 + 4957895 commit cf58378
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
53 changes: 53 additions & 0 deletions src/main/ManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ManagerController {
ObservableList<ObservableList<String>> excessReportData = FXCollections.observableArrayList();
ObservableList<ObservableList<String>> menuData = FXCollections.observableArrayList();
ObservableList<ObservableList<String>> salesReportData = FXCollections.observableArrayList();
ObservableList<ObservableList<String>> salesFrequentReportData = FXCollections.observableArrayList();
ObservableList<ObservableList<String>> xReportData = FXCollections.observableArrayList();
ObservableList<ObservableList<String>> zReportData = FXCollections.observableArrayList();

Expand All @@ -45,6 +46,8 @@ public class ManagerController {
@FXML
private TableView salesReportTableView;
@FXML
private TableView salesFrequentReportTableView;
@FXML
private TableView excessReportTableView;

@FXML
Expand Down Expand Up @@ -299,6 +302,56 @@ private void inventoryChangeMinAmt(ActionEvent event) {
}
}

/**
* Generates a sales report of what frequently bought together as pairs based on
* the selected start and end dates.
*
* If no dates are selected or the start date is after the end date, an error
* message is displayed.
*
* @param event the ActionEvent triggered by clicking the "Generate Frequent
* Sales Report" button
*/
@FXML
private void generateFrequentSalesReport(ActionEvent event) {
System.out.println("Manager has tried to generate a Sales Report");

salesFrequentReportTableView.getItems().clear();
salesFrequentReportTableView.getColumns().clear();
Alert a = new Alert(AlertType.NONE);

// Error handling
if (startDatePicker.getValue() == null || endDatePicker.getValue() == null) {
System.out.println("Error: No dates selected");
a.setAlertType(AlertType.ERROR);
a.setContentText("Error: No dates selected");
a.show();
return;
}
if (startDatePicker.getValue().isAfter(endDatePicker.getValue())) {
System.out.println("Start date is after end date");
a.setAlertType(AlertType.ERROR);
a.setContentText("Error: Start date is after end date");
a.show();
return;
}
if (startDatePicker.getValue().isEqual(endDatePicker.getValue())) {
System.out.println("Start date is equal to end date");
a.setAlertType(AlertType.ERROR);
a.setContentText("Error: Start date is equal to end date");
a.show();
return;
}
LocalDate startDate = startDatePicker.getValue();
LocalDate endDate = endDatePicker.getValue();
setTableResult(
db.generateFrequentSalesReport(
startDate.toString() + " 00:00:00",
endDate.toString() + " 23:59:59"),
salesFrequentReportData,
salesFrequentReportTableView);
}

/**
* Handles the action event for adding a new menu item to the menu table.
* Parses the input name, ID, class, and price from the corresponding fields,
Expand Down
25 changes: 25 additions & 0 deletions src/main/jdbcpostgreSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,31 @@ public ResultSet getSalesReport(Date startTime, Date endTime) {
return r;
}

/*
* Given a time window, display a list of pairs of menu
* items that sell together often, popular or not, sorted by most frequent.
*
* @param startTime The start time of the time window
*
* @param endTime The end time of the time window
*
* @return A ResultSet containing the sales report
*/
public ResultSet generateFrequentSalesReport(String startTime, String endTime) {
ResultSet r = null;
try {
Statement stmt = conn.createStatement();
String sqlStatement = "select t1.firstitem, t1.seconditem, t1.concat as menuitemstogether, COUNT(t1.concat) from (SELECT a.menuitemid as firstitem, b.menuitemid as seconditem, a.orderid, CONCAT(a.menuitemid, ' ', b.menuitemid) FROM orderlineitems a JOIN orderlineitems b ON a.orderid = b.orderid and a.menuitemid < b.menuitemid) t1 INNER JOIN (select orders.ordertime, orders.orderid from orders where orders.ordertime between '"
+ startTime + "' AND '" + endTime
+ "' group by orders.orderid) t2 ON t1.orderid = t2.orderid GROUP BY t1.firstitem, t1.seconditem, t1.concat ORDER BY COUNT(t1.concat) DESC;";
System.out.println(sqlStatement);
r = stmt.executeQuery(sqlStatement);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return r;
}

/**
* Retrieves a result set containing the X report for the most recent day.
*
Expand Down
32 changes: 25 additions & 7 deletions src/main/manager.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<DatePicker fx:id="endDatePicker" layoutX="291.0" layoutY="12.0" />
<Label layoutX="14.0" layoutY="16.0" text="Start:" />
<Label layoutX="254.0" layoutY="17.0" text="End:" />
<TableView fx:id="salesReportTableView" layoutX="14.0" layoutY="47.0" prefHeight="306.0" prefWidth="551.0">
<TableView fx:id="salesReportTableView" layoutX="14.0" layoutY="47.0" prefHeight="380.0" prefWidth="551.0">
<columns>
<TableColumn prefWidth="0" text="" />
</columns>
Expand All @@ -52,7 +52,7 @@
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView layoutX="14.0" layoutY="14.0" prefHeight="335.0" prefWidth="552.0" fx:id="xReportTableView">
<TableView layoutX="14.0" layoutY="14.0" prefHeight="400.0" prefWidth="552.0" fx:id="xReportTableView">
<columns>
<TableColumn prefWidth="0" text="" />
</columns>
Expand All @@ -65,20 +65,20 @@
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="zReportTableView" layoutX="14.0" layoutY="14.0" prefHeight="341.0" prefWidth="542.0">
<TableView fx:id="zReportTableView" layoutX="14.0" layoutY="14.0" prefHeight="400.0" prefWidth="552.0">
<columns>
<TableColumn prefWidth="0" text="" />
</columns>
</TableView>
<Button fx:id="zReportButton" layoutX="616.0" layoutY="41.0" mnemonicParsing="false" onAction="#generateZReport" text="Generate Z Report" />
<Button fx:id="zReportButton" layoutX="623.0" layoutY="43.0" mnemonicParsing="false" onAction="#generateZReport" text="Generate Z Report" />
</children></AnchorPane>
</content>
</Tab>
<Tab text="Excess Report">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="excessReportTableView" layoutX="14.0" layoutY="43.0" prefHeight="311.0" prefWidth="549.0">
<TableView fx:id="excessReportTableView" layoutX="14.0" layoutY="43.0" prefHeight="380.0" prefWidth="549.0">
<columns>
<TableColumn prefWidth="0" text="" />
</columns>
Expand All @@ -93,7 +93,7 @@
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="restockReportTableView" layoutX="14.0" layoutY="14.0" prefHeight="412.0" prefWidth="579.0">
<TableView fx:id="restockReportTableView" layoutX="14.0" layoutY="14.0" prefHeight="400.0" prefWidth="579.0">
<columns>
<TableColumn prefWidth="0" text="" />
</columns>
Expand All @@ -107,7 +107,7 @@
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="inventoryTableView" layoutX="14.0" layoutY="12.0" prefHeight="415.0" prefWidth="571.0">
<TableView fx:id="inventoryTableView" layoutX="14.0" layoutY="12.0" prefHeight="400.0" prefWidth="571.0">
<columns>
<TableColumn prefWidth="0" text="" />
</columns>
Expand All @@ -130,6 +130,24 @@
</AnchorPane>
</content>
</Tab>
<Tab text="Sales Frequently Together Report">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="157.0" prefWidth="441.0">
<children>
<DatePicker fx:id="startDatePicker" layoutX="50.0" layoutY="11.0" />
<DatePicker fx:id="endDatePicker" layoutX="291.0" layoutY="12.0" />
<Label layoutX="14.0" layoutY="16.0" text="Start:" />
<Label layoutX="254.0" layoutY="17.0" text="End:" />
<TableView fx:id="salesFrequentReportTableView" layoutX="14.0" layoutY="47.0" prefHeight="380.0" prefWidth="551.0">
<columns>
<TableColumn prefWidth="0" text="" />
</columns>
</TableView>
<Button layoutX="495.0" layoutY="12.0" mnemonicParsing="false" onAction="#generateFrequentSalesReport" text="Get Sales" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Menu">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
Expand Down

0 comments on commit cf58378

Please sign in to comment.