diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..ab1f416
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,10 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Ignored default folder with query files
+/queries/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..bf2c501
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..aa00ffa
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..4158879
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..eba6e1f
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml
new file mode 100644
index 0000000..ac6bd0c
--- /dev/null
+++ b/.idea/sqldialects.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..8306744
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/database.sql b/database.sql
index 2169346..530aa65 100644
--- a/database.sql
+++ b/database.sql
@@ -8,125 +8,40 @@
-- 5. Insert at least 3 menu items.
-- 6. The script should be executable from start to finish without errors.
+CREATE TABLE users (
+ id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+ username VARCHAR(50) UNIQUE,
+ password_hash VARCHAR(50) NOT NULL,
+ email VARCHAR(150)
+);
+
+CREATE TABLE menu_items (
+ id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+ name_ VARCHAR(70) NOT NULL,
+ description_ TEXT,
+ price NUMERIC(7, 2) NOT NULL CHECK(price > 0),
+ category VARCHAR(50)
+);
+
+CREATE TABLE orders (
+ id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+ user_id INT NOT NULL REFERENCES users(id),
+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),
+ total_price NUMERIC(7, 2) NOT NULL DEFAULT 0
+);
+
+CREATE TABLE order_details (
+ id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+ order_id INT NOT NULL REFERENCES orders(id),
+ menu_item_id INT NOT NULL REFERENCES menu_items(id),
+ quantity SMALLINT NOT NULL CHECK(quantity > 0),
+ unit_price NUMERIC(7, 2) NOT NULL CHECK(unit_price > 0)
+);
--- =======================================================
--- USER TABLE
--- =======================================================
---
--- Represents customers using the system.
---
--- Required information:
--- - Unique identifier
--- - Username
--- - Password
--- - Email (optional)
---
--- Requirements:
--- - Each user must have a unique identifier.
--- - Usernames must be unique.
--- - Username and password are required.
--- - Passwords should not be stored in plain text.
---
--- CREATE TABLE ...
-
-
-
--- =======================================================
--- MENU ITEM TABLE
--- =======================================================
---
--- Represents available food and drink items.
---
--- Required information:
--- - Unique identifier
--- - Name
--- - Description (optional)
--- - Price
--- - Category (optional)
---
--- Requirements:
--- - Each menu item must have a unique identifier.
--- - Name is required.
--- - Price must always be positive.
---
--- CREATE TABLE ...
-
-
-
--- =======================================================
--- ORDER TABLE
--- =======================================================
---
--- Represents orders placed by customers.
---
--- Required information:
--- - Unique identifier
--- - Reference to customer
--- - Creation date and time
--- - Total price
---
--- Requirements:
--- - Each order must belong to exactly one user.
--- - A user can have multiple orders.
--- - The relationship between User and Order must be implemented.
---
--- Note:
--- Avoid using reserved SQL keywords as table names.
--- Consider using a name such as "orders" or "customer_orders".
---
--- CREATE TABLE ...
-
-
-
--- =======================================================
--- ORDER DETAIL TABLE
--- =======================================================
---
--- Represents items inside an order.
---
--- Required information:
--- - Unique identifier
--- - Reference to an order
--- - Reference to a menu item
--- - Quantity
--- - Item price at purchase time
---
--- Requirements:
--- - Each detail record must belong to one order.
--- - Each detail record must reference one menu item.
--- - Quantity must always be greater than zero.
--- - Store the item's price at the moment of purchase.
---
--- CREATE TABLE ...
-
-
-
--- =======================================================
--- INITIAL MENU DATA
--- =======================================================
---
--- Insert at least 3 food or drink items.
---
--- Example categories:
--- - Pizza
--- - Burger
--- - Pasta
--- - Drink
---
--- INSERT INTO ...
-
-
-
--- =======================================================
--- OPTIONAL TEST DATA
--- =======================================================
---
--- You may insert sample users and orders for testing.
--- This section is optional.
---
--- INSERT INTO ...
-
+INSERT INTO menu_items(name_, description_, price, category) VALUES ('Bacon Pizza', 'Medium', 15.99, 'Pizza');
+INSERT INTO menu_items(name_, description_, price, category) VALUES ('Vegan Pizza', 'Small', 10.99, 'Pizza');
+INSERT INTO menu_items(name_, description_, price, category) VALUES ('Coke', 'Mini Can', 2.99, 'Drink');
-- =======================================================
diff --git a/pom.xml b/pom.xml
index 028ca50..eef0254 100644
--- a/pom.xml
+++ b/pom.xml
@@ -24,7 +24,7 @@
org.postgresql
postgresql
- ${postgresql.version}
+ 42.7.8
diff --git a/src/main/java/dev/dao/MenuItemDao.java b/src/main/java/dev/dao/MenuItemDao.java
index 3bbebe9..c68b474 100644
--- a/src/main/java/dev/dao/MenuItemDao.java
+++ b/src/main/java/dev/dao/MenuItemDao.java
@@ -1,7 +1,13 @@
package dev.dao;
+import dev.database.DatabaseConnection;
import dev.model.MenuItem;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
import java.util.List;
public class MenuItemDao {
@@ -10,8 +16,29 @@ public class MenuItemDao {
// TODO:
// Retrieve all menu items
+ List menuItems = new ArrayList<>();
+ try (Connection c = DatabaseConnection.getConnection()) {
+ String selectQuery = "SELECT * FROM menu_items";
+ try (Statement stmt = c.createStatement();
+ ResultSet rs = stmt.executeQuery(selectQuery)) {
- return null;
+ while (rs.next()) {
+ int id = rs.getInt("id");
+ String name = rs.getString("name_");
+ String description = rs.getString("description_");
+ double price = rs.getDouble("price");
+ String category = rs.getString("category");
+ MenuItem menuItem = new MenuItem(id, name, description, price, category);
+ menuItems.add(menuItem);
+ }
+ }
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
+
+ return menuItems;
}
public MenuItem findById(int id) {
@@ -19,6 +46,28 @@ public class MenuItemDao {
// TODO:
// Find menu item by id
+ try (Connection c = DatabaseConnection.getConnection()) {
+ String selectQuery = "SELECT * FROM menu_items";
+ try (Statement stmt = c.createStatement();
+ ResultSet rs = stmt.executeQuery(selectQuery)) {
+
+ while (rs.next()) {
+ int id_ = rs.getInt("id");
+ if (id_ == id) {
+ String name = rs.getString("name_");
+ String description = rs.getString("description_");
+ double price = rs.getDouble("price");
+ String category = rs.getString("category");
+ return new MenuItem(id, name, description, price, category);
+ }
+ }
+ }
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
+
return null;
}
diff --git a/src/main/java/dev/dao/OrderDao.java b/src/main/java/dev/dao/OrderDao.java
index 00999a2..6347b1d 100644
--- a/src/main/java/dev/dao/OrderDao.java
+++ b/src/main/java/dev/dao/OrderDao.java
@@ -1,8 +1,14 @@
package dev.dao;
+import dev.database.DatabaseConnection;
+import dev.model.MenuItem;
import dev.model.Order;
+import java.sql.*;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
public class OrderDao {
@@ -11,6 +17,32 @@ public class OrderDao {
// TODO:
// Insert order and return generated id
+ try (Connection c = DatabaseConnection.getConnection()) {
+ String insertQuery = "INSERT INTO orders (user_id, created_at, total_price) VALUES (?, ?, ?)";
+ try (PreparedStatement pstmt = c.prepareStatement(insertQuery)) {
+ pstmt.setInt(1, order.getUserId());
+ pstmt.setTimestamp(2, Timestamp.valueOf(order.getCreatedAt()));
+ pstmt.setDouble(3, order.getTotalPrice());
+ pstmt.executeUpdate();
+ }
+
+ String selectQuery = "SELECT * FROM orders ORDER BY created_at DESC";
+ try (Statement stmt = c.createStatement();
+ ResultSet rs = stmt.executeQuery(selectQuery)) {
+
+ while (rs.next()) {
+ int user_id = rs.getInt("user_id");
+ if (user_id == order.getUserId()) {
+ return rs.getInt("id");
+ }
+ }
+ }
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
+
return -1;
}
@@ -19,7 +51,30 @@ public class OrderDao {
// TODO:
// Retrieve all orders of a user
- return null;
+ List orders = new ArrayList<>();
+ try (Connection c = DatabaseConnection.getConnection()) {
+ String selectQuery = "SELECT * FROM orders";
+ try (Statement stmt = c.createStatement();
+ ResultSet rs = stmt.executeQuery(selectQuery)) {
+
+ while (rs.next()) {
+ int user_id = rs.getInt("user_id");
+ if (user_id == userId) {
+ int id = rs.getInt("id");
+ LocalDateTime LDT = rs.getTimestamp("created_at").toLocalDateTime();
+ double total_price = rs.getDouble("total_price");
+ Order order = new Order(id, user_id, LDT, total_price);
+ orders.add(order);
+ }
+ }
+ }
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
+
+ return orders;
}
}
\ No newline at end of file
diff --git a/src/main/java/dev/dao/OrderDetailDao.java b/src/main/java/dev/dao/OrderDetailDao.java
index ed42f56..e97e091 100644
--- a/src/main/java/dev/dao/OrderDetailDao.java
+++ b/src/main/java/dev/dao/OrderDetailDao.java
@@ -1,7 +1,12 @@
package dev.dao;
+import dev.database.DatabaseConnection;
+import dev.model.Order;
import dev.model.OrderDetail;
+import java.sql.*;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
import java.util.List;
public class OrderDetailDao {
@@ -11,6 +16,36 @@ public class OrderDetailDao {
// TODO:
// Insert order detail
+ try (Connection c = DatabaseConnection.getConnection()) {
+
+ double unitPrice = 0.0;
+
+ String selectQuery = "SELECT * FROM menu_items";
+ try (Statement stmt = c.createStatement();
+ ResultSet rs = stmt.executeQuery(selectQuery)) {
+
+ while (rs.next()) {
+ int menuItemId = rs.getInt("id");
+ if (menuItemId == detail.getMenuItemId()) {
+ unitPrice = rs.getDouble("price");
+ }
+ }
+ }
+
+ String insertQuery = "INSERT INTO order_details (order_id, menu_item_id, quantity, unit_price) VALUES (?, ?, ?, ?)";
+ try (PreparedStatement pstmt = c.prepareStatement(insertQuery)) {
+ pstmt.setInt(1, detail.getOrderId());
+ pstmt.setInt(2, detail.getMenuItemId());
+ pstmt.setInt(3, detail.getQuantity());
+ pstmt.setDouble(4, unitPrice);
+ pstmt.executeUpdate();
+ }
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
+
}
public List findByOrderId(int orderId) {
@@ -18,7 +53,31 @@ public class OrderDetailDao {
// TODO:
// Retrieve order details
- return null;
+ List orderDetails = new ArrayList<>();
+ try (Connection c = DatabaseConnection.getConnection()) {
+ String selectQuery = "SELECT * FROM order_details";
+ try (Statement stmt = c.createStatement();
+ ResultSet rs = stmt.executeQuery(selectQuery)) {
+
+ while (rs.next()) {
+ int order_id = rs.getInt("order_id");
+ if (order_id == orderId) {
+ int id = rs.getInt("id");
+ int menu_item_id = rs.getInt("menu_item_id");
+ int quantity = rs.getInt("quantity");
+ double unit_price = rs.getDouble("unit_price");
+ OrderDetail orderDetail = new OrderDetail(id, order_id, menu_item_id, quantity, unit_price);
+ orderDetails.add(orderDetail);
+ }
+ }
+ }
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
+
+ return orderDetails;
}
}
\ No newline at end of file
diff --git a/src/main/java/dev/dao/UserDao.java b/src/main/java/dev/dao/UserDao.java
index af08106..4c4dd81 100644
--- a/src/main/java/dev/dao/UserDao.java
+++ b/src/main/java/dev/dao/UserDao.java
@@ -1,7 +1,11 @@
package dev.dao;
+import dev.database.DatabaseConnection;
import dev.model.User;
+import java.sql.*;
+import java.util.Objects;
+
public class UserDao {
public boolean save(User user) {
@@ -9,6 +13,21 @@ public class UserDao {
// TODO:
// Insert user into database
+ try (Connection c = DatabaseConnection.getConnection()) {
+ String insertQuery = "INSERT INTO users (username, password_hash, email) VALUES (?, ?, ?)";
+ try (PreparedStatement pstmt = c.prepareStatement(insertQuery)) {
+ pstmt.setString(1, user.getUsername());
+ pstmt.setString(2, user.getPassword());
+ pstmt.setString(3, user.getEmail());
+ pstmt.executeUpdate();
+ return true;
+ }
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
+
return false;
}
@@ -17,6 +36,27 @@ public class UserDao {
// TODO:
// Find a user by username
+ try (Connection c = DatabaseConnection.getConnection()) {
+ String selectQuery = "SELECT * FROM users";
+ try (Statement stmt = c.createStatement();
+ ResultSet rs = stmt.executeQuery(selectQuery)) {
+
+ while (rs.next()) {
+ String Username = rs.getString("username");
+ if (Objects.equals(Username, username)) {
+ int id = rs.getInt("id");
+ String password = rs.getString("password_hash");
+ String email = rs.getString("email");
+ return new User(id, username, password, email);
+ }
+ }
+ }
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
+
return null;
}
diff --git a/src/main/java/dev/database/DatabaseConnection.java b/src/main/java/dev/database/DatabaseConnection.java
index da45535..8daedea 100644
--- a/src/main/java/dev/database/DatabaseConnection.java
+++ b/src/main/java/dev/database/DatabaseConnection.java
@@ -1,15 +1,16 @@
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 URL = "jdbc:postgresql://localhost:5432/restaurant"; // DB Server
private static final String USER = "postgres"; // Your Username
- private static final String PASSWORD = "password"; // Your Password
+ private static final String PASSWORD = "arshida"; // Your Password
private DatabaseConnection() {
@@ -20,8 +21,7 @@ public class DatabaseConnection {
// TODO:
// Return a valid PostgreSQL connection
-
- return null;
+ return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
\ No newline at end of file
diff --git a/src/main/java/dev/model/MenuItem.java b/src/main/java/dev/model/MenuItem.java
index 265ecc1..6dc480f 100644
--- a/src/main/java/dev/model/MenuItem.java
+++ b/src/main/java/dev/model/MenuItem.java
@@ -2,6 +2,14 @@ package dev.model;
public class 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;
+ }
+
private int id;
private String name;
@@ -12,4 +20,16 @@ public class MenuItem {
private String category;
+ public int getId() { return id; }
+
+ public String getName() { return name; }
+
+ public String getDescription() { return description; }
+
+ public double getPrice() { return price; }
+
+ public String getCategory() { return category; }
+
+ public void setId(int Id) { id = Id; }
+
}
\ No newline at end of file
diff --git a/src/main/java/dev/model/Order.java b/src/main/java/dev/model/Order.java
index 32049cb..cd80105 100644
--- a/src/main/java/dev/model/Order.java
+++ b/src/main/java/dev/model/Order.java
@@ -4,6 +4,13 @@ import java.time.LocalDateTime;
public class Order {
+ public Order (int id, int userId, LocalDateTime createdAt, double totalPrice) {
+ this.id = id;
+ this.userId = userId;
+ this.createdAt = createdAt;
+ this.totalPrice = totalPrice;
+ }
+
private int id;
private int userId;
@@ -12,4 +19,14 @@ public class Order {
private double totalPrice;
+ public int getId() { return id; }
+
+ public int getUserId() { return userId; }
+
+ public LocalDateTime getCreatedAt() { return createdAt; }
+
+ public double getTotalPrice() { return totalPrice; }
+
+ public void setId(int Id) { id = Id; }
+
}
\ No newline at end of file
diff --git a/src/main/java/dev/model/OrderDetail.java b/src/main/java/dev/model/OrderDetail.java
index b5b8bbe..911c099 100644
--- a/src/main/java/dev/model/OrderDetail.java
+++ b/src/main/java/dev/model/OrderDetail.java
@@ -2,6 +2,14 @@ package dev.model;
public class OrderDetail {
+ public OrderDetail (int id, int orderId, int menuItemId, int quantity, double price) {
+ this.id = id;
+ this.orderId = orderId;
+ this.menuItemId = menuItemId;
+ this.quantity = quantity;
+ this.price = price;
+ }
+
private int id;
private int orderId;
@@ -12,4 +20,16 @@ public class OrderDetail {
private double price;
+ public int getId() { return id; }
+
+ public int getOrderId() { return orderId; }
+
+ public int getMenuItemId() { return menuItemId; }
+
+ public int getQuantity() { return quantity; }
+
+ public double getPrice() { return price; }
+
+ public void setId(int Id) { id = Id; }
+
}
\ No newline at end of file
diff --git a/src/main/java/dev/model/User.java b/src/main/java/dev/model/User.java
index 0ab60c6..aad90d2 100644
--- a/src/main/java/dev/model/User.java
+++ b/src/main/java/dev/model/User.java
@@ -2,6 +2,13 @@ package dev.model;
public class User {
+ public User (int id, String username, String password, String email) {
+ this.id = id;
+ this.username = username;
+ this.password = password;
+ this.email = email;
+ }
+
private int id;
private String username;
@@ -10,4 +17,14 @@ public class User {
private String email;
+ public int getId() { return id; }
+
+ public String getUsername() { return username; }
+
+ public String getPassword() { return password; }
+
+ public String getEmail() { return email; }
+
+ public void setId(int Id) { id = Id; }
+
}
\ No newline at end of file
diff --git a/src/main/java/dev/service/AuthService.java b/src/main/java/dev/service/AuthService.java
index bee866a..6dfbdd7 100644
--- a/src/main/java/dev/service/AuthService.java
+++ b/src/main/java/dev/service/AuthService.java
@@ -1,14 +1,27 @@
package dev.service;
+import dev.dao.UserDao;
+import dev.database.DatabaseConnection;
+import dev.model.OrderDetail;
import dev.model.User;
+import java.sql.*;
+import java.util.Objects;
+
public class AuthService {
+ UserDao userDao = new UserDao();
+
public boolean register(String username, String password, String email) {
// TODO:
// Validate and register user
+ User user = userDao.findByUsername(username);
+ if (user == null) {
+ return userDao.save(new User(-1, username, password, email));
+ }
+ else System.out.println("Username already exists.");
return false;
}
@@ -16,8 +29,11 @@ public class AuthService {
// TODO:
// Authenticate user
+ User user = userDao.findByUsername(username);
- return null;
+ if (user == null) System.out.println("Username was not found.");
+
+ return user;
}
}
\ No newline at end of file
diff --git a/src/main/java/dev/service/MenuService.java b/src/main/java/dev/service/MenuService.java
index 6dbf4da..0768be1 100644
--- a/src/main/java/dev/service/MenuService.java
+++ b/src/main/java/dev/service/MenuService.java
@@ -1,12 +1,31 @@
package dev.service;
+import dev.dao.MenuItemDao;
+import dev.database.DatabaseConnection;
+import dev.model.MenuItem;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+
public class MenuService {
+ MenuItemDao menuItemDao = new MenuItemDao();
+
public void showMenu() {
// TODO:
// Display menu items
+ List Items = menuItemDao.findAll();
+
+ System.out.println("Available Items:");
+ for (MenuItem i : Items) {
+ System.out.println(i.getId() + " | " + i.getName() + " | " + i.getDescription() + " | " + "$" + i.getPrice());
+ }
+
}
}
\ No newline at end of file
diff --git a/src/main/java/dev/service/OrderService.java b/src/main/java/dev/service/OrderService.java
index 14708f8..86a2925 100644
--- a/src/main/java/dev/service/OrderService.java
+++ b/src/main/java/dev/service/OrderService.java
@@ -1,12 +1,91 @@
package dev.service;
+import dev.dao.MenuItemDao;
+import dev.dao.OrderDao;
+import dev.dao.OrderDetailDao;
+import dev.database.DatabaseConnection;
+import dev.model.MenuItem;
+import dev.model.Order;
+import dev.model.OrderDetail;
+
+import java.sql.*;
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.Scanner;
+
public class OrderService {
+ OrderDetailDao orderDetailDao = new OrderDetailDao();
+ OrderDao orderDao = new OrderDao();
+ MenuItemDao menuItemDao = new MenuItemDao();
+
public void placeOrder(int userId) {
// TODO:
// Create order
+ System.out.println("[Placing Order]");
+
+ int order_id = orderDao.save(new Order(-1, userId, LocalDateTime.now(), 0));
+ if (order_id == -1) {
+ System.out.println("Order id was not found.");
+ return;
+ }
+
+ Scanner scanner = new Scanner(System.in);
+
+ while (true){
+ System.out.print("\nEnter the ID of the item to add (or 0 to finish): ");
+ int itemId = scanner.nextInt();
+
+ if (itemId == 0) {
+
+ try (Connection c = DatabaseConnection.getConnection()) {
+
+ double total = 0.0;
+ String selectQuery = "SELECT * FROM order_details";
+ try (Statement stmt = c.createStatement();
+ ResultSet rs = stmt.executeQuery(selectQuery)) {
+
+ while (rs.next()) {
+ int orderId = rs.getInt("order_id");
+ if (orderId == order_id) {
+ double price = rs.getDouble("unit_price");
+ int quantity = rs.getInt("quantity");
+ total += price * quantity;
+ }
+ }
+ }
+
+ String insertQuery = "UPDATE orders SET total_price = ? WHERE id = ? ";
+ try (PreparedStatement pstmt = c.prepareStatement(insertQuery)) {
+ pstmt.setDouble(1, total);
+ pstmt.setInt(2, order_id);
+ pstmt.executeUpdate();
+ }
+ }
+ catch (SQLException e) {
+ //
+ }
+
+ break;
+ }
+
+ System.out.print("\nEnter quantity: ");
+ int quantity = scanner.nextInt();
+ System.out.println();
+
+ orderDetailDao.save(new OrderDetail(-1, order_id, itemId, quantity, 0));
+
+ MenuItem item = menuItemDao.findById(itemId);
+
+ System.out.println("Added " + quantity + "x " + item.getName() + " to your cart. \n");
+ }
+
+ printReceipt(order_id);
+
+ System.out.println("Order saved successfully!");
+
}
public void printReceipt(int orderId) {
@@ -14,6 +93,47 @@ public class OrderService {
// TODO:
// Print order receipt
+ System.out.println("[Order Summary / Receipt]");
+
+ try (Connection c = DatabaseConnection.getConnection()) {
+
+ double total_price = 1.0;
+ String selectQuery1 = "SELECT * FROM orders";
+ try (Statement stmt1 = c.createStatement();
+ ResultSet rs1 = stmt1.executeQuery(selectQuery1)) {
+
+ boolean isInOrders = false;
+ while (rs1.next()) {
+ int OrderId = rs1.getInt("id");
+ if (orderId == OrderId) {
+ isInOrders = true;
+ total_price = rs1.getDouble("total_price");
+ break;
+ }
+ }
+ if (!isInOrders) {
+ System.out.println("Order was not found.");
+ return;
+ }
+ }
+
+ List orderDetails = orderDetailDao.findByOrderId(orderId);
+
+ System.out.println("Item | Unit Price | Quantity | Total");
+ System.out.println("---------------------------------------");
+ for (OrderDetail od : orderDetails) {
+ MenuItem menuItem = menuItemDao.findById(od.getMenuItemId());
+ System.out.println(menuItem.getName() + " | " + "$" + od.getPrice() +
+ " | " + od.getQuantity() + " | " + "$" + od.getPrice() * od.getQuantity());
+ System.out.println("---------------------------------------");
+ }
+ System.out.println("Final Total: " + "$" + total_price + "\n");
+
+ }
+ catch (SQLException e) {
+ System.err.println("Error in connection with database: " + e.getMessage());
+ e.printStackTrace();
+ }
}
public void showOrderHistory(int userId) {
@@ -21,6 +141,13 @@ public class OrderService {
// TODO:
// Display user's order history
+ List orders = orderDao.findByUserId(userId);
+
+ System.out.println("Order Id | Date & Time | Total Price");
+ for (Order o : orders) {
+ System.out.println(o.getId() + " | " + o.getCreatedAt() + " | " + "$" + o.getTotalPrice());
+ }
+
}
}
\ No newline at end of file
diff --git a/src/main/java/dev/ui/ConsoleMenu.java b/src/main/java/dev/ui/ConsoleMenu.java
index 663dc0c..2f5c7ed 100644
--- a/src/main/java/dev/ui/ConsoleMenu.java
+++ b/src/main/java/dev/ui/ConsoleMenu.java
@@ -1,5 +1,10 @@
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 {
@@ -7,8 +12,49 @@ public class ConsoleMenu {
private final Scanner scanner =
new Scanner(System.in);
+ public void mainMenu(User user) {
+ OrderService orderService = new OrderService();
+
+ while (true) {
+ System.out.println("=======================================\n" +
+ " MAIN MENU \n" +
+ "=======================================\n" +
+ "1. Place a New Order\n" +
+ "2. View Order History\n" +
+ "3. Logout\n" +
+ "=======================================");
+ System.out.print("Choose an option: ");
+
+ int choice = scanner.nextInt();
+ System.out.println();
+ switch (choice) {
+ case 1: {
+ MenuService menuService = new MenuService();
+ menuService.showMenu();
+ orderService.placeOrder(user.getId());
+ System.out.println("Press _c_ to continue...");
+ scanner.next();
+ break;
+ }
+ case 2: {
+ orderService.showOrderHistory(user.getId());
+ System.out.println("Press _c_ to continue...");
+ scanner.next();
+ break;
+ }
+ case 3: {
+ return;
+ }
+ default:
+ System.out.println("Invalid choice");
+ }
+ }
+ }
+
public void start() {
+ AuthService authService = new AuthService();
+
while (true) {
System.out.println();
@@ -16,18 +62,50 @@ public class ConsoleMenu {
System.out.println("1. Login");
System.out.println("2. Register");
System.out.println("3. Exit");
+ System.out.print("Choose an option: ");
int choice = scanner.nextInt();
switch (choice) {
- case 1:
+ case 1: {
// TODO
+ System.out.print("\nEnter your username: ");
+ String username = scanner.next();
+ System.out.print("\nEnter your password: ");
+ String password = scanner.next();
+ System.out.println();
+ User user = authService.login(username, password);
+ if (user != null) {
+ System.out.println("Login success.");
+ mainMenu(user);
+ }
+ else {
+ System.out.println("Login failure.");
+ }
break;
+ }
- case 2:
+ case 2: {
// TODO
+ System.out.print("\nEnter your username: ");
+ String new_username = scanner.next();
+ System.out.print("\nEnter your password: ");
+ String new_password = scanner.next();
+ System.out.print("\nEnter your email (optional): ");
+ String new_email = scanner.next();
+ System.out.println();
+ boolean b = authService.register(new_username, new_password, new_email);
+ if (b) {
+ System.out.println("Registered successfully.");
+ }
+ else {
+ System.out.println("Registration failed.");
+ }
+ System.out.println("Press _c_ to continue...");
+ scanner.next();
break;
+ }
case 3:
return;