其 他 回 答共2條
1樓
前學(xué)習(xí)java又做實(shí)例挺值得學(xué)習(xí)
1.首先我先列出我們所需要java類(lèi)結(jié)構(gòu)
1)Database.java --------- 模擬存儲(chǔ)商品數(shù)據(jù)庫(kù)
2)McBean.java ------------ 商品實(shí)體類(lèi)普通javabean里面有商品基本屬性
3)OrderItemBean.java --- 訂單表
4)ShoppingCar.java ------ 主要購(gòu)物車(chē)當(dāng)比較簡(jiǎn)單
5)TestShoppingCar.java --- 測(cè)試類(lèi)
2.下面貼出具體代碼并帶關(guān)鍵注釋
---Database.java
public class Database {
/*采用Map存儲(chǔ)商品數(shù)據(jù)呢我覺(jué)得問(wèn)題自己需要想下
* Integer Mapkey值McBeanMapvalue值
*/
private static Map<Integer, McBean> data = new HashMap<Integer, McBean>();
public Database() {
McBean bean = new McBean();
bean.setId(1);
bean.setName("地瓜");
bean.setPrice(2.0);
bean.setInstuction("新鮮地瓜");
data.put(1, bean);//把商品放入Map
bean = new McBean();
bean.setId(2);
bean.setName("土豆");
bean.setPrice(1.2);
bean.setInstuction("又好又大土豆");
data.put(2, bean);//把商品放入Map
bean = new McBean();
bean.setId(3);
bean.setName("絲瓜");
bean.setPrice(1.5);
bean.setInstuction("本地絲瓜");
data.put(3, bean);//把商品放入Map
}
public void setMcBean(McBean mcBean){
data.put(mcBean.getId(),mcBean);
}
public McBean getMcBean(int nid) {
return data.get(nid);
}
}
---McBean.java
public class McBean {
private int id;//商品編號(hào)
private String name;//商品名
private double price;//商品價(jià)格
private String instuction;//商品說(shuō)明
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;
}
public String getInstuction() {
return instuction;
}
public void setInstuction(String instuction) {
this.instuction = instuction;
}
}
---ShoppingCar.java
public class ShoppingCar {
private double totalPrice; // 購(gòu)物車(chē)所有商品總價(jià)格
private int totalCount; // 購(gòu)物車(chē)所有商品數(shù)量
private Map<Integer, OrderItemBean> itemMap; // 商品編號(hào)與訂單項(xiàng)鍵值對(duì)
public ShoppingCar() {
itemMap = new HashMap<Integer, OrderItemBean>();
}
public void buy(int nid) {
OrderItemBean order = itemMap.get(nid);
McBean mb;
if (order == null) {
mb = new Database().getMcBean(nid);
order = new OrderItemBean(mb, 1);
itemMap.put(nid, order);
update(nid, 1);
} else {
order.setCount(order.getCount() + 1);
update(nid, 1);
}
}
public void delete(int nid) {
OrderItemBean delorder = itemMap.remove(nid);
totalCount = totalCount - delorder.getCount();
totalPrice = totalPrice - delorder.getThing().getPrice() * delorder.getCount();
}
public void update(int nid, int count) {
OrderItemBean updorder = itemMap.get(nid);
totalCount = totalCount + count;
totalPrice = totalPrice + updorder.getThing().getPrice() * count;
}
public void clear() {
itemMap.clear();
totalCount = 0;
totalPrice = 0.0;
}
public void show() {
DecimalFormat df = new DecimalFormat("¤#.##");
System.out.println("商品編號(hào)\t商品名稱(chēng)\t單價(jià)\t購(gòu)買(mǎi)數(shù)量\t總價(jià)");
Set set = itemMap.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
OrderItemBean order = itemMap.get(it.next());
System.out.println(order.getThing().getId() + "\t"
+ order.getThing().getName() + "\t"
+ df.format(order.getThing().getPrice()) + "\t" + order.getCount()
+ "\t" + df.format(order.getCount() * order.getThing().getPrice()));
}
System.out.println("合計(jì): 總數(shù)量: " + df.format(totalCount) + " 總價(jià)格: " + df.format(totalPrice));
System.out.println("**********************************************");
}
}
---OrderItemBean.java
public class OrderItemBean {
private McBean thing;//商品實(shí)體
private int count;//商品數(shù)量
public OrderItemBean(McBean thing, int count) {
super();
this.thing = thing;
this.count = count;
}
public McBean getThing() {
return thing;
}
public void setThing(McBean thing) {
this.thing = thing;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
---TestShoppingCar.java
package com.shop;
public class TestShoppingCar {
public static void main(String[] args) {
ShoppingCar s = new ShoppingCar();
s.buy(1);//購(gòu)買(mǎi)商品編號(hào)1商品
s.buy(1);
s.buy(2);
s.buy(3);
s.buy(1);
s.show();//顯示購(gòu)物車(chē)信息
s.delete(1);//刪除商品編號(hào)1商品
s.show();
s.clear();
s.show();
}
}
3.打印輸出結(jié)
商品編號(hào) 商品名稱(chēng) 單價(jià) 購(gòu)買(mǎi)數(shù)量 總價(jià)
1 地瓜 ¥2 3 ¥6
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計(jì): 總數(shù)量: ¥5 總價(jià)格: ¥8.7
**********************************************
商品編號(hào) 商品名稱(chēng) 單價(jià) 購(gòu)買(mǎi)數(shù)量 總價(jià)
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計(jì): 總數(shù)量: ¥2 總價(jià)格: ¥2.7
**********************************************
商品編號(hào) 商品名稱(chēng) 單價(jià) 購(gòu)買(mǎi)數(shù)量 總價(jià)
合計(jì): 總數(shù)量: ¥0 總價(jià)格: ¥0
**********************************************
4.打字太累了比較匆忙主要靠自己領(lǐng)會(huì)哪里清楚再提出來(lái)
知識(shí)庫(kù)標(biāo)簽:
|列兵