天天看點

購物車系統改進----java編寫購物車系統改進----java編寫

@TO購物車系統改進----java編寫C

購物車系統改進----java編寫

大家好! 這是我第一次使用 CSDN部落格 來記錄分享自己的旅途。歡迎大家指點我的不足,我是個菜鳥新人**^ - ^**

本人 @kingsirvince

Java 基礎–集合聯系

今天集合内容學完了 ,練習題是購物車系統:

  1. 添加商品;
  2. ** 删除**商品;
  3. 清空購物車;
  4. 顯示每類價格,總價格;

分析

  1. 建立類:商品類,購物車類,測試類
  2. 商品類Product:包含變量:id, name, price. 無參構造,有參構造,toString, get set, hashCode equals .
  3. 購物車類ShopCart:包含成員變量 productMap,(用Map的 key和value對應關系來記錄購買的商品名稱和數量),總價格BigDecimal totalPrice (涉及金融的用BigDecimal,保證運算精度),
  4. 購物車方法:添加add,删減remove,清空clear,顯示總價print
  5. 測試類Exercise06: 寫入商品詳細資訊,商品類指派;Scanner收集鍵盤輸入,判斷購物車動作,調用動作

商品類

// 商品類 Product
package com.king.ShopCart;

import java.math.BigDecimal;

public class Product {
private int id;
private String name;
private BigDecimal price;



public Product(){
	
}

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

@Override
public String toString() {
	return "Product [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;
}

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;
}


}

           

購物車類

//購物車類 ShopCart
package com.king.ShopCart;

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

public class ShopCart {

	Map<Product,Integer> productMap;//聲明一個成員變量, Map泛型的 變量,叫做 productMap
	BigDecimal totalPrice = BigDecimal.valueOf(0.0);
	
	public ShopCart() {
//		構造方法:定義成員變量productMap是一個  HashMap;new了就是開辟空間了,
		productMap = new HashMap<>();
	}
	
//	add
	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)));
		
	}
//	remove
	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)));
			
		}
	}
//	clear
	public void clear() {
		productMap.clear();
		totalPrice = BigDecimal.valueOf(0.0);
		
	}
//	print
	public void print() {
		Set<Product> key = productMap.keySet();
		Iterator<Product> iter = key.iterator();
		while(iter.hasNext()) {
			Product p = iter.next();
			int i = productMap.get(p);
			System.out.println(p+"\t個數\t"+i+"\t"+p.getPrice().multiply(BigDecimal.valueOf(i)));
		}
		System.out.println("總價:"+ totalPrice);
	}
}

           

測試類

//測試類 Exercise06
package com.king.ShopCart;

import java.math.BigDecimal;
import java.util.Scanner;

public class Exercise06 {

	public static void main(String[] args) {

		Product orange =new Product(1001, "orange", BigDecimal.valueOf(2.34));
		Product apple =new Product(1002, "apple", BigDecimal.valueOf(5.32));
		Product grape =new Product(1003, "grape", BigDecimal.valueOf(12.1));
		
		
		ShopCart sc = new ShopCart();
		Scanner s = new Scanner(System.in);
		System.out.println("歡迎購買");
		System.out.println(orange);
		System.out.println(apple);
		System.out.println(grape);
		while (true) {
			sc.print();
			System.out.println("選擇添加、删減、清空?");
			String first = s.next();
			if(first.equals("清空")) {
				sc.clear();
				}
			else if (first.equals("添加")) {
				System.out.println("輸入那種商品?");
				String pro = s.next();
				System.out.println("輸入幾斤?");
				int numb = s.nextInt();

				if (orange.getName().equals(pro)) {
					sc.add(orange, numb);
				} else if (apple.getName().equals(pro)) {
					sc.add(apple, numb);
				} else if (grape.getName().equals(pro)) {
					sc.add(grape, numb);
				}
			}else if(first.equals("删減")) {
				System.out.println("輸入那種商品?");
				String pro = s.next();
				System.out.println("輸入幾斤?");
				int numb = s.nextInt();

				if (orange.getName().equals(pro)) {
					sc.remove(orange, numb);
				} else if (apple.getName().equals(pro)) {
					sc.remove(apple, numb);
				} else if (grape.getName().equals(pro)) {
					sc.remove(grape, numb);
				}
			}
		}
	}
}

           

要點

  1. 使用
Map<Product,Integer> productMap;//聲明一個成員變量, Map泛型的 變量,叫做 productMap

totalPrice  = totalPrice.add(product.getPrice().multiply(BigDecimal.valueOf(num)));// BigDecimal用法