Class DB
java.lang.Object
team75.model.DB
Database access layer for the restaurant POS system.
Provides CRUD operations for menu items, inventory, employees, and orders.
- Author:
- Raghu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents an employee (cashier or manager).static classRepresents an employee clock-in/clock-out record.static classPer-employee sales performance data.static classSales data for a single hour.static classOne ingredient entry in a menu item's recipe, mapping an inventory ingredient to the quantity consumed per order of that menu item.static classRepresents an ingredient in inventory.static classRepresents a menu item (drink, food, or add-on).static classRepresents a completed order from order history.static classRepresents one line-item when a cashier submits a new order.static classAggregated sales count for a single menu item.static classAggregated ingredient usage for a time window.static classSummary of sales data including hourly breakdown. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddEmployee(int employeeId, String name, String access, int age, String phone, String password) Adds a new employee to the database.static voidaddInventoryItem(int ingredientId, String name, double quantity, String units) Adds a new inventory item.static voidaddMenuIngredientMapping(int itemId, int ingredientId, double quantity) Adds a single ingredient mapping to a menu item's recipe.static intaddMenuItem(String name, String category, BigDecimal price, String description) Adds a new menu item with an auto-generated ID.static intaddMenuItemWithIngredients(String name, String category, BigDecimal price, String description, List<DB.IngredientRecipe> recipe) Adds a new seasonal (or regular) menu item together with its full ingredient recipe in a single transaction.static voidclockIn(int employeeId) Clocks an employee in, setting timein to now and clearing timeout.static voidclockOut(int employeeId) Clocks an employee out by setting timeout to now.static Connectionconnect()Opens a new connection to the PostgreSQL database.static intdeleteEmployee(int employeeId) Deletes an employee and their check-in records in a transaction.static intdeleteInventoryItem(int ingredientId) Deletes an inventory item and its menu-ingredient mappings in a transaction.static intdeleteMenuItem(int itemId) Deletes a menu item and its ingredient mappings in a transaction.static List<DB.EmployeeCheckin> Retrieves all employee check-in/check-out records.static List<DB.Employee> Retrieves all employees ordered by ID.static List<DB.EmployeeSalesEntry> getEmployeeSalesReport(Timestamp start, Timestamp end) Per-employee sales performance in a time window: order count and total revenue for each employee who took orders.static List<DB.InventoryItem> Retrieves all inventory items ordered by ID.static List<DB.MenuItem> getMenu()Retrieves all menu items ordered by ID.static List<DB.MenuItem> getMenuByCategory(String category) Retrieves menu items filtered by category.Retrieves orders placed within a time window.static BigDecimalLooks up the price of a menu item by name (case-insensitive).static List<DB.ProductSalesEntry> getProductSalesReport(Timestamp start, Timestamp end) Counts how many times each menu item was sold in a time window by parsing the JSONB orderdetails column.static List<DB.ProductUsageEntry> getProductUsageChart(Timestamp start, Timestamp end) Calculates total ingredient consumption in a time window by joining items sold (from JSONB) with their recipes in menuingredientsmap.static DB.SalesSummarygetSalesReport(Timestamp start, Timestamp end) Overall sales summary for a time window: total orders, total revenue, and an hour-by-hour breakdown.static DB.SalesSummaryX-Report: hourly sales breakdown for the current day of operation.static DB.SalesSummaryZ-Report: end-of-day report that totals all sales and resets counters for the next business day.static booleanChecks whether the Z-Report has already been generated for today.static voidConnects to the DB and prints menu and employees.static UUIDsubmitOrder(int employeeId, List<DB.OrderItem> items, BigDecimal total) Inserts a new order into orderhistory and decrements inventory for every ingredient used by the ordered items and their add-ons.static intupdateEmployee(int employeeId, String name, String access, int age, String phone) Updates an employee's name, access level, age, and phone.static intupdateInventoryItem(int ingredientId, String name, double quantity, String units) Updates an inventory item's name, quantity, and units.static intupdateInventoryQuantity(int ingredientId, double quantity) Updates only the quantity of an inventory item.static intupdateMenuItem(int itemId, String name, String category, BigDecimal price, String description) Updates an existing menu item's fields.static intupdateMenuPrice(int itemId, BigDecimal newPrice) Updates only the price of a menu item.
-
Constructor Details
-
DB
public DB()
-
-
Method Details
-
connect
Opens a new connection to the PostgreSQL database.- Returns:
- a live JDBC connection
- Throws:
SQLException- if the connection cannot be established
-
getMenu
Retrieves all menu items ordered by ID.- Returns:
- list of all menu items
- Throws:
SQLException- if the query fails
-
getMenuByCategory
Retrieves menu items filtered by category.- Parameters:
category- the category to filter by (e.g. "Classic Drink", "Food", "Add-on")- Returns:
- list of matching menu items
- Throws:
SQLException- if the query fails
-
getPrice
Looks up the price of a menu item by name (case-insensitive).- Parameters:
itemName- the item name to look up- Returns:
- the item's price, or null if not found
- Throws:
SQLException- if the query fails
-
addMenuItem
public static int addMenuItem(String name, String category, BigDecimal price, String description) throws SQLException Adds a new menu item with an auto-generated ID.- Parameters:
name- item display namecategory- item categoryprice- item pricedescription- item description- Returns:
- the generated item ID
- Throws:
SQLException- if the insert fails
-
updateMenuItem
public static int updateMenuItem(int itemId, String name, String category, BigDecimal price, String description) throws SQLException Updates an existing menu item's fields.- Parameters:
itemId- ID of the item to updatename- new item namecategory- new categoryprice- new pricedescription- new description- Returns:
- number of rows affected
- Throws:
SQLException- if the update fails
-
updateMenuPrice
Updates only the price of a menu item.- Parameters:
itemId- ID of the item to updatenewPrice- the new price- Returns:
- number of rows affected
- Throws:
SQLException- if the update fails
-
deleteMenuItem
Deletes a menu item and its ingredient mappings in a transaction.- Parameters:
itemId- ID of the item to delete- Returns:
- number of rows deleted from the menu table
- Throws:
SQLException- if the delete fails (transaction is rolled back)
-
addMenuIngredientMapping
public static void addMenuIngredientMapping(int itemId, int ingredientId, double quantity) throws SQLException Adds a single ingredient mapping to a menu item's recipe.- Parameters:
itemId- the menu item IDingredientId- the inventory ingredient IDquantity- amount of ingredient consumed per order- Throws:
SQLException- if the insert fails
-
addMenuItemWithIngredients
public static int addMenuItemWithIngredients(String name, String category, BigDecimal price, String description, List<DB.IngredientRecipe> recipe) throws SQLException Adds a new seasonal (or regular) menu item together with its full ingredient recipe in a single transaction. This is the primary method for adding items to the POS — it creates the menu row and all menuingredientsmap rows atomically, so the item is never left in a half-configured state.- Parameters:
name- item display namecategory- item category (e.g. "Classic Drink", "Fruit Drink")price- item pricedescription- item descriptionrecipe- list of ingredient-quantity pairs defining the recipe; each ingredient must already exist in the inventory table- Returns:
- the generated menu item ID
- Throws:
SQLException- if the transaction fails (rolled back on error)
-
getInventory
Retrieves all inventory items ordered by ID.- Returns:
- list of all inventory items
- Throws:
SQLException- if the query fails
-
addInventoryItem
public static void addInventoryItem(int ingredientId, String name, double quantity, String units) throws SQLException Adds a new inventory item.- Parameters:
ingredientId- unique ingredient IDname- ingredient namequantity- initial stock quantityunits- unit of measurement- Throws:
SQLException- if the insert fails
-
updateInventoryItem
public static int updateInventoryItem(int ingredientId, String name, double quantity, String units) throws SQLException Updates an inventory item's name, quantity, and units.- Parameters:
ingredientId- ID of the ingredient to updatename- new ingredient namequantity- new quantityunits- new unit of measurement- Returns:
- number of rows affected
- Throws:
SQLException- if the update fails
-
updateInventoryQuantity
Updates only the quantity of an inventory item.- Parameters:
ingredientId- ID of the ingredient to updatequantity- new quantity value- Returns:
- number of rows affected
- Throws:
SQLException- if the update fails
-
deleteInventoryItem
Deletes an inventory item and its menu-ingredient mappings in a transaction.- Parameters:
ingredientId- ID of the ingredient to delete- Returns:
- number of rows deleted from the inventory table
- Throws:
SQLException- if the delete fails (transaction is rolled back)
-
getEmployees
Retrieves all employees ordered by ID.- Returns:
- list of all employees
- Throws:
SQLException- if the query fails
-
addEmployee
public static void addEmployee(int employeeId, String name, String access, int age, String phone, String password) throws SQLException Adds a new employee to the database.- Parameters:
employeeId- unique employee IDname- employee full nameaccess- access level ("manager" or "cashier")age- employee agephone- employee phone numberpassword- employee login password- Throws:
SQLException- if the insert fails
-
updateEmployee
public static int updateEmployee(int employeeId, String name, String access, int age, String phone) throws SQLException Updates an employee's name, access level, age, and phone.- Parameters:
employeeId- ID of the employee to updatename- new nameaccess- new access levelage- new agephone- new phone number- Returns:
- number of rows affected
- Throws:
SQLException- if the update fails
-
deleteEmployee
Deletes an employee and their check-in records in a transaction.- Parameters:
employeeId- ID of the employee to delete- Returns:
- number of rows deleted from the employees table
- Throws:
SQLException- if the delete fails (transaction is rolled back)
-
getEmployeeCheckins
Retrieves all employee check-in/check-out records.- Returns:
- list of all check-in records
- Throws:
SQLException- if the query fails
-
clockIn
Clocks an employee in, setting timein to now and clearing timeout. Uses upsert so re-clocking-in overwrites the previous record.- Parameters:
employeeId- ID of the employee to clock in- Throws:
SQLException- if the upsert fails
-
clockOut
Clocks an employee out by setting timeout to now.- Parameters:
employeeId- ID of the employee to clock out- Throws:
SQLException- if the update fails
-
submitOrder
public static UUID submitOrder(int employeeId, List<DB.OrderItem> items, BigDecimal total) throws SQLException Inserts a new order into orderhistory and decrements inventory for every ingredient used by the ordered items and their add-ons. Runs as a single transaction so inventory stays consistent.- Parameters:
employeeId- ID of the employee placing the orderitems- list of order line-itemstotal- total price for the order- Returns:
- the generated order UUID
- Throws:
SQLException- if the transaction fails (rolled back on error)
-
getOrders
Retrieves orders placed within a time window.- Parameters:
start- start of the time range (inclusive)end- end of the time range (inclusive)- Returns:
- list of orders in descending time order
- Throws:
SQLException- if the query fails
-
getSalesReport
Overall sales summary for a time window: total orders, total revenue, and an hour-by-hour breakdown.- Parameters:
start- start of the time range (inclusive)end- end of the time range (inclusive)- Returns:
- a SalesSummary with totals and hourly breakdown
- Throws:
SQLException- if the query fails
-
getProductSalesReport
public static List<DB.ProductSalesEntry> getProductSalesReport(Timestamp start, Timestamp end) throws SQLException Counts how many times each menu item was sold in a time window by parsing the JSONB orderdetails column.- Parameters:
start- start of the time range (inclusive)end- end of the time range (inclusive)- Returns:
- list of product sales entries sorted by quantity descending
- Throws:
SQLException- if the query fails
-
getProductUsageChart
public static List<DB.ProductUsageEntry> getProductUsageChart(Timestamp start, Timestamp end) throws SQLException Calculates total ingredient consumption in a time window by joining items sold (from JSONB) with their recipes in menuingredientsmap.- Parameters:
start- start of the time range (inclusive)end- end of the time range (inclusive)- Returns:
- list of ingredient usage entries sorted by amount descending
- Throws:
SQLException- if the query fails
-
getEmployeeSalesReport
public static List<DB.EmployeeSalesEntry> getEmployeeSalesReport(Timestamp start, Timestamp end) throws SQLException Per-employee sales performance in a time window: order count and total revenue for each employee who took orders.- Parameters:
start- start of the time range (inclusive)end- end of the time range (inclusive)- Returns:
- list of employee sales entries sorted by revenue descending
- Throws:
SQLException- if the query fails
-
hasZReportBeenRunToday
Checks whether the Z-Report has already been generated for today.- Returns:
- true if the Z-Report was already run today
- Throws:
SQLException- if the query fails
-
getXReport
X-Report: hourly sales breakdown for the current day of operation. Industry-standard register report that shows sales activity per hour so managers can identify rush and lull periods. This report has no side effects and can be run as often as desired.If the Z-Report has already been run today (day is closed), returns a zeroed-out summary since totals have been reset.
- Returns:
- today's sales summary with hourly breakdown, or zeros if day is closed
- Throws:
SQLException- if the query fails
-
getZReport
Z-Report: end-of-day report that totals all sales and resets counters for the next business day. Industry-standard close-out report that should only be run once per day when no more customers are expected.Includes the same hourly breakdown as the X-Report but covers the full day. After generation, the report date is logged so that subsequent X-Reports return zeros (totals are "reset") and the Z-Report cannot be generated again for the same day.
- Returns:
- today's complete sales summary, or null if already run today
- Throws:
SQLException- if the query fails
-
main
Connects to the DB and prints menu and employees.- Parameters:
args- command-line arguments (unused)
-