Develop #1
@@ -1,23 +1,82 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.MenuItem;
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuItemDao {
|
public class MenuItemDao {
|
||||||
|
|
||||||
public List<MenuItem> findAll() {
|
public List<MenuItem> findAll() {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT id, name, description, price, category FROM menu_items";
|
||||||
// Retrieve all menu items
|
|
||||||
|
|
||||||
return null;
|
List<MenuItem> menuItems = new ArrayList<>();
|
||||||
|
|
||||||
|
try (
|
||||||
|
Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement statement = connection.prepareStatement(sql);
|
||||||
|
ResultSet resultSet = statement.executeQuery()
|
||||||
|
) {
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
|
||||||
|
MenuItem menuItem = new MenuItem();
|
||||||
|
|
||||||
|
menuItem.setId(resultSet.getInt("id"));
|
||||||
|
menuItem.setName(resultSet.getString("name"));
|
||||||
|
menuItem.setDescription(resultSet.getString("description"));
|
||||||
|
menuItem.setPrice(resultSet.getDouble("price"));
|
||||||
|
menuItem.setCategory(resultSet.getString("category"));
|
||||||
|
|
||||||
|
menuItems.add(menuItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
|
||||||
|
System.out.println("Error loading menu items: " + e.getMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return menuItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT id, name, description, price, category FROM menu_items WHERE id = ?";
|
||||||
// Find menu item by id
|
|
||||||
|
try (
|
||||||
|
Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement statement = connection.prepareStatement(sql)
|
||||||
|
) {
|
||||||
|
|
||||||
|
statement.setInt(1, id);
|
||||||
|
|
||||||
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
|
||||||
|
MenuItem menuItem = new MenuItem();
|
||||||
|
|
||||||
|
menuItem.setId(resultSet.getInt("id"));
|
||||||
|
menuItem.setName(resultSet.getString("name"));
|
||||||
|
menuItem.setDescription(resultSet.getString("description"));
|
||||||
|
menuItem.setPrice(resultSet.getDouble("price"));
|
||||||
|
menuItem.setCategory(resultSet.getString("category"));
|
||||||
|
|
||||||
|
return menuItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
|
||||||
|
System.out.println("Error finding menu item: " + e.getMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,48 @@ package dev.model;
|
|||||||
public class MenuItem {
|
public class MenuItem {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
private String category;
|
private String category;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCategory() {
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCategory(String category) {
|
||||||
|
this.category = category;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user