idea实现药品销售统计系统代码
时间: 2024-02-24 18:55:39 浏览: 17
好的,下面是一个简单的药品销售统计系统的Java代码,仅供参考:
1. 数据库连接代码:
```
import java.sql.*;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/medicine_sales"; // 数据库连接地址
private static final String USERNAME = "root"; // 数据库用户名
private static final String PASSWORD = "123456"; // 数据库密码
// 获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载数据库驱动
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); // 获取数据库连接
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
```
2. 药品信息类:
```
public class Medicine {
private String id; // 药品编号
private String name; // 药品名称
private double price; // 药品单价
private int stock; // 药品库存
// 构造方法
public Medicine(String id, String name, double price, int stock) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
}
// Getter和Setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
```
3. 药品DAO类:
```
import java.sql.*;
public class MedicineDAO {
// 添加药品
public boolean addMedicine(Medicine medicine) {
Connection conn = null;
PreparedStatement pstmt = null;
boolean isSuccess = false;
try {
conn = DBUtil.getConnection(); // 获取数据库连接
String sql = "INSERT INTO medicine VALUES(?, ?, ?, ?)"; // SQL语句
pstmt = conn.prepareStatement(sql); // 创建PreparedStatement对象
pstmt.setString(1, medicine.getId()); // 设置参数
pstmt.setString(2, medicine.getName());
pstmt.setDouble(3, medicine.getPrice());
pstmt.setInt(4, medicine.getStock());
int result = pstmt.executeUpdate(); // 执行SQL语句
if (result > 0) {
isSuccess = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭PreparedStatement对象和数据库连接
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return isSuccess;
}
// 查询药品信息
public Medicine queryMedicine(String id) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Medicine medicine = null;
try {
conn = DBUtil.getConnection(); // 获取数据库连接
String sql = "SELECT * FROM medicine WHERE id = ?"; // SQL语句
pstmt = conn.prepareStatement(sql); // 创建PreparedStatement对象
pstmt.setString(1, id); // 设置参数
rs = pstmt.executeQuery(); // 执行SQL语句
if (rs.next()) {
// 获取查询结果
String name = rs.getString("name");
double price = rs.getDouble("price");
int stock = rs.getInt("stock");
medicine = new Medicine(id, name, price, stock);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭ResultSet、PreparedStatement对象和数据库连接
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return medicine;
}
}
```
4. 药品销售类:
```
import java.util.*;
public class MedicineSale {
private Map<Medicine, Integer> saleMap = new HashMap<>(); // 销售记录
// 添加销售记录
public void addSaleRecord(Medicine medicine, int quantity) {
if (saleMap.containsKey(medicine)) {
quantity += saleMap.get(medicine);
}
saleMap.put(medicine, quantity);
}
// 获取销售记录
public Map<Medicine, Integer> getSaleMap() {
return saleMap;
}
// 获取销售总金额
public double getTotalPrice() {
double totalPrice = 0;
for (Map.Entry<Medicine, Integer> entry : saleMap.entrySet()) {
Medicine medicine = entry.getKey();
int quantity = entry.getValue();
totalPrice += medicine.getPrice() * quantity;
}
return totalPrice;
}
}
```
以上代码仅提供药品销售统计系统的部分实现,具体实现过程需要根据具体需求进行调整。如果您需要更详细的指导,请告诉我您的具体需求。