用java面向对象的思想写超市购物,超市的货物包括生活用品和学习用品等,购物者根据货物名能够查找是否有该货物,根据货号能查找该货物的价格,打印购物清单。统计货品销售的数量。
时间: 2023-05-13 17:07:14 浏览: 167
Java编程超市购物程序设计
5星 · 资源好评率100%
以下是一个简单的 Java 面向对象的超市购物程序:
```java
import java.util.*;
class Goods {
private String name;
private String id;
private double price;
private int quantity;
public Goods(String name, String id, double price, int quantity) {
this.name = name;
this.id = id;
this.price = price;
this.quantity = quantity;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String toString() {
return String.format("%s (%s) - %.2f", name, id, price);
}
}
class ShoppingCart {
private List<Goods> goodsList;
public ShoppingCart() {
goodsList = new ArrayList<>();
}
public void addGoods(Goods goods) {
goodsList.add(goods);
}
public void removeGoods(Goods goods) {
goodsList.remove(goods);
}
public double getTotalPrice() {
double totalPrice = 0;
for (Goods goods : goodsList) {
totalPrice += goods.getPrice() * goods.getQuantity();
}
return totalPrice;
}
public void printShoppingList() {
System.out.println("购物清单:");
for (Goods goods : goodsList) {
System.out.printf("%s x %d\n", goods.toString(), goods.getQuantity());
}
System.out.printf("总价:%.2f\n", getTotalPrice());
}
public void printSalesStatistics() {
Map<String, Integer> salesMap = new HashMap<>();
for (Goods goods : goodsList) {
String name = goods.getName();
int quantity = goods.getQuantity();
if (salesMap.containsKey(name)) {
quantity += salesMap.get(name);
}
salesMap.put(name, quantity);
}
System.out.println("销售统计:");
for (Map.Entry<String, Integer> entry : salesMap.entrySet()) {
System.out.printf("%s x %d\n", entry.getKey(), entry.getValue());
}
}
}
public class Supermarket {
public static void main(String[] args) {
Goods[] goodsArray = {
new Goods("牙刷", "001", 2.5, 100),
new Goods("牙膏", "002", 5.0, 50),
new Goods("笔记本", "003", 10.0, 20),
new Goods("铅笔", "004", 0.5, 200),
new Goods("橡皮", "005", 0.3, 300)
};
Map<String, Goods> goodsMap = new HashMap<>();
for (Goods goods : goodsArray) {
goodsMap.put(goods.getName(), goods);
}
ShoppingCart cart = new ShoppingCart();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入货物名或货号(输入 q 结束):");
String input = scanner.nextLine();
if (input.equals("q")) {
break;
}
Goods goods = null;
if (goodsMap.containsKey(input)) {
goods = goodsMap.get(input);
} else {
for (Goods g : goodsArray) {
if (g.getId().equals(input)) {
goods = g;
break;
}
}
}
if (goods == null) {
System.out.println("没有找到该货物!");
} else {
System.out.printf("货物信息:%s\n", goods.toString());
System.out.print("请输入购买数量:");
int quantity = scanner.nextInt();
scanner.nextLine();
if (quantity > goods.getQuantity()) {
System.out.println("库存不足!");
} else {
goods.setQuantity(goods.getQuantity() - quantity);
cart.addGoods(new Goods(goods.getName(), goods.getId(), goods.getPrice(), quantity));
System.out.println("添加成功!");
}
}
}
cart.printShoppingList();
cart.printSalesStatistics();
}
}
```
这个程序使用了两个类:`Goods` 和 `ShoppingCart`。`Goods` 类表示一个货物,包括名称、货号、价格和数量等信息。`ShoppingCart` 类表示一个购物车,包括添加、删除、打印购物清单和统计销售数量等功能。
程序首先定义了一些货物,然后将它们存储在一个 `Map` 中,以便根据名称或货号查找。然后程序进入一个循环,每次循环中用户输入货物名或货号,程序根据输入查找货物,然后让用户输入购买数量。如果库存不足,则添加失败;否则将货物添加到购物车中。最后程序打印购物清单和销售统计。
这个程序只是一个简单的示例,实际的超市购物程序可能会更加复杂,需要考虑库存管理、支付、优惠等问题。
阅读全文