其 他 回 答共2條
1樓
前學(xué)習(xí)java又做實例挺值得學(xué)習(xí)
1.首先我先列出我們所需要java類結(jié)構(gòu)
1)Database.java --------- 模擬存儲商品數(shù)據(jù)庫
2)McBean.java ------------ 商品實體類普通javabean里面有商品基本屬性
3)OrderItemBean.java --- 訂單表
4)ShoppingCar.java ------ 主要購物車當(dāng)比較簡單
5)TestShoppingCar.java --- 測試類
2.下面貼出具體代碼并帶關(guān)鍵注釋
---Database.java
public class Database {
/*采用Map存儲商品數(shù)據(jù)呢我覺得問題自己需要想下
* 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;//商品編號
private String name;//商品名
private double price;//商品價格
private String instuction;//商品說明
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; // 購物車所有商品總價格
private int totalCount; // 購物車所有商品數(shù)量
private Map<Integer, OrderItemBean> itemMap; // 商品編號與訂單項鍵值對
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("商品編號\t商品名稱\t單價\t購買數(shù)量\t總價");
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("合計: 總數(shù)量: " + df.format(totalCount) + " 總價格: " + df.format(totalPrice));
System.out.println("**********************************************");
}
}
---OrderItemBean.java
public class OrderItemBean {
private McBean thing;//商品實體
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);//購買商品編號1商品
s.buy(1);
s.buy(2);
s.buy(3);
s.buy(1);
s.show();//顯示購物車信息
s.delete(1);//刪除商品編號1商品
s.show();
s.clear();
s.show();
}
}
3.打印輸出結(jié)
商品編號 商品名稱 單價 購買數(shù)量 總價
1 地瓜 ¥2 3 ¥6
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計: 總數(shù)量: ¥5 總價格: ¥8.7
**********************************************
商品編號 商品名稱 單價 購買數(shù)量 總價
2 土豆 ¥1.2 1 ¥1.2
3 絲瓜 ¥1.5 1 ¥1.5
合計: 總數(shù)量: ¥2 總價格: ¥2.7
**********************************************
商品編號 商品名稱 單價 購買數(shù)量 總價
合計: 總數(shù)量: ¥0 總價格: ¥0
**********************************************
4.打字太累了比較匆忙主要靠自己領(lǐng)會哪里清楚再提出來
知識庫標(biāo)簽:
|列兵