按照教材[案例7-8]的实现思路,在本实验第2题的基础上设计一个图书购物车,存储用户购买图书的信息。能够打印出购物车中图书的信息,图书价格、小计和总的费用。用HashMap类模拟购物车。 实现 (1)新建图书类(Product),该步骤第2题已实现。 (2)新建BookShop类,该类实现对所购图书信息的封装。 (3)新建书库类BookStore,书库里存放了大量的图书,这里我们用ArrayList来存放,并初始化部分图书信息。 (4)新建顾客类Customer。每个顾客进入书库都可以获得一个购物车,这个购物车用HashMap来实现,图书的编号是它的键,捆扎后的BookShop是它的值。 (5)新建测试主类SY6_3_BuyCar。
时间: 2024-03-18 13:46:05 浏览: 92
用HashMap模拟一个网上购物车
5星 · 资源好评率100%
好的,根据您的要求,我编写了以下代码示例:
Product类:
```java
public class Product {
private String id; // 图书编号
private String name; // 图书名称
private double price; // 图书价格
public Product(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
```
BookShop类:
```java
public class BookShop {
private Product product; // 所购图书
private int quantity; // 数量
public BookShop(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
public double getSubtotal() {
return product.getPrice() * quantity;
}
@Override
public String toString() {
return String.format("%-10s%-20s%-10.2f%-10d%-10.2f",
product.getId(), product.getName(), product.getPrice(), quantity, getSubtotal());
}
}
```
BookStore类:
```java
import java.util.ArrayList;
import java.util.List;
public class BookStore {
private List<Product> products;
public BookStore() {
products = new ArrayList<>();
products.add(new Product("001", "Java编程思想", 79.0));
products.add(new Product("002", "Java核心技术", 89.0));
products.add(new Product("003", "Java并发编程实战", 69.0));
}
public Product findProduct(String id) {
for (Product product : products) {
if (product.getId().equals(id)) {
return product;
}
}
return null;
}
}
```
Customer类:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Customer {
private String name; // 顾客姓名
private Map<String, BookShop> shoppingCart; // 购物车
private BookStore bookStore; // 书库
public Customer(String name, BookStore bookStore) {
this.name = name;
this.shoppingCart = new HashMap<>();
this.bookStore = bookStore;
}
public void addBookToCart() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要购买的图书编号:");
String id = scanner.nextLine();
Product product = bookStore.findProduct(id);
if (product == null) {
System.out.println("该图书不存在!");
} else {
System.out.print("请输入购买数量:");
int quantity = scanner.nextInt();
scanner.nextLine();
BookShop bookShop = shoppingCart.get(id);
if (bookShop != null) {
bookShop = new BookShop(product, bookShop.getQuantity() + quantity);
} else {
bookShop = new BookShop(product, quantity);
}
shoppingCart.put(id, bookShop);
System.out.println("已将图书添加到购物车!");
}
}
public void removeBookFromCart() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的图书编号:");
String id = scanner.nextLine();
BookShop bookShop = shoppingCart.get(id);
if (bookShop == null) {
System.out.println("购物车中不存在该图书!");
} else {
shoppingCart.remove(id);
System.out.println("已将图书从购物车中删除!");
}
}
public void printShoppingCart() {
System.out.println("===========================================");
System.out.println("顾客姓名:" + name);
System.out.println("购物车中的图书:");
System.out.println("===========================================");
System.out.printf("%-10s%-20s%-10s%-10s%-10s\n", "编号", "名称", "价格", "数量", "小计");
System.out.println("-------------------------------------------");
double totalCost = 0.0;
for (BookShop bookShop : shoppingCart.values()) {
System.out.println(bookShop);
totalCost += bookShop.getSubtotal();
}
System.out.println("-------------------------------------------");
System.out.printf("%-40s%-10.2f\n", "总费用:", totalCost);
System.out.println("===========================================");
}
}
```
SY6_3_BuyCar类:
```java
public class SY6_3_BuyCar {
public static void main(String[] args) {
BookStore bookStore = new BookStore();
Customer customer = new Customer("张三", bookStore);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加图书到购物车");
System.out.println("2. 从购物车中删除图书");
System.out.println("3. 查看购物车");
System.out.println("4. 退出系统");
System.out.print("请输入操作编号:");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
customer.addBookToCart();
break;
case 2:
customer.removeBookFromCart();
break;
case 3:
customer.printShoppingCart();
break;
case 4:
System.out.println("已退出系统!");
return;
default:
System.out.println("无效操作,请重新输入!");
}
}
}
}
```
希望能对您有所帮助!
阅读全文