创建java程序:输入商品id ---> 显示商品详情 ---> 输入购买数量 ---> 输入商品id, 如此反复;最后生成订单,按下“去结账” 按钮,
时间: 2024-02-23 12:59:35 浏览: 33
好的,我可以帮您创建这个Java程序。首先,我们需要先定义一个商品类,包含商品的ID、名称、描述、价格等信息。代码如下:
```java
public class Product {
private int id;
private String name;
private String description;
private double price;
public Product(int id, String name, String description, double price) {
this.id = id;
this.name = name;
this.description = description;
this.price = price;
}
// getter and setter methods
}
```
接下来,我们需要定义一个商品列表,用于存储所有的商品信息。代码如下:
```java
import java.util.ArrayList;
import java.util.List;
public class ProductList {
private List<Product> productList;
public ProductList() {
this.productList = new ArrayList<>();
// add some default products
productList.add(new Product(1, "Product 1", "This is product 1", 10.0));
productList.add(new Product(2, "Product 2", "This is product 2", 20.0));
productList.add(new Product(3, "Product 3", "This is product 3", 30.0));
}
public Product getProductById(int id) {
for (Product product : productList) {
if (product.getId() == id) {
return product;
}
}
return null;
}
}
```
然后,我们需要定义一个订单类,用于存储用户选择的商品信息。代码如下:
```java
import java.util.HashMap;
import java.util.Map;
public class Order {
private Map<Integer, Integer> orderMap;
private ProductList productList;
public Order(ProductList productList) {
this.productList = productList;
this.orderMap = new HashMap<>();
}
public void addProduct(int productId, int quantity) {
if (productList.getProductById(productId) != null) {
orderMap.put(productId, quantity);
}
}
public double getTotalPrice() {
double totalPrice = 0.0;
for (Map.Entry<Integer, Integer> entry : orderMap.entrySet()) {
int productId = entry.getKey();
int quantity = entry.getValue();
Product product = productList.getProductById(productId);
totalPrice += product.getPrice() * quantity;
}
return totalPrice;
}
}
```
最后,我们需要定义一个主程序,用于输入商品ID、显示商品详情、输入购买数量、生成订单等操作。代码如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ProductList productList = new ProductList();
Order order = new Order(productList);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入商品ID(按0结束):");
int productId = scanner.nextInt();
if (productId == 0) {
break;
}
Product product = productList.getProductById(productId);
if (product == null) {
System.out.println("商品不存在,请重新输入!");
continue;
}
System.out.println("商品详情:" + product.getName() + " - " + product.getDescription() + " - 价格:" + product.getPrice() + "元");
System.out.print("请输入购买数量:");
int quantity = scanner.nextInt();
order.addProduct(productId, quantity);
}
System.out.println("订单总价:" + order.getTotalPrice() + "元");
System.out.println("按下“去结账”按钮,结算您的订单。");
}
}
```
这样,我们就完成了一个简单的商品购买程序。用户可以输入商品ID,查看商品详情,输入购买数量,然后不断重复此过程,直到输入0结束。最后,程序会输出订单总价和结账提示。
相关推荐
![java](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)