java中使用oo思想收银结算,商品(名称,数量,单价),有非打折商品,打折商品(8折,9折),将来可能会有对折,返现等不同优惠。 编写一个程序,对一组商品 (用户输入) 进行录入并统计总价
时间: 2023-06-09 15:03:55 浏览: 42
好的,我明白了您的问题。这个问题涉及到 Java 中的面向对象编程,需要定义商品类、购物车类等对象,并实现不同种类的优惠策略。以下是一个可能的实现方案:
首先定义商品类,包含名称、数量、单价和是否打折等属性:
```java
public class Product {
private String name;
private int quantity;
private double price;
private boolean discounted;
public Product(String name, int quantity, double price, boolean discounted) {
this.name = name;
this.quantity = quantity;
this.price = price;
this.discounted = discounted;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isDiscounted() {
return discounted;
}
public void setDiscounted(boolean discounted) {
this.discounted = discounted;
}
public double getTotalPrice() {
return discounted ? price * quantity * 0.8 : price * quantity;
}
}
```
接下来定义购物车类,包含一个商品列表和计算总价的方法:
```java
public class ShoppingCart {
private List<Product> products;
public ShoppingCart() {
products = new ArrayList<>();
}
public void addProduct(Product product) {
products.add(product);
}
public double getTotalPrice() {
double totalPrice = 0;
for (Product product : products) {
totalPrice += product.getTotalPrice();
}
return totalPrice;
}
}
```
最后,可以在主程序中通过用户输入来创建商品和购物车对象,然后计算总价:
```java
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ShoppingCart cart = new ShoppingCart();
while (true) {
System.out.print("请输入商品名称(输入 exit 结束录入):");
String name = scanner.next();
if ("exit".equals(name)) {
break;
}
System.out.print("请输入商品数量:");
int quantity = scanner.nextInt();
System.out.print("请输入商品单价:");
double price = scanner.nextDouble();
System.out.print("是否打折(y/n):");
boolean discounted = "y".equals(scanner.next());
Product product = new Product(name, quantity, price, discounted);
cart.addProduct(product);
}
double totalPrice = cart.getTotalPrice();
System.out.printf("总价为:%.2f", totalPrice);
}
}
```
这样,就可以实现一个简单的收银系统。但是这个系统并不完整,还需要实现更多的优惠策略,比如对折、返现等等。如果有需要的话,可以继续完善它。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)