编写一个Java程序,写一个果蔬预订模块,可实现订单查询,销售,订单一览功能
时间: 2024-03-22 21:41:49 浏览: 66
好的,这是一个比较大的项目,我会尽可能详细地回答您的问题。
首先,我们需要设计一个数据库来存储果蔬预订的相关信息,包括订单信息和销售信息。我们可以使用MySQL或者其他关系型数据库来实现。
其次,我们需要设计一个Java程序来连接数据库并处理相关的业务逻辑。以下是一个简单的实现:
```
import java.sql.*;
import java.util.*;
public class FruitVegetableBooking {
private static final String DB_URL = "jdbc:mysql://localhost:3306/fruit_vegetable_booking";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.订单查询 2.销售 3.订单一览 4.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
searchOrder();
break;
case 2:
sell();
break;
case 3:
showOrders();
break;
case 4:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("无效的操作,请重新输入!");
break;
}
}
}
private static void searchOrder() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入订单号:");
int orderId = scanner.nextInt();
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM orders WHERE order_id = " + orderId)) {
if (rs.next()) {
System.out.println("订单号:" + rs.getInt("order_id"));
System.out.println("商品名称:" + rs.getString("product_name"));
System.out.println("数量:" + rs.getInt("quantity"));
System.out.println("价格:" + rs.getDouble("price"));
System.out.println("日期:" + rs.getDate("date"));
} else {
System.out.println("订单不存在!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void sell() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品名称:");
String productName = scanner.nextLine();
System.out.println("请输入数量:");
int quantity = scanner.nextInt();
System.out.println("请输入价格:");
double price = scanner.nextDouble();
Date date = new Date();
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO orders (product_name, quantity, price, date) VALUES (?, ?, ?, ?)")) {
stmt.setString(1, productName);
stmt.setInt(2, quantity);
stmt.setDouble(3, price);
stmt.setDate(4, date);
stmt.executeUpdate();
System.out.println("销售成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void showOrders() {
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM orders")) {
while (rs.next()) {
System.out.println("订单号:" + rs.getInt("order_id"));
System.out.println("商品名称:" + rs.getString("product_name"));
System.out.println("数量:" + rs.getInt("quantity"));
System.out.println("价格:" + rs.getDouble("price"));
System.out.println("日期:" + rs.getDate("date"));
System.out.println("---------------");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
这个程序实现了订单查询、销售和订单一览的功能。其中,订单查询和订单一览都是从数据库中获取数据并输出,而销售是向数据库中插入数据。
需要注意的是,这个程序还比较简单,没有进行异常处理和输入验证。在实际开发中,我们需要根据需求添加相应的功能和保证程序的健壮性。
希望这个回答能对您有所帮助。
阅读全文