java語言基礎--實現購物車的功能


Product.java

/**
 * 
 * 商品類
 *                 1.商品屬性
 *                 2.get-set
 *                 3.重寫toString、hashCode和equals方法
 *                 4.構造方法
 */
import java.math.BigDecimal;

public class Product {

    // 商品編號
    private int id;
    // 商品名稱
    private String name;
    // 商品價格 跟金融有關的數據我們一般用BigDecimal這個數據類型
    private BigDecimal price;
    
    public Product() {
        
    }

    public Product(int id, String name, BigDecimal price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "商品 [id=" + id + ", name=" + name + ", price=" + price + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((price == null) ? 0 : price.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Product other = (Product) obj;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (price == null) {
            if (other.price != null)
                return false;
        } else if (!price.equals(other.price))
            return false;
        return true;
    }
}

ShopCar.java

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 
 * 購物車類 1.創建Map集合 2.在構造方法中初始化map; 3.add方法,向購物車中添加商品 4.從購物車中刪除商品 5.清空購物車 6.打印
 */
public class ShopCar {

    // 存放商品信息和數量
    Map<Product, Integer> productMap;

    BigDecimal totalPrice = BigDecimal.valueOf(0.0);

    public ShopCar() {
        productMap = new HashMap<>();
    }

    // 向購物車中添加商品
    public void add(Product product, int num) {
        // 是否包含當前商品,若不包含,直接 添加
        if (!productMap.containsKey(product)) {
            productMap.put(product, num);
        } else {
            // 若包含
            int before = productMap.get(product); // 取得之前購物車中的商品數量
            int after = before + num;
            productMap.put(product, after);
        }

        // 總價
        totalPrice = totalPrice.add(product.getPrice().multiply(BigDecimal.valueOf(num)));
    }

    public void remove(Product product, int num) {

        // 獲取購物車中的商品數量
        int before = productMap.get(product);
        if (num >= before) {
            productMap.remove(product); // 將該商品刪除
            totalPrice= totalPrice.subtract(product.getPrice().multiply(BigDecimal.valueOf(before)));
        } else {
            int after = before - num;
            productMap.put(product, after);
            totalPrice= totalPrice.subtract(product.getPrice().multiply(BigDecimal.valueOf(num)));
        }
        
    }

    public void clear() {

        productMap.clear();
        totalPrice = BigDecimal.valueOf(0.0);
    }
    public void print() {
        System.out.println("商品清單");
        Set<Product> key = productMap.keySet();
        Iterator<Product> iter = key.iterator();
        while(iter.hasNext()) {
            Product product = iter.next();
            Integer i = productMap.get(product);
            System.out.println(i+ " " + product+ product.getPrice().multiply(BigDecimal.valueOf(i)));
        }
        System.out.println("總價:" + totalPrice);
    }
    
}

Test,java

import java.math.BigDecimal;

/**
 * 
 * 實現購物車的功能
 *
 * 分析: Product商品類 ShopCar購物車類 測試類Test
 */
public class Test {

    public static void main(String[] args) {
        Product apple = new Product(1001, "蘋果", BigDecimal.valueOf(10));
        Product bananer = new Product(1002, "香蕉", BigDecimal.valueOf(5));
        Product orange = new Product(1003, "橘子", BigDecimal.valueOf(15));
        Product longyan = new Product(1004, "龍眼", BigDecimal.valueOf(4.5));
        ShopCar sc = new ShopCar();
        sc.add(apple, 10);
        sc.add(longyan, 2);
        sc.add(orange, 20);
        sc.add(bananer, 3);
        
//        sc.remove(longyan, 2);
//        sc.add(apple, 10);
        sc.clear();
        sc.print();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM