java購物車案例
阿新 • • 發佈:2021-10-10
任務 | 姓名 |
---|---|
編碼規範、前期調查與功能設計 | 林小強 |
面向物件設計、PPT製作或部落格製作 | 陳澤役 |
1.前期調查、
- 可以在此直接搜尋商品
- 選擇對應商品加入購物車
- 購物車面板
- 刪除商品
2.系統功能結構圖
3.UML類圖
4.本系統哪裡體現了面向物件的封裝性
package ShoppingCart; public class Goods { private int id; //商品id private String name;//商品名稱 private double 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 double GetPrice() { return price; } public void SetPrice(double price) { this.price = price; } @Override public String toString() { return "goods [id=" + id + ", name=" + name + ", price=" + price + "]"; } public Goods(String name,double price){ this.name=name; this.price=price; } }
- 其中public、private都體現了面向物件的封裝性,private屬性私有化,通過使用private修飾符關鍵字進行修飾,外部無法直接訪問,只能通過getters來訪問。
5.專案包結構與關鍵程式碼
- 商品類
package ShoppingCart; public class Goods { private int id; //商品id private String name;//商品名稱 private double 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 double GetPrice() { return price; } public void SetPrice(double price) { this.price = price; } @Override public String toString() { return "goods [id=" + id + ", name=" + name + ", price=" + price + "]"; } public Goods(String name,double price){ this.name=name; this.price=price; } }
package ShoppingCart; public class Entry { private Goods item; private int number; private double Totalprice; //private ArrayList<Hash>HashList=new ArrayList<>();//商品陣列 public Goods GetItem() { return item; } public void SetItem(Goods item) { this.item = item; } public int GetNumber() { return number; } public void SetNumber(int number) { this.number = number; SetTotalprice(); } public double GetTotalprice() { return Totalprice; } public void SetTotalprice() { Totalprice = item.GetPrice()*number; } @Override public String toString() { return "entry [item=" + item + ", number=" + number + ", totalprice=" + Totalprice + "]"; } public Entry(Goods item,int number) { this.item=item; this.number=number; SetTotalprice(); } }
- 購物車類
package ShoppingCart;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class Cart {
HashMap<Integer,Entry> mp=new HashMap<>();//id,entry
private ArrayList<Hash>HashList=new ArrayList<>();//判斷id
public void AddGoods(int key,Entry item) {
if(mp.containsKey(key)){
//存在則增加數量
mp.get(key).SetNumber(mp.get(key).GetNumber()+item.GetNumber());//原來的數量+新增的數量
} else {
Hash temp=new Hash();
temp.key=key;
temp.name=item.GetItem().GetName();
HashList.add(temp);
mp.put(key,item);//不存在則增加商品
mp.get(key).GetItem().SetId(key);//置id
}
}
public boolean ModifyNumber(int key,int number){//修改商品的數量
if(!mp.containsKey(key))
return false;
mp.get(key).SetNumber(number);//修改數量
return true;
}
public boolean DeleteGoods(int number) {//刪除某種商品
if(mp.containsKey(number))
mp.remove(number);
else
return false;
return true;
}
public void ShowAll(){//檢視訂單資訊
Collection<Entry> goods = mp.values();
Iterator<Entry> iterator = goods.iterator();
while(iterator.hasNext()){
Entry productItem = iterator.next();
Goods product = productItem.GetItem();
System.out.println("商品ID:"+product.GetId()+",商品名稱:"+product.GetName()+",單價:"+product.GetPrice()
+",數量:"+productItem.GetNumber()
+",小計:"+productItem.GetTotalprice());
}
}
public void GetTotalPrice() {
Collection<Entry> goods = mp.values();
Iterator<Entry> iterator = goods.iterator();
double total=0;
while(iterator.hasNext()){
Entry productItem = iterator.next();
total+=productItem.GetTotalprice();
}
System.out.println("當前購物車總價格為:"+total+"元");
//return total;
}
public void clearcart() {//清空購物車
mp.clear();
}
//@SuppressWarnings("unlikely-arg-type")
public String GetUid(Entry pro){//判斷是否出現過
for(int i=0;i<HashList.size();i++) {
if(HashList.get(i).name.equals(pro.GetItem().GetName()))
return String.valueOf(HashList.get(i).key);
}
StringBuffer now = new StringBuffer(new SimpleDateFormat("yyyyMMdd").format(new Date()));
int n = (int)(Math.random() * 9000.0D + 1000.0D);
return now.append(n).toString().substring(4);// "202110063548"
}
}
-
新增商品
-
刪除商品