Java点餐系统,包括菜单管理、用户身份验证、订单处理和支付功能,并将数据存储到MySQL数据库中。
### 准备工作
首先,确保你已经创建了MySQL数据库,并且拥有以下两个表格来存储菜单和用户信息:
#### Menu 表结构
```sql
CREATE TABLE Menu (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2) NOT NULL
);
```
#### Users 表结构
```sql
CREATE TABLE Users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
isAdmin BOOLEAN NOT NULL
);
```
### Java 代码实现
#### MenuItem 类
```java
public class MenuItem {
private int id;
private String name;
private double price;
public MenuItem(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
// Getters and setters
}
```
#### Menu 类
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Menu {
private Connection conn;
public Menu() {
try {
this.conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name", "username", "password");
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<MenuItem> getMenuItems() {
List<MenuItem> menuItems = new ArrayList<>();
String sql = "SELECT * FROM Menu";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
menuItems.add(new MenuItem(id, name, price));
}
} catch (SQLException e) {
e.printStackTrace();
}
return menuItems;
}
public MenuItem getMenuItemByName(String name) {
String sql = "SELECT * FROM Menu WHERE name = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
double price = rs.getDouble("price");
return new MenuItem(id, name, price);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void addMenuItem(MenuItem item) {
String sql = "INSERT INTO Menu (name, price) VALUES (?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, item.getName());
stmt.setDouble(2, item.getPrice());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void removeMenuItem(MenuItem item) {
String sql = "DELETE FROM Menu WHERE id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, item.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateMenuItem(MenuItem item) {
String sql = "UPDATE Menu SET name = ?, price = ? WHERE id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, item.getName());
stmt.setDouble(2, item.getPrice());
stmt.setInt(3, item.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
#### User 类和 Authentication 类
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Authentication {
private Connection conn;
public Authentication() {
try {
this.conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name", "username", "password");
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean authenticate(String username, String password) {
String sql = "SELECT * FROM Users WHERE username = ? AND password = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public boolean isAdmin(String username) {
String sql = "SELECT isAdmin FROM Users WHERE username = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return rs.getBoolean("isAdmin");
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
```
#### Order 类
```java
import java.util.ArrayList;
import java.util.List;
public class Order {
private List<MenuItem> items;
private double total;
private boolean isPaid;
public Order() {
this.items = new ArrayList<>();
this.total = 0.0;
this.isPaid = false;
}
public void addItem(MenuItem item) {
items.add(item);
total += item.getPrice();
}
public void removeItem(MenuItem item) {
items.remove(item);
total -= item.getPrice();
}
public double calculateTotal() {
return total;
}
public void setPaid(boolean paid) {
isPaid = paid;
}
public boolean isPaid() {
return isPaid;
}
public List<MenuItem> getItems() {
return items;
}
}
```
#### Main 类
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Menu menu = new Menu();
Authentication authentication = new Authentication();
Order order = new Order();
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎使用点餐系统!");
boolean loggedIn = false;
String username = "";
while (!loggedIn) {
System.out.print("请输入用户名:");
username = scanner.nextLine().trim();
System.out.print("请输入密码:");
String password = scanner.nextLine().trim();
loggedIn = authentication.authenticate(username, password);
if (!loggedIn) {
System.out.println("登录失败,请重新输入用户名和密码。");
}
}
boolean isAdmin = authentication.isAdmin(username);
while (true) {
displayMenu(menu);
System.out.print("请输入菜品名称添加到订单(输入0结算):");
String userInput = scanner.nextLine().trim();
if (userInput.equals("0")) {
break;
}
MenuItem menuItem = menu.getMenuItemByName(userInput);
if (menuItem != null) {
order.addItem(menuItem);
System.out.println(menuItem.getName() + " 已添加到订单!");
} else {
System.out.println("抱歉,菜品不存在,请重新输入。");
}
}
// 结算订单
System.out.println("\n订单详情:");
for (MenuItem item : order.getItems()) {
System.out.println(item.getName() + "\t¥" + item.getPrice());
}
System.out.println("总计:¥" + order.calculateTotal());
// 支付处理
System.out.print("请选择支付方式(1.支付宝 2.信用卡):");
int paymentMethod = scanner.nextInt();
scanner.nextLine(); // 消耗换行符
// 模拟支付过程
System.out.println("支付中...");
// 这里可以添加具体的支付处理逻辑,例如调用支付接口等
order.setPaid(true);
System.out.println("支付成功!谢谢使用,再见!");
}
private static void displayMenu(Menu menu) {
System.out.println("\n菜单列表:");
for (MenuItem item : menu.getMenuItems()) {
System.out.println(item.getName() + "\t¥" + item.getPrice());
}
}
}
```