1 Commits
Author SHA1 Message Date
Arshida_0 976b779736 WS 10 2026-06-25 00:38:34 +04:30
22 changed files with 637 additions and 128 deletions
+10
View File
@@ -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/
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="WS-10-Database" />
</profile>
</annotationProcessing>
</component>
</project>
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
+20
View File
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://maven.devneeds.ir/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK" />
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/database.sql" dialect="PostgreSQL" />
<file url="file://$PROJECT_DIR$/src/main/java/dev/service/AuthService.java" dialect="GenericSQL" />
<file url="file://$PROJECT_DIR$/src/main/java/dev/service/OrderService.java" dialect="GenericSQL" />
</component>
</project>
Generated
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
+32 -117
View File
@@ -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');
-- =======================================================
+1 -1
View File
@@ -24,7 +24,7 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
<version>42.7.8</version>
</dependency>
</dependencies>
+50 -1
View File
@@ -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<MenuItem> 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;
}
+56 -1
View File
@@ -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<Order> 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;
}
}
+60 -1
View File
@@ -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<OrderDetail> findByOrderId(int orderId) {
@@ -18,7 +53,31 @@ public class OrderDetailDao {
// TODO:
// Retrieve order details
return null;
List<OrderDetail> 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;
}
}
+40
View File
@@ -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;
}
@@ -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);
}
}
+20
View File
@@ -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; }
}
+17
View File
@@ -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; }
}
+20
View File
@@ -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; }
}
+17
View File
@@ -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; }
}
+17 -1
View File
@@ -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;
}
}
@@ -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<MenuItem> Items = menuItemDao.findAll();
System.out.println("Available Items:");
for (MenuItem i : Items) {
System.out.println(i.getId() + " | " + i.getName() + " | " + i.getDescription() + " | " + "$" + i.getPrice());
}
}
}
+127
View File
@@ -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<OrderDetail> 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<Order> 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());
}
}
}
+80 -2
View File
@@ -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;