view menu
This commit is contained in:
@@ -1,25 +1,77 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.MenuItem;
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
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:
|
List<MenuItem> result = new ArrayList<>();
|
||||||
// Retrieve all menu items
|
|
||||||
|
String quary = "SELECT * FROM menu_items";
|
||||||
|
|
||||||
|
|
||||||
|
try (Connection c = DatabaseConnection.getConnection();
|
||||||
|
Statement pst = c.createStatement()){
|
||||||
|
|
||||||
|
ResultSet rst = pst.executeQuery(quary);
|
||||||
|
while (rst.next()){
|
||||||
|
MenuItem item = new MenuItem(rst.getInt("id"),
|
||||||
|
rst.getString("name"),
|
||||||
|
rst.getString("description"),
|
||||||
|
rst.getDouble("price"),
|
||||||
|
rst.getString("category"));
|
||||||
|
result.add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
String quary = "SELECT * FROM menu_items WHERE id = ?";
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Find menu item by id
|
try (Connection c = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement pst = c.prepareStatement(quary)){
|
||||||
|
|
||||||
|
pst.setInt(1 , id);
|
||||||
|
|
||||||
|
try (ResultSet rst = pst.executeQuery()){
|
||||||
|
if(rst.next()){
|
||||||
|
MenuItem item = new MenuItem(rst.getInt("id"),
|
||||||
|
rst.getString("name"),
|
||||||
|
rst.getString("descrition"),
|
||||||
|
rst.getDouble("price"),
|
||||||
|
rst.getString("category"));
|
||||||
|
|
||||||
|
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,13 +3,38 @@ 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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCategory() {
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,28 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.MenuItemDao;
|
||||||
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuService {
|
public class MenuService {
|
||||||
|
|
||||||
|
private MenuItemDao menuItemDao = new MenuItemDao();
|
||||||
|
|
||||||
public void showMenu() {
|
public void showMenu() {
|
||||||
|
|
||||||
// TODO:
|
List<MenuItem> allfood = menuItemDao.findAll();
|
||||||
// Display menu items
|
|
||||||
|
for(int i = 0 ; i < allfood.size() ; i++){
|
||||||
|
MenuItem food = allfood.get(i);
|
||||||
|
System.out.printf("%-5d | %-20s | %-15s | %-10.2f | %s%n",
|
||||||
|
food.getId(),
|
||||||
|
food.getName(),
|
||||||
|
food.getCategory(),
|
||||||
|
food.getPrice(),
|
||||||
|
food.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package dev.ui;
|
|||||||
|
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
import dev.service.AuthService;
|
import dev.service.AuthService;
|
||||||
|
import dev.service.MenuService;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
@@ -9,6 +10,7 @@ public class ConsoleMenu {
|
|||||||
|
|
||||||
private final Scanner scanner = new Scanner(System.in);
|
private final Scanner scanner = new Scanner(System.in);
|
||||||
private final AuthService authService = new AuthService();
|
private final AuthService authService = new AuthService();
|
||||||
|
private final MenuService menuService= new MenuService();
|
||||||
private User user;
|
private User user;
|
||||||
|
|
||||||
|
|
||||||
@@ -42,7 +44,7 @@ public class ConsoleMenu {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.out.println("Login success");
|
System.out.println("Login success");
|
||||||
|
menu();
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -87,6 +89,28 @@ public class ConsoleMenu {
|
|||||||
"Choose an option: ");
|
"Choose an option: ");
|
||||||
|
|
||||||
int choice = scanner.nextInt();
|
int choice = scanner.nextInt();
|
||||||
|
switch (choice){
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
menuService.showMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user