MenuItemDao, DatabaseConnection and MenuItem completed.

This commit is contained in:
2026-06-22 23:07:01 +03:30
parent 922d7afd74
commit 09f468d9dd
3 changed files with 51 additions and 10 deletions
+42 -5
View File
@@ -1,23 +1,60 @@
package dev.dao;
import dev.database.DatabaseConnection;
import dev.model.MenuItem;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MenuItemDao {
public List<MenuItem> findAll() {
List<MenuItem> items = new ArrayList<>();
String sql = "select * FROM menu_item";
// TODO:
// Retrieve all menu items
try (Connection connection = DatabaseConnection.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)
) {
while (resultSet.next()) {
MenuItem item = new MenuItem(
resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getString("description"),
resultSet.getDouble("price"),
resultSet.getString("category")
);
items.add(item);
}
} catch (SQLException e) {
System.err.println("Error fetching menu items: " + e.getMessage());
}
return null;
return items;
}
public MenuItem findById(int id) {
String sql = "SELECT * FROM menu_items WHERE id = ?";
// TODO:
// Find menu item by id
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
return new MenuItem(
rs.getInt("id"),
rs.getString("name"),
rs.getString("description"),
rs.getDouble("price"),
rs.getString("category")
);
}
}
} catch (SQLException e) {
System.err.println("Error finding menu item: " + e.getMessage());
}
return null;
}
@@ -1,6 +1,7 @@
package dev.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
@@ -17,11 +18,7 @@ public class DatabaseConnection {
public static Connection getConnection()
throws SQLException {
// TODO:
// Return a valid PostgreSQL connection
return null;
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
+7
View File
@@ -12,4 +12,11 @@ public class MenuItem {
private String category;
public MenuItem(int id, String name, String description, double price, String category) {
this.id = id;
this.name = name;
this.description = description;
this.price = price;
this.category = category;
}
}