forked from AdvancedProgramming1404/WS-10-Database
database
This commit is contained in:
@@ -1,25 +1,133 @@
|
||||
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() {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
|
||||
return null;
|
||||
public List<MenuItem> findAll(){
|
||||
|
||||
|
||||
List<MenuItem> list=new ArrayList<>();
|
||||
|
||||
|
||||
String sql="SELECT * FROM menu_items";
|
||||
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
|
||||
ResultSet rs=ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
while(rs.next()){
|
||||
|
||||
|
||||
list.add(new MenuItem(
|
||||
|
||||
rs.getInt("id"),
|
||||
|
||||
rs.getString("name"),
|
||||
|
||||
rs.getString("description"),
|
||||
|
||||
rs.getDouble("price"),
|
||||
|
||||
rs.getString("category")
|
||||
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
public MenuItem findById(int id) {
|
||||
|
||||
// TODO:
|
||||
// Find menu item by id
|
||||
|
||||
|
||||
|
||||
|
||||
public MenuItem findById(int id){
|
||||
|
||||
|
||||
String sql =
|
||||
"SELECT * FROM menu_items WHERE id=?";
|
||||
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setInt(1,id);
|
||||
|
||||
|
||||
|
||||
ResultSet rs=ps.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){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +1,130 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class OrderDao {
|
||||
|
||||
|
||||
public int save(Order order) {
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
|
||||
String sql =
|
||||
"INSERT INTO orders(user_id,total_price) VALUES(?,?)";
|
||||
|
||||
|
||||
try(Connection con = DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)){
|
||||
|
||||
|
||||
ps.setInt(1, order.getUserId());
|
||||
|
||||
ps.setDouble(2, order.getTotalPrice());
|
||||
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
|
||||
|
||||
ResultSet rs = ps.getGeneratedKeys();
|
||||
|
||||
|
||||
if(rs.next()){
|
||||
|
||||
return rs.getInt(1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
|
||||
return null;
|
||||
|
||||
public List<Order> findByUserId(int userId){
|
||||
|
||||
|
||||
List<Order> orders = new ArrayList<>();
|
||||
|
||||
|
||||
String sql =
|
||||
"SELECT * FROM orders WHERE user_id=?";
|
||||
|
||||
|
||||
try(Connection con = DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setInt(1,userId);
|
||||
|
||||
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
while(rs.next()){
|
||||
|
||||
|
||||
Order order = new Order();
|
||||
|
||||
|
||||
order.setId(rs.getInt("id"));
|
||||
|
||||
order.setUserId(rs.getInt("user_id"));
|
||||
|
||||
order.setTotalPrice(
|
||||
rs.getDouble("total_price")
|
||||
);
|
||||
|
||||
|
||||
Timestamp timestamp =
|
||||
rs.getTimestamp("created_at");
|
||||
|
||||
|
||||
if(timestamp != null){
|
||||
|
||||
order.setCreatedAt(
|
||||
timestamp.toLocalDateTime()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
orders.add(order);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return orders;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,24 +1,144 @@
|
||||
package dev.dao;
|
||||
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
||||
public class OrderDetailDao {
|
||||
|
||||
public void save(OrderDetail detail) {
|
||||
|
||||
// TODO:
|
||||
// Insert order detail
|
||||
|
||||
public void save(OrderDetail detail){
|
||||
|
||||
|
||||
|
||||
String sql =
|
||||
"""
|
||||
INSERT INTO order_details
|
||||
(order_id,menu_item_id,quantity,price)
|
||||
VALUES(?,?,?,?)
|
||||
""";
|
||||
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
|
||||
ps.setInt(1,detail.getOrderId());
|
||||
|
||||
ps.setInt(2,detail.getMenuItemId());
|
||||
|
||||
ps.setInt(3,detail.getQuantity());
|
||||
|
||||
ps.setDouble(4,detail.getPrice());
|
||||
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
|
||||
return null;
|
||||
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId){
|
||||
|
||||
|
||||
List<OrderDetail> list =
|
||||
new ArrayList<>();
|
||||
|
||||
|
||||
String sql =
|
||||
"SELECT * FROM order_details WHERE order_id=?";
|
||||
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setInt(1,orderId);
|
||||
|
||||
|
||||
|
||||
ResultSet rs =
|
||||
ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
while(rs.next()){
|
||||
|
||||
|
||||
OrderDetail detail =
|
||||
new OrderDetail();
|
||||
|
||||
|
||||
detail.setId(
|
||||
rs.getInt("id")
|
||||
);
|
||||
|
||||
|
||||
detail.setOrderId(
|
||||
rs.getInt("order_id")
|
||||
);
|
||||
|
||||
|
||||
detail.setMenuItemId(
|
||||
rs.getInt("menu_item_id")
|
||||
);
|
||||
|
||||
|
||||
detail.setQuantity(
|
||||
rs.getInt("quantity")
|
||||
);
|
||||
|
||||
|
||||
detail.setPrice(
|
||||
rs.getDouble("price")
|
||||
);
|
||||
|
||||
|
||||
list.add(detail);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,23 +1,109 @@
|
||||
package dev.dao;
|
||||
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.User;
|
||||
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
|
||||
|
||||
public class UserDao {
|
||||
|
||||
public boolean save(User user) {
|
||||
|
||||
// TODO:
|
||||
// Insert user into database
|
||||
|
||||
public boolean save(User user){
|
||||
|
||||
|
||||
String sql =
|
||||
"INSERT INTO users(username,password,email) VALUES(?,?,?)";
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setString(1,user.getUsername());
|
||||
|
||||
ps.setString(2,user.getPassword());
|
||||
|
||||
ps.setString(3,user.getEmail());
|
||||
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
|
||||
// TODO:
|
||||
// Find a user by username
|
||||
|
||||
|
||||
|
||||
public User findByUsername(String username){
|
||||
|
||||
|
||||
String sql =
|
||||
"SELECT * FROM users WHERE username=?";
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setString(1,username);
|
||||
|
||||
|
||||
|
||||
ResultSet rs=ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
if(rs.next()){
|
||||
|
||||
|
||||
return new User(
|
||||
|
||||
rs.getInt("id"),
|
||||
|
||||
rs.getString("username"),
|
||||
|
||||
rs.getString("password"),
|
||||
|
||||
rs.getString("email")
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +1,43 @@
|
||||
package dev.database;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
public class DatabaseConnection {
|
||||
|
||||
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant_db"; // DB Server
|
||||
|
||||
private static final String USER = "postgres"; // Your Username
|
||||
private static final String URL =
|
||||
"jdbc:postgresql://localhost:5432/restaurant_db";
|
||||
|
||||
private static final String PASSWORD = "password"; // Your Password
|
||||
|
||||
private DatabaseConnection() {
|
||||
private static final String USER =
|
||||
"postgres";
|
||||
|
||||
|
||||
private static final String PASSWORD =
|
||||
"neda3113";
|
||||
|
||||
|
||||
|
||||
private DatabaseConnection(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Connection getConnection()
|
||||
throws SQLException {
|
||||
|
||||
// TODO:
|
||||
// Return a valid PostgreSQL connection
|
||||
|
||||
return null;
|
||||
return DriverManager.getConnection(
|
||||
URL,
|
||||
USER,
|
||||
PASSWORD
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package dev.model;
|
||||
|
||||
|
||||
public class MenuItem {
|
||||
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
@@ -12,4 +14,74 @@ public class MenuItem {
|
||||
|
||||
private String category;
|
||||
|
||||
|
||||
|
||||
public MenuItem(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package dev.model;
|
||||
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
||||
public class Order {
|
||||
|
||||
|
||||
private int id;
|
||||
|
||||
private int userId;
|
||||
@@ -12,4 +15,61 @@ public class Order {
|
||||
|
||||
private double totalPrice;
|
||||
|
||||
|
||||
|
||||
public Order(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Order(int userId,double totalPrice){
|
||||
|
||||
this.userId=userId;
|
||||
this.totalPrice=totalPrice;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public void setId(int id){
|
||||
this.id=id;
|
||||
}
|
||||
|
||||
|
||||
public int getUserId(){
|
||||
return userId;
|
||||
}
|
||||
|
||||
|
||||
public void setUserId(int userId){
|
||||
this.userId=userId;
|
||||
}
|
||||
|
||||
|
||||
public LocalDateTime getCreatedAt(){
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
|
||||
public void setCreatedAt(LocalDateTime createdAt){
|
||||
this.createdAt=createdAt;
|
||||
}
|
||||
|
||||
|
||||
public double getTotalPrice(){
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
|
||||
public void setTotalPrice(double totalPrice){
|
||||
this.totalPrice=totalPrice;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package dev.model;
|
||||
|
||||
|
||||
public class OrderDetail {
|
||||
|
||||
|
||||
private int id;
|
||||
|
||||
private int orderId;
|
||||
@@ -12,4 +14,75 @@ public class OrderDetail {
|
||||
|
||||
private double price;
|
||||
|
||||
|
||||
|
||||
public OrderDetail(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public OrderDetail(int orderId,
|
||||
int menuItemId,
|
||||
int quantity,
|
||||
double price){
|
||||
|
||||
this.orderId=orderId;
|
||||
this.menuItemId=menuItemId;
|
||||
this.quantity=quantity;
|
||||
this.price=price;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public void setId(int id){
|
||||
this.id=id;
|
||||
}
|
||||
|
||||
|
||||
public int getOrderId(){
|
||||
return orderId;
|
||||
}
|
||||
|
||||
|
||||
public void setOrderId(int orderId){
|
||||
this.orderId=orderId;
|
||||
}
|
||||
|
||||
|
||||
public int getMenuItemId(){
|
||||
return menuItemId;
|
||||
}
|
||||
|
||||
|
||||
public void setMenuItemId(int menuItemId){
|
||||
this.menuItemId=menuItemId;
|
||||
}
|
||||
|
||||
|
||||
public int getQuantity(){
|
||||
return quantity;
|
||||
}
|
||||
|
||||
|
||||
public void setQuantity(int quantity){
|
||||
this.quantity=quantity;
|
||||
}
|
||||
|
||||
|
||||
public double getPrice(){
|
||||
return price;
|
||||
}
|
||||
|
||||
|
||||
public void setPrice(double price){
|
||||
this.price=price;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,11 +3,67 @@ package dev.model;
|
||||
public class User {
|
||||
|
||||
private int id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String email;
|
||||
|
||||
|
||||
public User() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public User(String username, String password, String email) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
|
||||
public User(int id, String username, String password, String email) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,136 @@
|
||||
package dev.service;
|
||||
|
||||
|
||||
import dev.dao.UserDao;
|
||||
import dev.model.User;
|
||||
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
|
||||
|
||||
public class AuthService {
|
||||
|
||||
public boolean register(String username, String password, String email) {
|
||||
|
||||
// TODO:
|
||||
// Validate and register user
|
||||
private final UserDao userDao =
|
||||
new UserDao();
|
||||
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
public boolean register(String username,
|
||||
String password,
|
||||
String email){
|
||||
|
||||
|
||||
|
||||
User existingUser = userDao.findByUsername(username);
|
||||
|
||||
if (existingUser != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
String hashedPassword = hashPassword(password);
|
||||
|
||||
|
||||
User user = new User(username, hashedPassword, email);
|
||||
|
||||
|
||||
return userDao.save(user);
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
|
||||
// TODO:
|
||||
// Authenticate user
|
||||
|
||||
|
||||
|
||||
|
||||
public User login(String username,
|
||||
String password){
|
||||
|
||||
|
||||
|
||||
User user =
|
||||
userDao.findByUsername(username);
|
||||
|
||||
|
||||
|
||||
if(user==null){
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
String hashed =
|
||||
hashPassword(password);
|
||||
|
||||
|
||||
|
||||
if(user.getPassword().equals(hashed)){
|
||||
|
||||
|
||||
return user;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private String hashPassword(String password){
|
||||
|
||||
|
||||
try{
|
||||
|
||||
|
||||
MessageDigest md =
|
||||
MessageDigest.getInstance("SHA-256");
|
||||
|
||||
|
||||
byte[] bytes =
|
||||
md.digest(
|
||||
password.getBytes()
|
||||
);
|
||||
|
||||
|
||||
|
||||
StringBuilder sb =
|
||||
new StringBuilder();
|
||||
|
||||
|
||||
|
||||
for(byte b:bytes){
|
||||
|
||||
|
||||
sb.append(
|
||||
String.format("%02x",b)
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return sb.toString();
|
||||
|
||||
|
||||
|
||||
}catch(Exception e){
|
||||
|
||||
throw new RuntimeException(e);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,12 +1,54 @@
|
||||
package dev.service;
|
||||
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
||||
public class MenuService {
|
||||
|
||||
public void showMenu() {
|
||||
|
||||
// TODO:
|
||||
// Display menu items
|
||||
private final MenuItemDao dao =
|
||||
new MenuItemDao();
|
||||
|
||||
|
||||
|
||||
public void showMenu(){
|
||||
|
||||
|
||||
List<MenuItem> items =
|
||||
dao.findAll();
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"========== MENU =========="
|
||||
);
|
||||
|
||||
|
||||
|
||||
for(MenuItem item:items){
|
||||
|
||||
|
||||
System.out.println(
|
||||
|
||||
item.getId()
|
||||
+" - "
|
||||
+item.getName()
|
||||
+" $"
|
||||
+item.getPrice()
|
||||
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,26 +1,300 @@
|
||||
package dev.service;
|
||||
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.dao.OrderDao;
|
||||
import dev.dao.OrderDetailDao;
|
||||
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.Order;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
|
||||
public class OrderService {
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
private final MenuItemDao menuDao =
|
||||
new MenuItemDao();
|
||||
|
||||
|
||||
private final OrderDao orderDao =
|
||||
new OrderDao();
|
||||
|
||||
|
||||
private final OrderDetailDao detailDao =
|
||||
new OrderDetailDao();
|
||||
|
||||
|
||||
|
||||
public int placeOrder(int userId){
|
||||
|
||||
|
||||
Scanner scanner =
|
||||
new Scanner(System.in);
|
||||
|
||||
|
||||
|
||||
List<OrderDetail> cart =
|
||||
new ArrayList<>();
|
||||
|
||||
|
||||
double total = 0;
|
||||
|
||||
|
||||
|
||||
while(true){
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Enter menu item id (0 finish): "
|
||||
);
|
||||
|
||||
|
||||
int itemId =
|
||||
scanner.nextInt();
|
||||
|
||||
|
||||
|
||||
if(itemId == 0){
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
MenuItem item =
|
||||
menuDao.findById(itemId);
|
||||
|
||||
|
||||
|
||||
if(item == null){
|
||||
|
||||
System.out.println(
|
||||
"Item not found!"
|
||||
);
|
||||
|
||||
continue;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Enter quantity:"
|
||||
);
|
||||
|
||||
|
||||
int quantity =
|
||||
scanner.nextInt();
|
||||
|
||||
|
||||
|
||||
OrderDetail detail =
|
||||
new OrderDetail(
|
||||
0,
|
||||
item.getId(),
|
||||
quantity,
|
||||
item.getPrice()
|
||||
);
|
||||
|
||||
|
||||
|
||||
cart.add(detail);
|
||||
|
||||
|
||||
|
||||
total +=
|
||||
item.getPrice()
|
||||
*
|
||||
quantity;
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Added to cart."
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(cart.isEmpty()){
|
||||
|
||||
System.out.println(
|
||||
"Cart is empty"
|
||||
);
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Save Order first
|
||||
|
||||
Order order =
|
||||
new Order(
|
||||
userId,
|
||||
total
|
||||
);
|
||||
|
||||
|
||||
|
||||
int orderId =
|
||||
orderDao.save(order);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
for(OrderDetail detail:cart){
|
||||
|
||||
|
||||
detail.setOrderId(orderId);
|
||||
|
||||
|
||||
detailDao.save(detail);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Order created successfully!"
|
||||
);
|
||||
|
||||
|
||||
printReceipt(orderId);
|
||||
|
||||
|
||||
|
||||
return orderId;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void printReceipt(int orderId){
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"\n========== RECEIPT =========="
|
||||
);
|
||||
|
||||
|
||||
|
||||
List<OrderDetail> details =
|
||||
detailDao.findByOrderId(orderId);
|
||||
|
||||
|
||||
|
||||
double total = 0;
|
||||
|
||||
|
||||
|
||||
for(OrderDetail d:details){
|
||||
|
||||
|
||||
|
||||
MenuItem item =
|
||||
menuDao.findById(
|
||||
d.getMenuItemId()
|
||||
);
|
||||
|
||||
|
||||
|
||||
double subtotal =
|
||||
d.getQuantity()
|
||||
*
|
||||
d.getPrice();
|
||||
|
||||
|
||||
|
||||
total += subtotal;
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
item.getName()
|
||||
+" | Qty: "
|
||||
+d.getQuantity()
|
||||
+" | Unit: $"
|
||||
+d.getPrice()
|
||||
+" | Total: $"
|
||||
+subtotal
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"----------------------------"
|
||||
);
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Grand Total: $"
|
||||
+total
|
||||
);
|
||||
|
||||
|
||||
System.out.println(
|
||||
"============================"
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void showOrderHistory(int userId){
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"\n======= ORDER HISTORY ======="
|
||||
);
|
||||
|
||||
|
||||
|
||||
for(Order order:
|
||||
orderDao.findByUserId(userId)){
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Order ID: "
|
||||
+order.getId()
|
||||
+
|
||||
" Total: $"+
|
||||
order.getTotalPrice()
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,44 +1,302 @@
|
||||
package dev.ui;
|
||||
|
||||
|
||||
import dev.model.User;
|
||||
|
||||
import dev.service.AuthService;
|
||||
import dev.service.MenuService;
|
||||
import dev.service.OrderService;
|
||||
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
|
||||
public class ConsoleMenu {
|
||||
|
||||
|
||||
private final Scanner scanner =
|
||||
new Scanner(System.in);
|
||||
|
||||
public void start() {
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===== JAVA PIZZERIA =====");
|
||||
System.out.println("1. Login");
|
||||
System.out.println("2. Register");
|
||||
System.out.println("3. Exit");
|
||||
private final AuthService authService =
|
||||
new AuthService();
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
private final MenuService menuService =
|
||||
new MenuService();
|
||||
|
||||
|
||||
private final OrderService orderService =
|
||||
new OrderService();
|
||||
|
||||
|
||||
|
||||
private User currentUser;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void start(){
|
||||
|
||||
|
||||
|
||||
while(true){
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"""
|
||||
=======================================
|
||||
🍕 WELCOME TO JAVA PIZZERIA 🍕
|
||||
=======================================
|
||||
|
||||
1. Login
|
||||
2. Register New Account
|
||||
3. Exit
|
||||
|
||||
Choose an option:
|
||||
"""
|
||||
);
|
||||
|
||||
|
||||
|
||||
int choice =
|
||||
scanner.nextInt();
|
||||
|
||||
|
||||
|
||||
scanner.nextLine();
|
||||
|
||||
|
||||
|
||||
switch(choice){
|
||||
|
||||
|
||||
case 1:
|
||||
// TODO
|
||||
|
||||
login();
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
case 2:
|
||||
// TODO
|
||||
|
||||
register();
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
case 3:
|
||||
|
||||
System.out.println(
|
||||
"Goodbye!"
|
||||
);
|
||||
|
||||
return;
|
||||
|
||||
|
||||
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
|
||||
System.out.println(
|
||||
"Invalid option"
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void register() {
|
||||
|
||||
System.out.println("Username:");
|
||||
String username = scanner.nextLine();
|
||||
|
||||
System.out.println("Password:");
|
||||
String password = scanner.nextLine();
|
||||
|
||||
System.out.println("Email:");
|
||||
String email = scanner.nextLine();
|
||||
|
||||
boolean success = authService.register(username, password, email);
|
||||
|
||||
if (success) {
|
||||
System.out.println("Registration successful!");
|
||||
} else {
|
||||
System.out.println("Username already exists!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void login(){
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Username:"
|
||||
);
|
||||
|
||||
String username =
|
||||
scanner.nextLine();
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Password:"
|
||||
);
|
||||
|
||||
String password =
|
||||
scanner.nextLine();
|
||||
|
||||
|
||||
|
||||
|
||||
currentUser =
|
||||
authService.login(
|
||||
username,
|
||||
password
|
||||
);
|
||||
|
||||
|
||||
|
||||
if(currentUser != null){
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Login successful"
|
||||
);
|
||||
|
||||
|
||||
mainMenu();
|
||||
|
||||
|
||||
}
|
||||
|
||||
else{
|
||||
|
||||
|
||||
System.out.println(
|
||||
"Wrong username or password"
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void mainMenu(){
|
||||
|
||||
|
||||
|
||||
while(currentUser != null){
|
||||
|
||||
|
||||
|
||||
System.out.println(
|
||||
"""
|
||||
=======================================
|
||||
🍽️ MAIN MENU 🍽️
|
||||
=======================================
|
||||
1. View Menu
|
||||
2. Place Order
|
||||
3. Order History
|
||||
4. Logout
|
||||
=========================
|
||||
"""
|
||||
);
|
||||
|
||||
|
||||
|
||||
int choice =
|
||||
scanner.nextInt();
|
||||
|
||||
|
||||
|
||||
switch(choice){
|
||||
|
||||
|
||||
case 1:
|
||||
|
||||
menuService.showMenu();
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
case 2:
|
||||
|
||||
orderService.placeOrder(
|
||||
currentUser.getId()
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
case 3:
|
||||
|
||||
orderService.showOrderHistory(
|
||||
currentUser.getId()
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
case 4:
|
||||
|
||||
currentUser=null;
|
||||
|
||||
System.out.println(
|
||||
"Logged out"
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
default:
|
||||
|
||||
System.out.println(
|
||||
"Invalid option"
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user