你能用java模拟超市收银系统,要求 1、 由收银员输入顾客的会员卡卡号(若有卡)、所购商品的货号等,从数据库中取出商品的有关信息,如商品名称、价格等,再把这些信息返回给收银台,生成购买商品清单。 2、 同时计算出商品总数量、总金额。 3、 结算后把有关种类商品的剩余量存入数据库以供查询库存商品清单。 4、 另外,如果一个未持卡的顾客一次购物满200元,可为其发放一张会员卡,以后在该商场购物可享9折优惠。
时间: 2024-03-05 20:49:09 浏览: 20
当然可以,以下是一个简单的Java代码示例,实现了超市收银系统的基本功能:
```
import java.sql.*;
import java.util.*;
public class Supermarket {
public static void main(String[] args) {
// 连接数据库
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/supermarket", "root", "password");
} catch (Exception e) {
System.out.println("数据库连接失败!");
return;
}
// 定义HashMap,用于保存商品信息
Map<String, Double> goodsMap = new HashMap<>();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM goods");
while (rs.next()) {
String goodsId = rs.getString("id");
Double price = rs.getDouble("price");
goodsMap.put(goodsId, price);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 定义变量,记录商品总数量和金额
int totalNum = 0;
double totalPrice = 0.00;
// 定义Scanner,读取收银员输入的信息
Scanner scanner = new Scanner(System.in);
System.out.println("请输入顾客的会员卡卡号(若无卡请输入0):");
String cardNo = scanner.nextLine();
Map<String, Integer> goodsNumMap = new HashMap<>();
while (true) {
System.out.println("请输入购买商品的货号(输入0表示结束购买):");
String goodsId = scanner.nextLine();
if (goodsId.equals("0")) {
break;
} else if (!goodsMap.containsKey(goodsId)) {
System.out.println("商品不存在,请重新输入!");
continue;
}
System.out.println("请输入购买商品的数量:");
int goodsNum = scanner.nextInt();
scanner.nextLine();
goodsNumMap.put(goodsId, goodsNumMap.getOrDefault(goodsId, 0) + goodsNum);
totalNum += goodsNum;
totalPrice += goodsMap.get(goodsId) * goodsNum;
}
// 输出购买清单
System.out.println("商品清单:");
for (Map.Entry<String, Integer> entry : goodsNumMap.entrySet()) {
String goodsId = entry.getKey();
int goodsNum = entry.getValue();
String goodsName = "";
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT name FROM goods WHERE id='" + goodsId + "'");
if (rs.next()) {
goodsName = rs.getString("name");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(goodsId + " " + goodsName + " " + goodsNum + " " + goodsMap.get(goodsId) * goodsNum);
}
// 判断是否需要发放会员卡
if (cardNo.equals("0") && totalPrice >= 200.00) {
try {
stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO member_cards (discount) VALUES (0.9)");
rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
if (rs.next()) {
int cardId = rs.getInt(1);
System.out.println("发放会员卡成功,卡号为:" + cardId);
cardNo = String.valueOf(cardId);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 结算
try {
stmt = conn.createStatement();
for (Map.Entry<String, Integer> entry : goodsNumMap.entrySet()) {
String goodsId = entry.getKey();
int goodsNum = entry.getValue();
stmt.executeUpdate("UPDATE goods SET num=num-" + goodsNum + " WHERE id='" + goodsId + "'");
}
if (!cardNo.equals("0")) {
stmt.executeUpdate("INSERT INTO sales_records (card_no, total_price) VALUES ('" + cardNo + "', " + totalPrice + ")");
} else {
stmt.executeUpdate("INSERT INTO sales_records (total_price) VALUES (" + totalPrice + ")");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 输出结算结果
System.out.println("商品总数量:" + totalNum);
System.out.println("商品总金额:" + totalPrice);
}
}
```
需要注意的是,以上代码仅供参考,实际情况可能会更加复杂,具体实现还需要根据实际情况进行调整。