diff --git a/build.gradle b/build.gradle index e8bdf58..ada2461 100644 --- a/build.gradle +++ b/build.gradle @@ -1,56 +1,59 @@ plugins { - id 'java' - id 'application' - id 'org.javamodularity.moduleplugin' version '1.8.12' - id 'org.openjfx.javafxplugin' version '0.0.13' - id 'org.beryx.jlink' version '2.25.0' + id 'java' + id 'application' + id 'org.javamodularity.moduleplugin' version '1.8.12' + id 'org.openjfx.javafxplugin' version '0.0.13' + id 'org.beryx.jlink' version '2.25.0' } group 'AP.Restaurant' version '1.0-SNAPSHOT' repositories { - mavenCentral() + mavenCentral() } ext { - junitVersion = '5.10.2' + junitVersion = '5.10.2' } -sourceCompatibility = '23' -targetCompatibility = '23' +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} tasks.withType(JavaCompile) { - options.encoding = 'UTF-8' + options.encoding = 'UTF-8' } application { - mainModule = 'ap.restaurant.restaurant' - mainClass = 'ap.restaurant.restaurant.HelloApplication' + mainModule = 'ap.restaurant.restaurant' + mainClass = 'ap.restaurant.restaurant.HelloApplication' } javafx { - version = '17.0.6' - modules = ['javafx.controls', 'javafx.fxml'] + version = '21.0.7' + modules = ['javafx.controls', 'javafx.fxml'] } dependencies { - - testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") + testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") } test { -useJUnitPlatform()} + useJUnitPlatform() +} jlink { - imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip") - options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] - launcher { - name = 'app' - } + imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip") + options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] + launcher { + name = 'app' + } } jlinkZip { - group = 'distribution' -} \ No newline at end of file + group = 'distribution' +} diff --git a/src/.idea/.gitignore b/src/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/src/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml new file mode 100644 index 0000000..d4658b0 --- /dev/null +++ b/src/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/.idea/modules.xml b/src/.idea/modules.xml new file mode 100644 index 0000000..9b30282 --- /dev/null +++ b/src/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/.idea/vcs.xml b/src/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/src/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/ap/restaurant/restaurant/Authenticate.java b/src/main/java/ap/restaurant/restaurant/Authenticate.java new file mode 100644 index 0000000..ce8b078 --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/Authenticate.java @@ -0,0 +1,42 @@ +package ap.restaurant.restaurant; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Scanner; + +import static ap.restaurant.restaurant.User.users; + +public class Authenticate { + private String username; + private String password; + Scanner sc = new Scanner(System.in); + + public User signin() { + int id = (int)(Math.random() * 900) + 100; + System.out.println("your id: " + id); + System.out.println("Enter username: "); + String username = sc.nextLine(); + System.out.println("Enter password: "); + String password = sc.nextLine(); + System.out.println("Enter email: "); + String email = sc.nextLine(); + + User user = new User(id, username, password, email); + users.add(user); + System.out.println("ُSign in successful"); + return user; + } + + + public void login(User user) { + if (users.contains(user)) { + System.out.println("You are logged in!"); + } + } +} \ No newline at end of file diff --git a/src/main/java/ap/restaurant/restaurant/HelloApplication.java b/src/main/java/ap/restaurant/restaurant/HelloApplication.java index 6fc62a1..92f521c 100644 --- a/src/main/java/ap/restaurant/restaurant/HelloApplication.java +++ b/src/main/java/ap/restaurant/restaurant/HelloApplication.java @@ -5,14 +5,12 @@ import javafx.scene.Scene; import javafx.stage.Stage; -import java.io.IOException; - public class HelloApplication extends Application { @Override - public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 320, 240); - stage.setTitle("Hello!"); + public void start(Stage stage) throws Exception { + FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("login.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 400, 300); + stage.setTitle("AMD Restaurant"); stage.setScene(scene); stage.show(); } @@ -20,4 +18,4 @@ public void start(Stage stage) throws IOException { public static void main(String[] args) { launch(); } -} \ No newline at end of file +} diff --git a/src/main/java/ap/restaurant/restaurant/LoginController.java b/src/main/java/ap/restaurant/restaurant/LoginController.java new file mode 100644 index 0000000..93e3416 --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/LoginController.java @@ -0,0 +1,79 @@ +package ap.restaurant.restaurant; + +import javafx.fxml.FXML; +import javafx.scene.control.*; +import javafx.scene.Scene; +import javafx.stage.Stage; +import javafx.fxml.FXMLLoader; + +import java.io.IOException; +import java.sql.*; + +public class LoginController { + @FXML private TextField usernameField; + @FXML private PasswordField passwordField; + @FXML private TextField emailField; + @FXML private Label messageLabel; + + private final String url = "jdbc:postgresql://localhost:5432/Restaurant"; + private final String dbUser = "postgres"; + private final String dbPassword = "AmirMahdiImani"; + + @FXML + protected void onSignUp() { + String username = usernameField.getText(); + String password = passwordField.getText(); + String email = emailField.getText(); + + if (username.isEmpty() || password.isEmpty() || email.isEmpty()) { + messageLabel.setText("Please fill all fields."); + return; + } + + int id = (int)(Math.random() * 9000 + 1000); + + try (Connection conn = DriverManager.getConnection(url, dbUser, dbPassword)) { + String insert = "INSERT INTO users (id, username, password, email) VALUES (?, ?, ?, ?)"; + try (PreparedStatement stmt = conn.prepareStatement(insert)) { + stmt.setInt(1, id); + stmt.setString(2, username); + stmt.setString(3, password); + stmt.setString(4, email); + stmt.executeUpdate(); + messageLabel.setText("Signup successful!"); + } + } catch (SQLException e) { + messageLabel.setText("Error: " + e.getMessage()); + } + } + + @FXML + protected void onLogin() { + String username = usernameField.getText(); + String password = passwordField.getText(); + + try (Connection conn = DriverManager.getConnection(url, dbUser, dbPassword)) { + String query = "SELECT * FROM users WHERE username = ? AND password = ?"; + try (PreparedStatement stmt = conn.prepareStatement(query)) { + stmt.setString(1, username); + stmt.setString(2, password); + ResultSet rs = stmt.executeQuery(); + + if (rs.next()) { + int userId = rs.getInt("id"); + FXMLLoader loader = new FXMLLoader(getClass().getResource("menu.fxml")); + Scene menuScene = new Scene(loader.load()); + MenuController controller = loader.getController(); + controller.setUserId(userId); + + Stage stage = (Stage) usernameField.getScene().getWindow(); + stage.setScene(menuScene); + } else { + messageLabel.setText("Invalid login."); + } + } + } catch (SQLException | IOException e) { + messageLabel.setText("Error: " + e.getMessage()); + } + } +} diff --git a/src/main/java/ap/restaurant/restaurant/Main.java b/src/main/java/ap/restaurant/restaurant/Main.java new file mode 100644 index 0000000..9987736 --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/Main.java @@ -0,0 +1,147 @@ +package ap.restaurant.restaurant; + +import java.sql.*; +import java.util.Scanner; + +public class Main { + static Authenticate u = new Authenticate(); + public static void main(String[] args) throws SQLException { + String url = "jdbc:postgresql://localhost:5432/Restaurant"; + String user = "postgres"; + String password = "AmirMahdiImani"; + Scanner sc = new Scanner(System.in); + + try (Connection conn = DriverManager.getConnection(url, user, password)) { + if (conn != null) { + System.out.println("Connected"); + System.out.println("do you want to order something?"); + String order = sc.nextLine(); + if (order.equals("yes")) { + System.out.println("Please signup first: "); + User newUser = u.signin(); + String insertSQL = "INSERT INTO users (id, username, password, email) VALUES (?, ?, ?, ?)"; + try (PreparedStatement stmt = conn.prepareStatement(insertSQL)) { + stmt.setInt(1, newUser.getId()); + stmt.setString(2, newUser.getUsername()); + stmt.setString(3, newUser.getPassword()); + stmt.setString(4, newUser.getEmail()); + stmt.executeUpdate(); + System.out.println("User added to database."); + } catch (SQLException ex) { + System.out.println("Failed to insert user: " + ex.getMessage()); + } + System.out.println("Welcome to AMD Restaurant!"); + System.out.println("menu Items: "); + String selectMenu = "SELECT * FROM menuitem"; + try (Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(selectMenu)) { + + while (rs.next()) { + int id = rs.getInt("ids"); + String name = rs.getString("names"); + String description = rs.getString("description"); + double price = rs.getDouble("price"); + String category = rs.getString("category"); + System.out.println("ID: " + id + "| Name: " + name + "| Description: " + description + "| Price: " + price + "| Category: " + category); + } + } catch (SQLException e) { + System.out.println("Error retrieving menu items: " + e.getMessage()); + } + System.out.println("Enter the ID of the menu item you want to order:"); + int selectedMenuid = Integer.parseInt(sc.nextLine()); + + System.out.println("Enter quantity:"); + int quantity = Integer.parseInt(sc.nextLine()); + + String priceQuery = "SELECT price FROM menuitem WHERE ids = ?"; + double unitPrice = 0; + + try (PreparedStatement priceStmt = conn.prepareStatement(priceQuery)) { + priceStmt.setInt(1, selectedMenuid); + try (ResultSet rs = priceStmt.executeQuery()) { + if (rs.next()) { + unitPrice = rs.getDouble("price"); + } else { + System.out.println("Menu item not found!"); + return; + } + } + } catch (SQLException e) { + System.out.println("Error fetching price: " + e.getMessage()); + return; + } + + double totalPrice = unitPrice * quantity; + int orderid = 1; + int ID = (int)(Math.random() * 900) + 100; + + insertOrderDetails(conn, ID , orderid, selectedMenuid, quantity, totalPrice); + System.out.println("A good order,please confirm this order!\n Write yes to confirm:"); + String confirmOrder = sc.nextLine(); + if (confirmOrder.equals("yes")) { + String sql = "INSERT INTO orders (ids, userid, createdat, totalprice) VALUES (?, ?, ?, ?)"; + int orderId = (int) (Math.random() * 90) + 10; + int userId = newUser.getId(); + int createdAt = (int) (System.currentTimeMillis() / 1000); + double Totalprice = totalPrice; + try (Connection con = DriverManager.getConnection(url, user, password); + PreparedStatement pstmt = conn.prepareStatement(sql)) { + + pstmt.setInt(1, orderId); + pstmt.setInt(2, userId); + pstmt.setInt(3, createdAt); + pstmt.setDouble(4, Totalprice); + + int rowsInserted = pstmt.executeUpdate(); + + if (rowsInserted > 0) { + System.out.println("Order saved successfully!"); + } else { + System.out.println("Failed to save order."); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + } else { + System.out.println("Order not confirmed."); + } + + sc.close(); + System.out.println("please rate us:(from 10): "); + int rate = Integer.parseInt(sc.nextLine()); + System.out.println("Thank you for using AMD Restaurant!"); + return; + + } + + else{ + System.out.println("see you soon"); + return; + } + + } + else { + System.out.println("Not Connected"); + } + } catch (SQLException e) { + System.out.println("failed " + e.getMessage()); + } + + } + public static void insertOrderDetails(Connection conn,int ID , int orderid, int menuitemid, int quantity, double price) { + String insertSQL = "INSERT INTO orderdetails (id,orderid, menuitemid, quantityid, price) VALUES (?, ?, ?, ?,?)"; + try (PreparedStatement stmt = conn.prepareStatement(insertSQL)) { + stmt.setInt(1, ID); + stmt.setInt(2, orderid); + stmt.setInt(3, menuitemid); + stmt.setInt(4, quantity); + stmt.setDouble(5, price); + stmt.executeUpdate(); + System.out.println("Order detail added successfully."); + } catch (SQLException e) { + System.out.println("Failed to insert order detail: " + e.getMessage()); + } + } + +} diff --git a/src/main/java/ap/restaurant/restaurant/MenuController.java b/src/main/java/ap/restaurant/restaurant/MenuController.java new file mode 100644 index 0000000..4d0fc51 --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/MenuController.java @@ -0,0 +1,92 @@ +package ap.restaurant.restaurant; + +import javafx.fxml.FXML; +import javafx.scene.control.*; +import javafx.collections.*; + +import java.sql.*; +import java.time.Instant; + +public class MenuController { + @FXML private ListView menuList; + @FXML private TextField quantityField; + @FXML private Label orderMessage; + + private int userId; + private final String url = "jdbc:postgresql://localhost:5432/Restaurant"; + private final String dbUser = "postgres"; + private final String dbPassword = "AmirMahdiImani"; + + public void setUserId(int id) { + this.userId = id; + loadMenu(); + } + + private void loadMenu() { + ObservableList items = FXCollections.observableArrayList(); + + try (Connection conn = DriverManager.getConnection(url, dbUser, dbPassword)) { + String query = "SELECT * FROM menuitem"; + try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query)) { + while (rs.next()) { + String row = rs.getInt("ids") + " - " + rs.getString("names") + " (" + rs.getDouble("price") + ")"; + items.add(row); + } + menuList.setItems(items); + } + } catch (SQLException e) { + orderMessage.setText("Error loading menu."); + } + } + + @FXML + protected void onOrder() { + String selected = menuList.getSelectionModel().getSelectedItem(); + if (selected == null || quantityField.getText().isEmpty()) { + orderMessage.setText("Select item and quantity."); + return; + } + + int menuItemId = Integer.parseInt(selected.split(" ")[0]); + int quantity = Integer.parseInt(quantityField.getText()); + + try (Connection conn = DriverManager.getConnection(url, dbUser, dbPassword)) { + String priceQuery = "SELECT price FROM menuitem WHERE ids = ?"; + double price = 0; + try (PreparedStatement stmt = conn.prepareStatement(priceQuery)) { + stmt.setInt(1, menuItemId); + ResultSet rs = stmt.executeQuery(); + if (rs.next()) { + price = rs.getDouble("price"); + } + } + + int orderId = (int)(Math.random() * 9000 + 1000); + double total = quantity * price; + int timestamp = (int) Instant.now().getEpochSecond(); + + String orderSQL = "INSERT INTO orders (ids, userid, createdat, totalprice) VALUES (?, ?, ?, ?)"; + try (PreparedStatement stmt = conn.prepareStatement(orderSQL)) { + stmt.setInt(1, orderId); + stmt.setInt(2, userId); + stmt.setInt(3, timestamp); + stmt.setDouble(4, total); + stmt.executeUpdate(); + } + + String detailSQL = "INSERT INTO orderdetails (id, orderid, menuitemid, quantityid, price) VALUES (?, ?, ?, ?, ?)"; + try (PreparedStatement stmt = conn.prepareStatement(detailSQL)) { + stmt.setInt(1, (int)(Math.random() * 9000 + 1000)); + stmt.setInt(2, orderId); + stmt.setInt(3, menuItemId); + stmt.setInt(4, quantity); + stmt.setDouble(5, total); + stmt.executeUpdate(); + } + + orderMessage.setText("Order placed successfully!"); + } catch (SQLException e) { + orderMessage.setText("Error placing order."); + } + } +} diff --git a/src/main/java/ap/restaurant/restaurant/MenuItem.java b/src/main/java/ap/restaurant/restaurant/MenuItem.java new file mode 100644 index 0000000..fe817be --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/MenuItem.java @@ -0,0 +1,36 @@ +package ap.restaurant.restaurant; + +public class MenuItem { + private int id; + private String name; + private String description; + private double price; + 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 String getName() { + return name; + } + + public String getDescription() { + return description; + } + public double getPrice() { + return price; + } + public String getCategory() { + return category; + } + +} \ No newline at end of file diff --git a/src/main/java/ap/restaurant/restaurant/Order.java b/src/main/java/ap/restaurant/restaurant/Order.java new file mode 100644 index 0000000..749d32c --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/Order.java @@ -0,0 +1,28 @@ +package ap.restaurant.restaurant; + +public class Order { + private int ID; + private int userid; + private int createdat; + private double totalpricee; + + public Order(int ID, int userid, int createdat, double totalprice) { + this.ID = ID; + this.userid = userid; + this.createdat = createdat; + this.totalpricee = totalprice; + } + + public int getID() { + return ID; + } + public int getUserid() { + return userid; + } + public int getCreatedat() { + return createdat; + } + public double getTotalprice() { + return totalpricee; + } +} \ No newline at end of file diff --git a/src/main/java/ap/restaurant/restaurant/OrderDetails.java b/src/main/java/ap/restaurant/restaurant/OrderDetails.java new file mode 100644 index 0000000..164ed83 --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/OrderDetails.java @@ -0,0 +1,32 @@ +package ap.restaurant.restaurant; + +public class OrderDetails { + private int id; + private int orderId; + private int menuitemId; + private int quantityId; + private double price; + + public OrderDetails(int id, int orderId, int menuitemId, int quantityId, double price) { + this.id = id; + this.orderId = orderId; + this.menuitemId = menuitemId; + this.quantityId = quantityId; + this.price = price; + } + public int getId() { + return id; + } + public int getOrderId() { + return orderId; + } + public int getMenuitemId() { + return menuitemId; + } + public int getQuantityId() { + return quantityId; + } + public double getPrice() { + return price; + } +} \ No newline at end of file diff --git a/src/main/java/ap/restaurant/restaurant/SignUpController.java b/src/main/java/ap/restaurant/restaurant/SignUpController.java new file mode 100644 index 0000000..c928e46 --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/SignUpController.java @@ -0,0 +1,44 @@ +package ap.restaurant.restaurant; + +import javafx.fxml.FXML; +import javafx.scene.control.*; +import java.sql.*; + +public class SignUpController { + + @FXML private TextField usernameField; + @FXML private PasswordField passwordField; + @FXML private TextField emailField; + @FXML private Label messageLabel; + + private final String URL = "jdbc:postgresql://localhost:5432/Restaurant"; + private final String USER = "postgres"; + private final String PASSWORD = "AmirMahdiImani"; + + @FXML + private void handleSignUp() { + String username = usernameField.getText(); + String password = passwordField.getText(); + String email = emailField.getText(); + + if (username.isEmpty() || password.isEmpty()) { + messageLabel.setText("Username and password required."); + return; + } + + try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) { + String sql = "INSERT INTO users (id, username, password, email) VALUES (?, ?, ?, ?)"; + PreparedStatement stmt = conn.prepareStatement(sql); + int userId = (int)(Math.random() * 9000) + 1000; + stmt.setInt(1, userId); + stmt.setString(2, username); + stmt.setString(3, password); + stmt.setString(4, email); + stmt.executeUpdate(); + + messageLabel.setText("Sign up successful!"); + } catch (SQLException e) { + messageLabel.setText("Error: " + e.getMessage()); + } + } +} diff --git a/src/main/java/ap/restaurant/restaurant/User.java b/src/main/java/ap/restaurant/restaurant/User.java new file mode 100644 index 0000000..b7589af --- /dev/null +++ b/src/main/java/ap/restaurant/restaurant/User.java @@ -0,0 +1,22 @@ +package ap.restaurant.restaurant; + +import java.util.ArrayList; + +public class User { + private int id; + private String username; + private String password; + private String email; + public static ArrayList users = new ArrayList(); + + 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 String getUsername() { return username; } + public String getPassword() { return password; } + public String getEmail() { return email; } +} \ No newline at end of file diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 359f4ee..fc225b6 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,8 +1,8 @@ module ap.restaurant.restaurant { requires javafx.controls; requires javafx.fxml; - + requires java.sql; opens ap.restaurant.restaurant to javafx.fxml; exports ap.restaurant.restaurant; -} \ No newline at end of file +} diff --git a/src/main/resources/ap/restaurant/restaurant/login-view.tld b/src/main/resources/ap/restaurant/restaurant/login-view.tld new file mode 100644 index 0000000..a56279c --- /dev/null +++ b/src/main/resources/ap/restaurant/restaurant/login-view.tld @@ -0,0 +1,18 @@ + + + + + + + + + +