Java项目代码程序:输入商品id ---> 显示商品详情 ---> 输入购买数量 ---> 输入商品id, 如此反复;直至按下“去结账” 按钮 b )去结账 ---> 提示选择支付方式(会员卡/现金) ----------> 选择支付方式------- ----> 会员卡--->输入卡号 ---> 扣除金额 (如果余额不足,则提示现金支付) 现金 ----------------------> 扣除金额
时间: 2023-08-07 07:23:23 浏览: 42
好的,以下是一个简单的Java项目代码程序,实现了您的需求。您可以根据自己的需求进行修改和扩展。
```java
import java.util.*;
class Product {
public String id;
public String name;
public double price;
public int stock;
public Product(String id, String name, double price, int stock) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
}
}
class Cart {
public List<Product> items = new ArrayList<>();
public double totalPrice = 0;
public void add(Product product, int quantity) {
if (product.stock >= quantity) {
items.add(product);
totalPrice += product.price * quantity;
product.stock -= quantity;
} else {
System.out.println("库存不足!");
}
}
public void remove(Product product, int quantity) {
if (items.contains(product)) {
items.remove(product);
totalPrice -= product.price * quantity;
product.stock += quantity;
}
}
public void clear() {
items.clear();
totalPrice = 0;
}
}
class Payment {
public static boolean payByCard(double amount, String cardNumber) {
// TODO: 验证卡号是否正确,以及卡内余额是否足够支付
return true;
}
public static boolean payByCash(double amount) {
// TODO: 扣除现金
return true;
}
}
public class Shopping {
public static void main(String[] args) {
List<Product> products = new ArrayList<>();
products.add(new Product("001", "商品1", 10, 100));
products.add(new Product("002", "商品2", 20, 50));
products.add(new Product("003", "商品3", 30, 80));
Cart cart = new Cart();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入商品id(按下回车键确认):");
String id = scanner.nextLine();
if (id.equals("去结账")) {
break;
}
Product product = null;
for (Product p : products) {
if (p.id.equals(id)) {
product = p;
break;
}
}
if (product == null) {
System.out.println("没有该商品!");
continue;
}
System.out.println("商品详情:");
System.out.println("商品名称:" + product.name);
System.out.println("商品价格:" + product.price);
System.out.println("商品库存:" + product.stock);
System.out.print("请输入购买数量:");
int quantity = scanner.nextInt();
scanner.nextLine();
cart.add(product, quantity);
System.out.println("已加入购物车!");
}
double total = cart.totalPrice;
System.out.println("购物车商品列表:");
for (Product p : cart.items) {
System.out.println(p.name + " x " + cart.items.size() + " = " + p.price * cart.items.size());
}
System.out.println("总价:" + total);
while (true) {
System.out.println("请选择支付方式(输入数字):");
System.out.println("1. 会员卡");
System.out.println("2. 现金");
int choice = scanner.nextInt();
scanner.nextLine();
boolean success = false;
switch (choice) {
case 1:
System.out.print("请输入卡号:");
String cardNumber = scanner.nextLine();
success = Payment.payByCard(total, cardNumber);
if (!success) {
System.out.println("卡内余额不足,请使用现金支付!");
continue;
}
break;
case 2:
success = Payment.payByCash(total);
break;
default:
System.out.println("无效的选择!");
continue;
}
if (success) {
System.out.println("支付成功!");
break;
} else {
System.out.println("支付失败!");
}
}
}
}
```
希望能对您有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)